@@ -47,32 +47,43 @@ def func(*args):
4747 for (c_arg , arg ) in zip (ffi .typeof (a ).args , args ):
4848 # print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
4949 if c_arg .kind == 'pointer' :
50- if type (arg ) == str :
50+ if type (arg ) is str :
5151 arg = arg .encode ('utf-8' )
52- elif type (arg ) is bool :
53- arg = ffi .new ("bool *" , arg )
54- elif type (arg ) is int :
55- arg = ffi .new ("int *" , arg )
56- elif type (arg ) is float :
57- arg = ffi .new ("float *" , arg )
52+ # if c_arg is a 'char *' not a 'const char *' then we ought to raise here because its an out
53+ # parameter and user should supply a ctype pointer, but cffi cant detect const
54+ # so we would have to get the info from raylib.json
5855 elif type (arg ) is list and str (c_arg ) == "<ctype 'char * *'>" :
5956 arg = [ffi .new ("char[]" , x .encode ('utf-8' )) for x in arg ]
60- elif str (type (arg )) == "<class '_cffi_backend.__CDataOwn'>" and "*" not in str (arg ): # CPython
61- arg = ffi .addressof (arg )
62- elif str (type (arg )) == "<class '_cffi_backend._CDataBase'>" and "*" not in str (arg ): # Pypy
57+ elif is_cdata (arg ) and "*" not in str (arg ):
6358 arg = ffi .addressof (arg )
6459 elif arg is None :
6560 arg = ffi .NULL
61+ elif not is_cdata (arg ):
62+ if str (c_arg ) == "<ctype '_Bool *'>" :
63+ raise TypeError (
64+ "Argument must be a ctype bool, please create one with: pyray.ffi.new('bool *', True)" )
65+ elif str (c_arg ) == "<ctype 'int *'>" :
66+ raise TypeError (
67+ "Argument must be a ctype int, please create one with: pyray.ffi.new('int *', 1)" )
68+ elif str (c_arg ) == "<ctype 'float *'>" :
69+ raise TypeError (
70+ "Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)" )
6671 modified_args .append (arg )
6772 result = a (* modified_args )
6873 if result is None :
6974 return
70- if str ( type ( result )) == "<class '_cffi_backend._CDataBase'>" and str (result ).startswith ("<cdata 'char *'" ):
75+ elif is_cdata ( result ) and str (result ).startswith ("<cdata 'char *'" ):
7176 if str (result ) == "<cdata 'char *' NULL>" :
72- result = ""
77+ return ""
7378 else :
74- result = ffi .string (result ).decode ('utf-8' )
75- return result
79+ return ffi .string (result ).decode ('utf-8' )
80+ else :
81+ return result
82+
83+ # apparently pypy and cpython produce different types so check for both
84+ def is_cdata (arg ):
85+ return str (type (arg )) == "<class '_cffi_backend.__CDataOwn'>" or str (
86+ type (arg )) == "<class '_cffi_backend._CDataBase'>"
7687
7788 return func
7889
@@ -99,20 +110,16 @@ def func(*args):
99110
100111
101112for name , attr in getmembers (rl ):
102- # print(name, attr)
113+ #print(name, dir( attr) )
103114 uname = inflection .underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
104115 if isbuiltin (attr ) or str (type (attr )) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str (
105116 type (attr )) == "<class '_cffi_backend._CDataBase'>" :
106117 # print(attr.__call__)
107118 # print(attr.__doc__)
108- # print(attr.__text_signature__)
109119 # print(dir(attr))
110120 # print(dir(attr.__repr__))
111121 f = makefunc (attr )
112122 setattr (current_module , uname , f )
113- # def wrap(*args):
114- # print("call to ",attr)
115- # setattr(PyRay, uname, lambda *args: print("call to ",attr))
116123 else :
117124 setattr (current_module , name , attr )
118125
0 commit comments