@@ -47,32 +47,43 @@ def func(*args):
47
47
for (c_arg , arg ) in zip (ffi .typeof (a ).args , args ):
48
48
# print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
49
49
if c_arg .kind == 'pointer' :
50
- if type (arg ) == str :
50
+ if type (arg ) is str :
51
51
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
58
55
elif type (arg ) is list and str (c_arg ) == "<ctype 'char * *'>" :
59
56
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 ):
63
58
arg = ffi .addressof (arg )
64
59
elif arg is None :
65
60
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)" )
66
71
modified_args .append (arg )
67
72
result = a (* modified_args )
68
73
if result is None :
69
74
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 *'" ):
71
76
if str (result ) == "<cdata 'char *' NULL>" :
72
- result = ""
77
+ return ""
73
78
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'>"
76
87
77
88
return func
78
89
@@ -99,20 +110,16 @@ def func(*args):
99
110
100
111
101
112
for name , attr in getmembers (rl ):
102
- # print(name, attr)
113
+ #print(name, dir( attr) )
103
114
uname = inflection .underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
104
115
if isbuiltin (attr ) or str (type (attr )) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str (
105
116
type (attr )) == "<class '_cffi_backend._CDataBase'>" :
106
117
# print(attr.__call__)
107
118
# print(attr.__doc__)
108
- # print(attr.__text_signature__)
109
119
# print(dir(attr))
110
120
# print(dir(attr.__repr__))
111
121
f = makefunc (attr )
112
122
setattr (current_module , uname , f )
113
- # def wrap(*args):
114
- # print("call to ",attr)
115
- # setattr(PyRay, uname, lambda *args: print("call to ",attr))
116
123
else :
117
124
setattr (current_module , name , attr )
118
125
0 commit comments