Alien-SVN
view release on metacpan or search on metacpan
src/subversion/subversion/bindings/swig/include/proxy.swg view on Meta::CPAN
* how this _is_valid stuff actually works. It's part of the magic
* that lets us gracefully handle objects that are allocated from
* a pool that's been cleared or destroyed.
*/
%pythoncode %{
def _copy_metadata_deep(value, old_value):
"""Copy all attributes of old_value into value, recursively traversing
lists and dicts if needed."""
if value is None or old_value is None or value is old_value: return
if isinstance(value, dict):
for k, v in value.iteritems():
_copy_metadata_deep(v, old_value[k])
elif isinstance(value, list):
for v, old_v in zip(value, old_value):
_copy_metadata_deep(v, old_v)
else:
try:
value.__dict__.update(old_value.__dict__)
except AttributeError:
pass
def _assert_valid_deep(value):
"""Assert value's validity, recursively traversing lists and dicts."""
if isinstance(value, dict):
for v in value.itervalues():
_assert_valid_deep(v)
elif isinstance(value, list):
for v in value:
_assert_valid_deep(v)
else:
if hasattr(value, "assert_valid"):
value.assert_valid()
%}
/* Default code for all wrapped proxy classes in Python */
%define %proxy_pythoncode(TYPE)
%pythoncode {
def set_parent_pool(self, parent_pool=None):
"""Create a new proxy object for TYPE"""
import libsvn.core, weakref
self.__dict__["_parent_pool"] = \
parent_pool or libsvn.core.application_pool;
if self.__dict__["_parent_pool"]:
self.__dict__["_is_valid"] = weakref.ref(
self.__dict__["_parent_pool"]._is_valid)
def assert_valid(self):
"""Assert that this object is using valid pool memory"""
if "_is_valid" in self.__dict__:
assert self.__dict__["_is_valid"](), "Variable has already been deleted"
def __getattr__(self, name):
"""Get an attribute from this object"""
self.assert_valid()
value = _swig_getattr(self, self.__class__, name)
# If we got back a different object than we have, we need to copy all our
# metadata into it, so that it looks identical
members = self.__dict__.get("_members")
if members is not None:
_copy_metadata_deep(value, members.get(name))
# Verify that the new object is good
_assert_valid_deep(value)
return value
def __setattr__(self, name, value):
"""Set an attribute on this object"""
self.assert_valid()
# Save a copy of the object, so that the garbage
# collector won't kill the object while it's in
# SWIG-land
self.__dict__.setdefault("_members",{})[name] = value
return _swig_setattr(self, self.__class__, name, value)
}
%enddef
/* Define a proxy for wrapping an existing struct */
%define %proxy(TYPE)
%extend TYPE {
%proxy_pythoncode(TYPE);
}
%enddef
/* Define a proxy class for wrapping an opaque struct */
%define %opaque_proxy(TYPE)
struct TYPE {
%extend {
%proxy_pythoncode(TYPE);
}
}
%enddef
/* Treat typemapped function pointers as objects, which have a bound
* __call__ method. This mapping depends on the function pointer being
* typemapped as a CALLABLE_CALLBACK. */
%define %funcptr_proxy(TYPE, INVOKE)
%nodefault TYPE;
struct TYPE {
%extend {
%proxy_pythoncode(TYPE);
%pythoncode %{
def __call__(self, *args):
return INVOKE(self, *args)
%}
}
};
%enddef
/* Add member functions to objects so that their function pointers can
* be invoked.
*
* Unlike the CALLABLE_CALLBACKS, these member functions don't have or
* need typemaps, because the underlying C/SWIG object for the callback
( run in 0.435 second using v1.01-cache-2.11-cpan-d7f47b0818f )