11
11
# available at https://www.gnu.org/software/classpath/license.html.
12
12
#
13
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
14
+ import re
14
15
import weakref
15
16
from array import array
16
17
23
24
print ("sorry deprecated enums dont work on dynamic version" )
24
25
25
26
from inspect import getmembers , isbuiltin
26
- import inflection
27
27
28
28
current_module = __import__ (__name__ )
29
29
30
30
31
- def pointer (self , struct ):
31
+ def _underscore (word : str ) -> str :
32
+ """
33
+ from inflection
34
+ """
35
+ word = re .sub (r"([A-Z]+)([A-Z][a-z])" , r'\1_\2' , word )
36
+ word = re .sub (r"([a-z\d])([A-Z])" , r'\1_\2' , word )
37
+ word = word .replace ("-" , "_" )
38
+ return word .lower ()
39
+
40
+
41
+ def pointer (struct ):
32
42
return ffi .addressof (struct )
33
43
34
44
@@ -40,11 +50,11 @@ def pointer(self, struct):
40
50
# Another possibility is ffi.typeof() but that will throw an exception if you give it a type that isn't a ctype
41
51
# Another way to improve performance might be to special-case simple types before doing the string comparisons
42
52
43
- def makefunc ( a ):
53
+ def _wrap_function ( original_func ):
44
54
# print("makefunc ",a, ffi.typeof(a).args)
45
- def func (* args ):
55
+ def wrapped_func (* args ):
46
56
modified_args = []
47
- for (c_arg , arg ) in zip (ffi .typeof (a ).args , args ):
57
+ for (c_arg , arg ) in zip (ffi .typeof (original_func ).args , args ):
48
58
# print("arg:",str(arg), "c_arg.kind:", c_arg.kind, "c_arg:", c_arg, "type(arg):",str(type(arg)))
49
59
if c_arg .kind == 'pointer' :
50
60
if type (arg ) is str :
@@ -69,7 +79,7 @@ def func(*args):
69
79
raise TypeError (
70
80
"Argument must be a ctype float, please create one with: pyray.ffi.new('float *', 1.0)" )
71
81
modified_args .append (arg )
72
- result = a (* modified_args )
82
+ result = original_func (* modified_args )
73
83
if result is None :
74
84
return
75
85
elif is_cdata (result ) and str (result ).startswith ("<cdata 'char *'" ):
@@ -85,11 +95,13 @@ def is_cdata(arg):
85
95
return str (type (arg )) == "<class '_cffi_backend.__CDataOwn'>" or str (
86
96
type (arg )) == "<class '_cffi_backend._CDataBase'>"
87
97
88
- return func
98
+ return wrapped_func
99
+
89
100
90
101
global_weakkeydict = weakref .WeakKeyDictionary ()
91
102
92
- def makeStructHelper (struct ):
103
+
104
+ def _make_struct_constructor_function (struct ):
93
105
def func (* args ):
94
106
# print(struct, args)
95
107
modified_args = []
@@ -100,7 +112,7 @@ def func(*args):
100
112
elif (field [1 ].type .kind == 'pointer'
101
113
and (str (type (arg )) == "<class 'numpy.ndarray'>"
102
114
or isinstance (arg , (array , bytes , bytearray , memoryview )))):
103
- arg = ffi .from_buffer (field [1 ].type , arg )
115
+ arg = ffi .from_buffer (field [1 ].type , arg )
104
116
modified_args .append (arg )
105
117
s = ffi .new (f"struct { struct } *" , modified_args )[0 ]
106
118
global_weakkeydict [s ] = modified_args
@@ -110,21 +122,21 @@ def func(*args):
110
122
111
123
112
124
for name , attr in getmembers (rl ):
113
- #print(name, dir( attr) )
114
- uname = inflection . underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
125
+ # print(name, attr)
126
+ uname = _underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
115
127
if isbuiltin (attr ) or str (type (attr )) == "<class '_cffi_backend.__FFIFunctionWrapper'>" or str (
116
128
type (attr )) == "<class '_cffi_backend._CDataBase'>" :
117
129
# print(attr.__call__)
118
130
# print(attr.__doc__)
119
131
# print(dir(attr))
120
132
# print(dir(attr.__repr__))
121
- f = makefunc (attr )
133
+ f = _wrap_function (attr )
122
134
setattr (current_module , uname , f )
123
135
else :
124
136
setattr (current_module , name , attr )
125
137
126
138
for struct in ffi .list_types ()[0 ]:
127
- f = makeStructHelper (struct )
139
+ f = _make_struct_constructor_function (struct )
128
140
setattr (current_module , struct , f )
129
141
130
142
# overwrite ffi enums with our own
0 commit comments