Proxy to another object.
| 49 | |
| 50 | |
| 51 | class Proxy: |
| 52 | """Proxy to another object.""" |
| 53 | |
| 54 | # Code stolen from werkzeug.local.Proxy. |
| 55 | __slots__ = ('__local', '__args', '__kwargs', '__dict__') |
| 56 | |
| 57 | def __init__(self, local, |
| 58 | args=None, kwargs=None, name=None, __doc__=None): |
| 59 | object.__setattr__(self, '_Proxy__local', local) |
| 60 | object.__setattr__(self, '_Proxy__args', args or ()) |
| 61 | object.__setattr__(self, '_Proxy__kwargs', kwargs or {}) |
| 62 | if name is not None: |
| 63 | object.__setattr__(self, '__custom_name__', name) |
| 64 | if __doc__ is not None: |
| 65 | object.__setattr__(self, '__doc__', __doc__) |
| 66 | |
| 67 | @_default_cls_attr('name', str, __name__) |
| 68 | def __name__(self): |
| 69 | try: |
| 70 | return self.__custom_name__ |
| 71 | except AttributeError: |
| 72 | return self._get_current_object().__name__ |
| 73 | |
| 74 | @_default_cls_attr('qualname', str, __name__) |
| 75 | def __qualname__(self): |
| 76 | try: |
| 77 | return self.__custom_name__ |
| 78 | except AttributeError: |
| 79 | return self._get_current_object().__qualname__ |
| 80 | |
| 81 | @_default_cls_attr('module', str, __module__) |
| 82 | def __module__(self): |
| 83 | return self._get_current_object().__module__ |
| 84 | |
| 85 | @_default_cls_attr('doc', str, __doc__) |
| 86 | def __doc__(self): |
| 87 | return self._get_current_object().__doc__ |
| 88 | |
| 89 | def _get_class(self): |
| 90 | return self._get_current_object().__class__ |
| 91 | |
| 92 | @property |
| 93 | def __class__(self): |
| 94 | return self._get_class() |
| 95 | |
| 96 | def _get_current_object(self): |
| 97 | """Get current object. |
| 98 | |
| 99 | This is useful if you want the real |
| 100 | object behind the proxy at a time for performance reasons or because |
| 101 | you want to pass the object into a different context. |
| 102 | """ |
| 103 | loc = object.__getattribute__(self, '_Proxy__local') |
| 104 | if not hasattr(loc, '__release_local__'): |
| 105 | return loc(*self.__args, **self.__kwargs) |
| 106 | try: # pragma: no cover |
| 107 | # not sure what this is about |
| 108 | return getattr(loc, self.__name__) |
no outgoing calls