forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_pythonization.py
355 lines (287 loc) · 11.8 KB
/
_pythonization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
""" Pythonization API.
"""
# TODO: externalize this (have PythonizationScope and UserPythonizations as
# globals here and picked up from this module
# TODO: set explicit export list
# TODO: move cast to cppyy.lowlevel or some sort
# TODO: remove all need for accessing _backend
def _set_backend( backend ):
global _backend
_backend = backend
def set_pythonization_scope(scope):
_backend.PythonizationScope = scope
if scope not in _backend.UserPythonizations:
_backend.UserPythonizations[scope] = []
def add_pythonization(pythonizor):
"""Takes a callable that should take two arguments -- the class proxy,
and its C++ name -- and which is called the first time the named
class is bound.
"""
scope = _backend.PythonizationScope
#scope = _pythonization_scope
if pythonizor and not callable(pythonizor):
raise TypeError("given '%s' object is not callable" % str(pythonizor))
if pythonizor:
# _pythonizations[scope]
_backend.UserPythonizations[scope].append(pythonizor)
def pin_type(derived_type, base_type):
_backend.SetTypePinning(derived_type, base_type)
def make_interface(base_type):
pin_type(base_type, base_type)
def ignore_type_pinning(some_type):
_backend.IgnoreTypePinning(some_type)
def cast(some_object, new_type):
return _backend.Cast(some_object, new_type)
def add_exception_mapping(cpp_exception, py_exception):
_backend.UserExceptions[cpp_exception] = py_exception
#--- Pythonization factories --------------------------------------------
def set_gil_policy(match_class, match_method, release_gil=True):
return set_method_property(match_class, match_method, '_threaded', int(release_gil))
def set_ownership_policy(match_class, match_method, python_owns_result):
return set_method_property(match_class, match_method,
'_creates', int(python_owns_result))
def set_smart_ptr_policy(match_class, match_method, manage_smart_ptr=False):
return set_method_property(match_class, match_method,
'_manage_smart_ptr', bool(manage_smart_ptr))
# NB: Ideally, we'd use the version commented out below, but for now, we
# make do with the hackier version here.
def rename_attribute(match_class, orig_attribute, new_attribute, keep_orig=False):
class attribute_pythonizor(object):
class getter(object):
def __init__(self, attr):
self.attr = attr
def __call__(self, obj):
return getattr(obj, self.attr)
class setter(object):
def __init__(self, attr):
self.attr = attr
def __call__(self, obj, value):
return setattr(obj, self.attr, value)
class deleter(object):
def __init__(self, attr):
self.attr = attr
def __call__(self, obj):
return delattr(obj, self.attr)
def __init__(self, match_class, orig_attribute, new_attribute, keep_orig):
import re
self.match_class = re.compile(match_class)
self.match_attr = re.compile(orig_attribute)
self.new_attr = new_attribute
self.keep_orig = keep_orig
def __call__(self, obj, name):
if not self.match_class.match(name):
return
for k in dir(obj): #.__dict__:
if self.match_attr.match(k):
tmp = property(self.getter(k), self.setter(k), self.deleter(k))
setattr(obj, self.new_attr, tmp)
#if not self.keep_orig: delattr(obj, k)
return attribute_pythonizor(match_class, orig_attribute, new_attribute, keep_orig)
# def rename_attribute(match_class, orig_attribute, new_attribute, keep_orig=False):
# class method_pythonizor:
# def __init__(self, match_class, orig_attribute, new_attribute, keep_orig):
# import re
# self.match_class = re.compile(match_class)
# self.match_attr = re.compile(orig_attribute)
# self.new_attr = new_attribute
# self.keep_orig = keep_orig
# def __call__(self, obj, name):
# import sys
# if not self.match_class.match(name):
# return
# sys.stderr.write("%s %s %s %s" % ("!!!", obj, name, "\n"))
# for k in dir(obj): #obj.__dict__:
# if not self.match_attr.match(k): continue
# try:
# tmp = getattr(obj, k)
# except Exception as e:
# continue
# setattr(obj, self.new_attr, tmp)
# if not self.keep_orig: delattr(obj, k)
# return method_pythonizor(match_class, orig_attribute, new_attribute, keep_orig)
# Shared with PyPy:
def add_overload(match_class, match_method, overload):
class method_pythonizor(object):
def __init__(self, match_class, match_method, overload):
import re
self.match_class = re.compile(match_class)
self.match_method = re.compile(match_method)
self.overload = overload
def __call__(self, obj, name):
if not self.match_class.match(name):
return
for k in dir(obj): #.__dict__:
try:
tmp = getattr(obj, k)
except:
continue
if self.match_method.match(k):
try:
tmp.__add_overload__(overload)
except AttributeError: pass
return method_pythonizor(match_class, match_method, overload)
def compose_method(match_class, match_method, g):
class composition_pythonizor(object):
def __init__(self, match_class, match_method, g):
import re
self.match_class = re.compile(match_class)
self.match_method = re.compile(match_method)
self.g = g
def __call__(self, obj, name):
if not self.match_class.match(name):
return
g = self.g
for k in obj.__dict__:
if not self.match_method.match(k):
continue
try:
f = getattr(obj, k)
except:
continue
def make_fun(f, g):
def h(self, *args, **kwargs):
return g(self, f(self, *args, **kwargs))
return h
h = make_fun(f, g)
setattr(obj, k, h)
return composition_pythonizor(match_class, match_method, g)
def set_method_property(match_class, match_method, prop, value):
class method_pythonizor(object):
def __init__(self, match_class, match_method, prop, value):
import re
self.match_class = re.compile(match_class)
self.match_method = re.compile(match_method)
self.prop = prop
self.value = value
def __call__(self, obj, name):
if not self.match_class.match(name):
return
for k in dir(obj): #.__dict__:
try:
tmp = getattr(obj, k)
except:
continue
if self.match_method.match(k):
setattr(tmp, self.prop, self.value)
return method_pythonizor(match_class, match_method, prop, value)
def make_property(match_class, match_get, match_set=None, match_del=None, prop_name=None):
class property_pythonizor(object):
def __init__(self, match_class, match_get, match_set, match_del, prop_name):
import re
self.match_class = re.compile(match_class)
self.match_get = re.compile(match_get)
match_many_getters = self.match_get.groups == 1
if match_set:
self.match_set = re.compile(match_set)
match_many_setters = self.match_set.groups == 1
if match_many_getters ^ match_many_setters:
raise ValueError('Must match getters and setters equally')
else:
self.match_set = None
if match_del:
self.match_del = re.compile(match_del)
match_many_deleters = self.match_del.groups == 1
if match_many_getters ^ match_many_deleters:
raise ValueError('Must match getters and deleters equally')
else:
self.match_del = None
self.match_many = match_many_getters
if not (self.match_many or prop_name):
raise ValueError("If not matching properties by regex, need a property name with exactly one substitution field")
if self.match_many and prop_name:
if prop_name.format(').!:(') == prop_name:
raise ValueError("If matching properties by regex and providing a property name, the name needs exactly one substitution field")
self.prop_name = prop_name
def make_get_del_proxy(self, getter):
class proxy(object):
def __init__(self, getter):
self.getter = getter
def __call__(self, obj):
return getattr(obj, self.getter)()
return proxy(getter)
def make_set_proxy(self, setter):
class proxy(object):
def __init__(self, setter):
self.setter = setter
def __call__(self, obj, arg):
return getattr(obj, self.setter)(arg)
return proxy(setter)
def __call__(self, obj, name):
if not self.match_class.match(name):
return
names = []
named_getters = {}
named_setters = {}
named_deleters = {}
if not self.match_many:
fget, fset, fdel = None, None, None
for k in dir(obj): #.__dict__:
match = self.match_get.match(k)
try:
tmp = getattr(obj, k)
except:
continue
if match and hasattr(tmp, '__call__'):
if self.match_many:
name = match.group(1)
named_getters[name] = k
else:
fget = self.make_get_del_proxy(k)
break
if self.match_set:
for k in dir(obj): #.__dict__:
match = self.match_set.match(k)
try:
tmp = getattr(obj, k)
except:
continue
if match and hasattr(tmp, '__call__'):
if self.match_many:
name = match.group(1)
named_setters[name] = k
else:
fset = self.make_set_proxy(k)
break
if self.match_del:
for k in dir(obj): #.__dict__:
match = self.match_del.match(k)
try:
tmp = getattr(obj, k)
except:
continue
if match and hasattr(tmp, '__call__'):
if self.match_many:
name = match.group(1)
named_deleters[name] = k
else:
fdel = self.make_get_del_proxy(k)
break
if not self.match_many:
new_prop = property(fget, fset, fdel)
setattr(obj, self.prop_name, new_prop)
return
names += list(named_getters.keys())
names += list(named_setters.keys())
names += list(named_deleters.keys())
names = set(names)
properties = []
for name in names:
if name in named_getters:
fget = self.make_get_del_proxy(named_getters[name])
else:
fget = None
if name in named_setters:
fset = self.make_set_proxy(named_setters[name])
else:
fset = None
if name in named_deleters:
fdel = self.make_get_del_proxy(named_deleters[name])
else:
fdel = None
new_prop = property(fget, fset, fdel)
if self.prop_name:
prop_name = self.prop_name.format(name)
else:
prop_name = name
setattr(obj, prop_name, new_prop)
return property_pythonizor(match_class, match_get, match_set, match_del, prop_name)