17 _backend.PythonizationScope = scope
18 if scope
not in _backend.UserPythonizations:
19 _backend.UserPythonizations[scope] = []
23 """Takes a callable that should take two arguments -- the class proxy,
24 and its C++ name -- and which is called the first time the named
27 scope = _backend.PythonizationScope
29 if pythonizor
and not callable(pythonizor):
30 raise TypeError(
"given '%s' object is not callable" % str(pythonizor))
33 _backend.UserPythonizations[scope].append(pythonizor)
37 _backend.SetTypePinning(derived_type, base_type)
45 _backend.IgnoreTypePinning(some_type)
48 def cast(some_object, new_type):
49 return _backend.Cast(some_object, new_type)
53 _backend.UserExceptions[cpp_exception] = py_exception
65 '_creates', int(python_owns_result))
69 '_manage_smart_ptr', bool(manage_smart_ptr))
75 class attribute_pythonizor(object):
77 def __init__(self, attr):
79 def __call__(self, obj):
80 return getattr(obj, self.attr)
83 def __init__(self, attr):
85 def __call__(self, obj, value):
86 return setattr(obj, self.attr, value)
88 class deleter(object):
89 def __init__(self, attr):
91 def __call__(self, obj):
92 return delattr(obj, self.attr)
94 def __init__(self, match_class, orig_attribute, new_attribute, keep_orig):
96 self.match_class = re.compile(match_class)
97 self.match_attr = re.compile(orig_attribute)
98 self.new_attr = new_attribute
99 self.keep_orig = keep_orig
101 def __call__(self, obj, name):
102 if not self.match_class.match(name):
105 if self.match_attr.match(k):
106 tmp = property(self.getter(k), self.setter(k), self.deleter(k))
107 setattr(obj, self.new_attr, tmp)
109 return attribute_pythonizor(match_class, orig_attribute, new_attribute, keep_orig)
139 class method_pythonizor(object):
140 def __init__(self, match_class, match_method, overload):
142 self.match_class = re.compile(match_class)
143 self.match_method = re.compile(match_method)
144 self.overload = overload
146 def __call__(self, obj, name):
147 if not self.match_class.match(name):
151 tmp = getattr(obj, k)
154 if self.match_method.match(k):
156 tmp.__add_overload__(overload)
157 except AttributeError:
pass
158 return method_pythonizor(match_class, match_method, overload)
162 class composition_pythonizor(object):
163 def __init__(self, match_class, match_method, g):
165 self.match_class = re.compile(match_class)
166 self.match_method = re.compile(match_method)
169 def __call__(self, obj, name):
170 if not self.match_class.match(name):
174 if not self.match_method.match(k):
180 def h(self, *args, **kwargs):
181 return g(self,
f(self, *args, **kwargs))
183 return composition_pythonizor(match_class, match_method, g)
187 class method_pythonizor(object):
188 def __init__(self, match_class, match_method, prop, value):
190 self.match_class = re.compile(match_class)
191 self.match_method = re.compile(match_method)
195 def __call__(self, obj, name):
196 if not self.match_class.match(name):
200 tmp = getattr(obj, k)
203 if self.match_method.match(k):
204 setattr(tmp, self.prop, self.value)
205 return method_pythonizor(match_class, match_method, prop, value)
208 def make_property(match_class, match_get, match_set=None, match_del=None, prop_name=None):
209 class property_pythonizor(object):
210 def __init__(self, match_class, match_get, match_set, match_del, prop_name):
212 self.match_class = re.compile(match_class)
214 self.match_get = re.compile(match_get)
215 match_many_getters = self.match_get.groups == 1
218 self.match_set = re.compile(match_set)
219 match_many_setters = self.match_set.groups == 1
220 if match_many_getters ^ match_many_setters:
221 raise ValueError(
'Must match getters and setters equally')
223 self.match_set =
None
226 self.match_del = re.compile(match_del)
227 match_many_deleters = self.match_del.groups == 1
228 if match_many_getters ^ match_many_deleters:
229 raise ValueError(
'Must match getters and deleters equally')
231 self.match_del =
None
233 self.match_many = match_many_getters
234 if not (self.match_many
or prop_name):
235 raise ValueError(
"If not matching properties by regex, need a property name with exactly one substitution field")
236 if self.match_many
and prop_name:
237 if prop_name.format(
').!:(') == prop_name:
238 raise ValueError(
"If matching properties by regex and providing a property name, the name needs exactly one substitution field")
240 self.prop_name = prop_name
242 def make_get_del_proxy(self, getter):
244 def __init__(self, getter):
247 def __call__(self, obj):
248 return getattr(obj, self.getter)()
251 def make_set_proxy(self, setter):
253 def __init__(self, setter):
256 def __call__(self, obj, arg):
257 return getattr(obj, self.setter)(arg)
260 def __call__(self, obj, name):
261 if not self.match_class.match(name):
269 if not self.match_many:
270 fget, fset, fdel =
None,
None,
None
273 match = self.match_get.match(k)
275 tmp = getattr(obj, k)
278 if match
and hasattr(tmp,
'__call__'):
280 name = match.group(1)
281 named_getters[name] = k
283 fget = self.make_get_del_proxy(k)
288 match = self.match_set.match(k)
290 tmp = getattr(obj, k)
293 if match
and hasattr(tmp,
'__call__'):
295 name = match.group(1)
296 named_setters[name] = k
298 fset = self.make_set_proxy(k)
303 match = self.match_del.match(k)
305 tmp = getattr(obj, k)
308 if match
and hasattr(tmp,
'__call__'):
310 name = match.group(1)
311 named_deleters[name] = k
313 fdel = self.make_get_del_proxy(k)
316 if not self.match_many:
317 new_prop = property(fget, fset, fdel)
318 setattr(obj, self.prop_name, new_prop)
321 names += list(named_getters.keys())
322 names += list(named_setters.keys())
323 names += list(named_deleters.keys())
328 if name
in named_getters:
329 fget = self.make_get_del_proxy(named_getters[name])
333 if name
in named_setters:
334 fset = self.make_set_proxy(named_setters[name])
338 if name
in named_deleters:
339 fdel = self.make_get_del_proxy(named_deleters[name])
343 new_prop = property(fget, fset, fdel)
345 prop_name = self.prop_name.format(name)
349 setattr(obj, prop_name, new_prop)
351 return property_pythonizor(match_class, match_get, match_set, match_del, prop_name)
def add_pythonization(pythonizor)
def add_exception_mapping(cpp_exception, py_exception)
def set_pythonization_scope(scope)
def make_interface(base_type)
def set_method_property(match_class, match_method, prop, value)
def cast(some_object, new_type)
def _set_backend(backend)
def compose_method(match_class, match_method, g)
def add_overload(match_class, match_method, overload)
def set_ownership_policy(match_class, match_method, python_owns_result)
def ignore_type_pinning(some_type)
def pin_type(derived_type, base_type)