123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- import operator
- from .compat import PY2
- from .compat import PY3
- from .compat import string_types
- from .compat import with_metaclass
- from .utils import await_
- from .utils import identity
- class _ProxyMethods(object):
- # We use properties to override the values of __module__ and
- # __doc__. If we add these in ObjectProxy, the derived class
- # __dict__ will still be setup to have string variants of these
- # attributes and the rules of descriptors means that they appear to
- # take precedence over the properties in the base class. To avoid
- # that, we copy the properties into the derived class type itself
- # via a meta class. In that way the properties will always take
- # precedence.
- @property
- def __module__(self):
- return self.__wrapped__.__module__
- @__module__.setter
- def __module__(self, value):
- self.__wrapped__.__module__ = value
- @property
- def __doc__(self):
- return self.__wrapped__.__doc__
- @__doc__.setter
- def __doc__(self, value):
- self.__wrapped__.__doc__ = value
- # We similar use a property for __dict__. We need __dict__ to be
- # explicit to ensure that vars() works as expected.
- @property
- def __dict__(self):
- return self.__wrapped__.__dict__
- # Need to also propagate the special __weakref__ attribute for case
- # where decorating classes which will define this. If do not define
- # it and use a function like inspect.getmembers() on a decorator
- # class it will fail. This can't be in the derived classes.
- @property
- def __weakref__(self):
- return self.__wrapped__.__weakref__
- class _ProxyMetaType(type):
- def __new__(cls, name, bases, dictionary):
- # Copy our special properties into the class so that they
- # always take precedence over attributes of the same name added
- # during construction of a derived class. This is to save
- # duplicating the implementation for them in all derived classes.
- dictionary.update(vars(_ProxyMethods))
- return type.__new__(cls, name, bases, dictionary)
- class Proxy(with_metaclass(_ProxyMetaType)):
- """
- A proxy implementation in pure Python, using slots. You can subclass this to add
- local methods or attributes, or enable __dict__.
- The most important internals:
- * ``__factory__`` is the callback that "materializes" the object we proxy to.
- * ``__target__`` will contain the object we proxy to, once it's "materialized".
- * ``__resolved__`` is a boolean, `True` if factory was called.
- * ``__wrapped__`` is a property that does either:
- * return ``__target__`` if it's set.
- * calls ``__factory__``, saves result to ``__target__`` and returns said result.
- """
- __slots__ = '__target__', '__factory__'
- def __init__(self, factory):
- object.__setattr__(self, '__factory__', factory)
- @property
- def __resolved__(self, __getattr__=object.__getattribute__):
- try:
- __getattr__(self, '__target__')
- except AttributeError:
- return False
- else:
- return True
- @property
- def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__,
- __delattr__=object.__delattr__):
- try:
- return __getattr__(self, '__target__')
- except AttributeError:
- try:
- factory = __getattr__(self, '__factory__')
- except AttributeError:
- raise ValueError("Proxy hasn't been initiated: __factory__ is missing.")
- target = factory()
- __setattr__(self, '__target__', target)
- return target
- @__wrapped__.deleter
- def __wrapped__(self, __delattr__=object.__delattr__):
- __delattr__(self, '__target__')
- @__wrapped__.setter
- def __wrapped__(self, target, __setattr__=object.__setattr__):
- __setattr__(self, '__target__', target)
- @property
- def __name__(self):
- return self.__wrapped__.__name__
- @__name__.setter
- def __name__(self, value):
- self.__wrapped__.__name__ = value
- @property
- def __class__(self):
- return self.__wrapped__.__class__
- @__class__.setter # noqa: F811
- def __class__(self, value): # noqa: F811
- self.__wrapped__.__class__ = value
- @property
- def __annotations__(self):
- return self.__wrapped__.__anotations__
- @__annotations__.setter
- def __annotations__(self, value):
- self.__wrapped__.__annotations__ = value
- def __dir__(self):
- return dir(self.__wrapped__)
- def __str__(self):
- return str(self.__wrapped__)
- if PY3:
- def __bytes__(self):
- return bytes(self.__wrapped__)
- def __repr__(self, __getattr__=object.__getattribute__):
- try:
- target = __getattr__(self, '__target__')
- except AttributeError:
- return '<{} at 0x{:x} with factory {!r}>'.format(
- type(self).__name__, id(self),
- self.__factory__
- )
- else:
- return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
- type(self).__name__, id(self),
- target, id(target),
- self.__factory__
- )
- def __fspath__(self):
- wrapped = self.__wrapped__
- if isinstance(wrapped, string_types):
- return wrapped
- else:
- fspath = getattr(wrapped, '__fspath__', None)
- if fspath is None:
- return wrapped
- else:
- return fspath()
- def __reversed__(self):
- return reversed(self.__wrapped__)
- if PY3:
- def __round__(self):
- return round(self.__wrapped__)
- def __lt__(self, other):
- return self.__wrapped__ < other
- def __le__(self, other):
- return self.__wrapped__ <= other
- def __eq__(self, other):
- return self.__wrapped__ == other
- def __ne__(self, other):
- return self.__wrapped__ != other
- def __gt__(self, other):
- return self.__wrapped__ > other
- def __ge__(self, other):
- return self.__wrapped__ >= other
- def __hash__(self):
- return hash(self.__wrapped__)
- def __nonzero__(self):
- return bool(self.__wrapped__)
- def __bool__(self):
- return bool(self.__wrapped__)
- def __setattr__(self, name, value, __setattr__=object.__setattr__):
- if hasattr(type(self), name):
- __setattr__(self, name, value)
- else:
- setattr(self.__wrapped__, name, value)
- def __getattr__(self, name):
- if name in ('__wrapped__', '__factory__'):
- raise AttributeError(name)
- else:
- return getattr(self.__wrapped__, name)
- def __delattr__(self, name, __delattr__=object.__delattr__):
- if hasattr(type(self), name):
- __delattr__(self, name)
- else:
- delattr(self.__wrapped__, name)
- def __add__(self, other):
- return self.__wrapped__ + other
- def __sub__(self, other):
- return self.__wrapped__ - other
- def __mul__(self, other):
- return self.__wrapped__ * other
- def __div__(self, other):
- return operator.div(self.__wrapped__, other)
- def __truediv__(self, other):
- return operator.truediv(self.__wrapped__, other)
- def __floordiv__(self, other):
- return self.__wrapped__ // other
- def __mod__(self, other):
- return self.__wrapped__ % other
- def __divmod__(self, other):
- return divmod(self.__wrapped__, other)
- def __pow__(self, other, *args):
- return pow(self.__wrapped__, other, *args)
- def __lshift__(self, other):
- return self.__wrapped__ << other
- def __rshift__(self, other):
- return self.__wrapped__ >> other
- def __and__(self, other):
- return self.__wrapped__ & other
- def __xor__(self, other):
- return self.__wrapped__ ^ other
- def __or__(self, other):
- return self.__wrapped__ | other
- def __radd__(self, other):
- return other + self.__wrapped__
- def __rsub__(self, other):
- return other - self.__wrapped__
- def __rmul__(self, other):
- return other * self.__wrapped__
- def __rdiv__(self, other):
- return operator.div(other, self.__wrapped__)
- def __rtruediv__(self, other):
- return operator.truediv(other, self.__wrapped__)
- def __rfloordiv__(self, other):
- return other // self.__wrapped__
- def __rmod__(self, other):
- return other % self.__wrapped__
- def __rdivmod__(self, other):
- return divmod(other, self.__wrapped__)
- def __rpow__(self, other, *args):
- return pow(other, self.__wrapped__, *args)
- def __rlshift__(self, other):
- return other << self.__wrapped__
- def __rrshift__(self, other):
- return other >> self.__wrapped__
- def __rand__(self, other):
- return other & self.__wrapped__
- def __rxor__(self, other):
- return other ^ self.__wrapped__
- def __ror__(self, other):
- return other | self.__wrapped__
- def __iadd__(self, other):
- self.__wrapped__ += other
- return self
- def __isub__(self, other):
- self.__wrapped__ -= other
- return self
- def __imul__(self, other):
- self.__wrapped__ *= other
- return self
- def __idiv__(self, other):
- self.__wrapped__ = operator.idiv(self.__wrapped__, other)
- return self
- def __itruediv__(self, other):
- self.__wrapped__ = operator.itruediv(self.__wrapped__, other)
- return self
- def __ifloordiv__(self, other):
- self.__wrapped__ //= other
- return self
- def __imod__(self, other):
- self.__wrapped__ %= other
- return self
- def __ipow__(self, other):
- self.__wrapped__ **= other
- return self
- def __ilshift__(self, other):
- self.__wrapped__ <<= other
- return self
- def __irshift__(self, other):
- self.__wrapped__ >>= other
- return self
- def __iand__(self, other):
- self.__wrapped__ &= other
- return self
- def __ixor__(self, other):
- self.__wrapped__ ^= other
- return self
- def __ior__(self, other):
- self.__wrapped__ |= other
- return self
- def __neg__(self):
- return -self.__wrapped__
- def __pos__(self):
- return +self.__wrapped__
- def __abs__(self):
- return abs(self.__wrapped__)
- def __invert__(self):
- return ~self.__wrapped__
- def __int__(self):
- return int(self.__wrapped__)
- if PY2:
- def __long__(self):
- return long(self.__wrapped__) # noqa
- def __float__(self):
- return float(self.__wrapped__)
- def __oct__(self):
- return oct(self.__wrapped__)
- def __hex__(self):
- return hex(self.__wrapped__)
- def __index__(self):
- if hasattr(self.__wrapped__, '__index__'):
- return operator.index(self.__wrapped__)
- else:
- return int(self.__wrapped__)
- def __len__(self):
- return len(self.__wrapped__)
- def __contains__(self, value):
- return value in self.__wrapped__
- def __getitem__(self, key):
- return self.__wrapped__[key]
- def __setitem__(self, key, value):
- self.__wrapped__[key] = value
- def __delitem__(self, key):
- del self.__wrapped__[key]
- def __getslice__(self, i, j):
- return self.__wrapped__[i:j]
- def __setslice__(self, i, j, value):
- self.__wrapped__[i:j] = value
- def __delslice__(self, i, j):
- del self.__wrapped__[i:j]
- def __enter__(self):
- return self.__wrapped__.__enter__()
- def __exit__(self, *args, **kwargs):
- return self.__wrapped__.__exit__(*args, **kwargs)
- def __iter__(self):
- return iter(self.__wrapped__)
- def __next__(self):
- return next(self.__wrapped__)
- def __call__(self, *args, **kwargs):
- return self.__wrapped__(*args, **kwargs)
- def __reduce__(self):
- return identity, (self.__wrapped__,)
- def __reduce_ex__(self, protocol):
- return identity, (self.__wrapped__,)
- if await_:
- from .utils import __aenter__
- from .utils import __aexit__
- from .utils import __aiter__
- from .utils import __anext__
- from .utils import __await__
- __aiter__, __anext__, __await__, __aenter__, __aexit__ # noqa
|