diff options
author | Jason Kirtland <jek@discorporate.us> | 2008-03-06 14:12:22 +0000 |
---|---|---|
committer | Jason Kirtland <jek@discorporate.us> | 2008-03-06 14:12:22 +0000 |
commit | 4f6d0ff71e72fc4f5171a062eda6b577475bb3b1 (patch) | |
tree | 9503dfd9d214348f2dedc723d2a925732e6e7615 /lib/sqlalchemy/util.py | |
parent | 06d55b8e1daf43ff91b81d23c59ddf11c17ace52 (diff) | |
download | sqlalchemy-4f6d0ff71e72fc4f5171a062eda6b577475bb3b1.tar.gz |
- Synonyms riding on top of existing descriptors are now full proxies
to those descriptors.
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r-- | lib/sqlalchemy/util.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 4e8a837c9..9adb3983d 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -344,6 +344,35 @@ def warn_exception(func, *args, **kwargs): except: warn("%s('%s') ignored" % sys.exc_info()[0:2]) +def monkeypatch_proxied_specials(into_cls, from_cls, skip=None, only=None, + name='self.proxy', from_instance=None): + """Automates delegation of __specials__ for a proxying type.""" + + if only: + dunders = only + else: + if skip is None: + skip = ('__slots__', '__del__', '__getattribute__', + '__metaclass__', '__getstate__', '__setstate__') + dunders = [m for m in dir(from_cls) + if (m.startswith('__') and m.endswith('__') and + not hasattr(into_cls, m) and m not in skip)] + for method in dunders: + try: + spec = inspect.getargspec(getattr(from_cls, method)) + fn_args = inspect.formatargspec(spec[0]) + d_args = inspect.formatargspec(spec[0][1:]) + except TypeError: + fn_args = '(self, *args, **kw)' + d_args = '(*args, **kw)' + + py = ("def %(method)s%(fn_args)s: " + "return %(name)s.%(method)s%(d_args)s" % locals()) + + env = from_instance is not None and {name: from_instance} or {} + exec py in env + setattr(into_cls, method, env[method]) + class SimpleProperty(object): """A *default* property accessor.""" |