slots.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. import operator
  2. from .compat import PY2
  3. from .compat import PY3
  4. from .compat import string_types
  5. from .compat import with_metaclass
  6. from .utils import await_
  7. from .utils import identity
  8. class _ProxyMethods(object):
  9. # We use properties to override the values of __module__ and
  10. # __doc__. If we add these in ObjectProxy, the derived class
  11. # __dict__ will still be setup to have string variants of these
  12. # attributes and the rules of descriptors means that they appear to
  13. # take precedence over the properties in the base class. To avoid
  14. # that, we copy the properties into the derived class type itself
  15. # via a meta class. In that way the properties will always take
  16. # precedence.
  17. @property
  18. def __module__(self):
  19. return self.__wrapped__.__module__
  20. @__module__.setter
  21. def __module__(self, value):
  22. self.__wrapped__.__module__ = value
  23. @property
  24. def __doc__(self):
  25. return self.__wrapped__.__doc__
  26. @__doc__.setter
  27. def __doc__(self, value):
  28. self.__wrapped__.__doc__ = value
  29. # We similar use a property for __dict__. We need __dict__ to be
  30. # explicit to ensure that vars() works as expected.
  31. @property
  32. def __dict__(self):
  33. return self.__wrapped__.__dict__
  34. # Need to also propagate the special __weakref__ attribute for case
  35. # where decorating classes which will define this. If do not define
  36. # it and use a function like inspect.getmembers() on a decorator
  37. # class it will fail. This can't be in the derived classes.
  38. @property
  39. def __weakref__(self):
  40. return self.__wrapped__.__weakref__
  41. class _ProxyMetaType(type):
  42. def __new__(cls, name, bases, dictionary):
  43. # Copy our special properties into the class so that they
  44. # always take precedence over attributes of the same name added
  45. # during construction of a derived class. This is to save
  46. # duplicating the implementation for them in all derived classes.
  47. dictionary.update(vars(_ProxyMethods))
  48. return type.__new__(cls, name, bases, dictionary)
  49. class Proxy(with_metaclass(_ProxyMetaType)):
  50. """
  51. A proxy implementation in pure Python, using slots. You can subclass this to add
  52. local methods or attributes, or enable __dict__.
  53. The most important internals:
  54. * ``__factory__`` is the callback that "materializes" the object we proxy to.
  55. * ``__target__`` will contain the object we proxy to, once it's "materialized".
  56. * ``__resolved__`` is a boolean, `True` if factory was called.
  57. * ``__wrapped__`` is a property that does either:
  58. * return ``__target__`` if it's set.
  59. * calls ``__factory__``, saves result to ``__target__`` and returns said result.
  60. """
  61. __slots__ = '__target__', '__factory__'
  62. def __init__(self, factory):
  63. object.__setattr__(self, '__factory__', factory)
  64. @property
  65. def __resolved__(self, __getattr__=object.__getattribute__):
  66. try:
  67. __getattr__(self, '__target__')
  68. except AttributeError:
  69. return False
  70. else:
  71. return True
  72. @property
  73. def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__,
  74. __delattr__=object.__delattr__):
  75. try:
  76. return __getattr__(self, '__target__')
  77. except AttributeError:
  78. try:
  79. factory = __getattr__(self, '__factory__')
  80. except AttributeError:
  81. raise ValueError("Proxy hasn't been initiated: __factory__ is missing.")
  82. target = factory()
  83. __setattr__(self, '__target__', target)
  84. return target
  85. @__wrapped__.deleter
  86. def __wrapped__(self, __delattr__=object.__delattr__):
  87. __delattr__(self, '__target__')
  88. @__wrapped__.setter
  89. def __wrapped__(self, target, __setattr__=object.__setattr__):
  90. __setattr__(self, '__target__', target)
  91. @property
  92. def __name__(self):
  93. return self.__wrapped__.__name__
  94. @__name__.setter
  95. def __name__(self, value):
  96. self.__wrapped__.__name__ = value
  97. @property
  98. def __class__(self):
  99. return self.__wrapped__.__class__
  100. @__class__.setter # noqa: F811
  101. def __class__(self, value): # noqa: F811
  102. self.__wrapped__.__class__ = value
  103. @property
  104. def __annotations__(self):
  105. return self.__wrapped__.__anotations__
  106. @__annotations__.setter
  107. def __annotations__(self, value):
  108. self.__wrapped__.__annotations__ = value
  109. def __dir__(self):
  110. return dir(self.__wrapped__)
  111. def __str__(self):
  112. return str(self.__wrapped__)
  113. if PY3:
  114. def __bytes__(self):
  115. return bytes(self.__wrapped__)
  116. def __repr__(self, __getattr__=object.__getattribute__):
  117. try:
  118. target = __getattr__(self, '__target__')
  119. except AttributeError:
  120. return '<{} at 0x{:x} with factory {!r}>'.format(
  121. type(self).__name__, id(self),
  122. self.__factory__
  123. )
  124. else:
  125. return '<{} at 0x{:x} wrapping {!r} at 0x{:x} with factory {!r}>'.format(
  126. type(self).__name__, id(self),
  127. target, id(target),
  128. self.__factory__
  129. )
  130. def __fspath__(self):
  131. wrapped = self.__wrapped__
  132. if isinstance(wrapped, string_types):
  133. return wrapped
  134. else:
  135. fspath = getattr(wrapped, '__fspath__', None)
  136. if fspath is None:
  137. return wrapped
  138. else:
  139. return fspath()
  140. def __reversed__(self):
  141. return reversed(self.__wrapped__)
  142. if PY3:
  143. def __round__(self):
  144. return round(self.__wrapped__)
  145. def __lt__(self, other):
  146. return self.__wrapped__ < other
  147. def __le__(self, other):
  148. return self.__wrapped__ <= other
  149. def __eq__(self, other):
  150. return self.__wrapped__ == other
  151. def __ne__(self, other):
  152. return self.__wrapped__ != other
  153. def __gt__(self, other):
  154. return self.__wrapped__ > other
  155. def __ge__(self, other):
  156. return self.__wrapped__ >= other
  157. def __hash__(self):
  158. return hash(self.__wrapped__)
  159. def __nonzero__(self):
  160. return bool(self.__wrapped__)
  161. def __bool__(self):
  162. return bool(self.__wrapped__)
  163. def __setattr__(self, name, value, __setattr__=object.__setattr__):
  164. if hasattr(type(self), name):
  165. __setattr__(self, name, value)
  166. else:
  167. setattr(self.__wrapped__, name, value)
  168. def __getattr__(self, name):
  169. if name in ('__wrapped__', '__factory__'):
  170. raise AttributeError(name)
  171. else:
  172. return getattr(self.__wrapped__, name)
  173. def __delattr__(self, name, __delattr__=object.__delattr__):
  174. if hasattr(type(self), name):
  175. __delattr__(self, name)
  176. else:
  177. delattr(self.__wrapped__, name)
  178. def __add__(self, other):
  179. return self.__wrapped__ + other
  180. def __sub__(self, other):
  181. return self.__wrapped__ - other
  182. def __mul__(self, other):
  183. return self.__wrapped__ * other
  184. def __div__(self, other):
  185. return operator.div(self.__wrapped__, other)
  186. def __truediv__(self, other):
  187. return operator.truediv(self.__wrapped__, other)
  188. def __floordiv__(self, other):
  189. return self.__wrapped__ // other
  190. def __mod__(self, other):
  191. return self.__wrapped__ % other
  192. def __divmod__(self, other):
  193. return divmod(self.__wrapped__, other)
  194. def __pow__(self, other, *args):
  195. return pow(self.__wrapped__, other, *args)
  196. def __lshift__(self, other):
  197. return self.__wrapped__ << other
  198. def __rshift__(self, other):
  199. return self.__wrapped__ >> other
  200. def __and__(self, other):
  201. return self.__wrapped__ & other
  202. def __xor__(self, other):
  203. return self.__wrapped__ ^ other
  204. def __or__(self, other):
  205. return self.__wrapped__ | other
  206. def __radd__(self, other):
  207. return other + self.__wrapped__
  208. def __rsub__(self, other):
  209. return other - self.__wrapped__
  210. def __rmul__(self, other):
  211. return other * self.__wrapped__
  212. def __rdiv__(self, other):
  213. return operator.div(other, self.__wrapped__)
  214. def __rtruediv__(self, other):
  215. return operator.truediv(other, self.__wrapped__)
  216. def __rfloordiv__(self, other):
  217. return other // self.__wrapped__
  218. def __rmod__(self, other):
  219. return other % self.__wrapped__
  220. def __rdivmod__(self, other):
  221. return divmod(other, self.__wrapped__)
  222. def __rpow__(self, other, *args):
  223. return pow(other, self.__wrapped__, *args)
  224. def __rlshift__(self, other):
  225. return other << self.__wrapped__
  226. def __rrshift__(self, other):
  227. return other >> self.__wrapped__
  228. def __rand__(self, other):
  229. return other & self.__wrapped__
  230. def __rxor__(self, other):
  231. return other ^ self.__wrapped__
  232. def __ror__(self, other):
  233. return other | self.__wrapped__
  234. def __iadd__(self, other):
  235. self.__wrapped__ += other
  236. return self
  237. def __isub__(self, other):
  238. self.__wrapped__ -= other
  239. return self
  240. def __imul__(self, other):
  241. self.__wrapped__ *= other
  242. return self
  243. def __idiv__(self, other):
  244. self.__wrapped__ = operator.idiv(self.__wrapped__, other)
  245. return self
  246. def __itruediv__(self, other):
  247. self.__wrapped__ = operator.itruediv(self.__wrapped__, other)
  248. return self
  249. def __ifloordiv__(self, other):
  250. self.__wrapped__ //= other
  251. return self
  252. def __imod__(self, other):
  253. self.__wrapped__ %= other
  254. return self
  255. def __ipow__(self, other):
  256. self.__wrapped__ **= other
  257. return self
  258. def __ilshift__(self, other):
  259. self.__wrapped__ <<= other
  260. return self
  261. def __irshift__(self, other):
  262. self.__wrapped__ >>= other
  263. return self
  264. def __iand__(self, other):
  265. self.__wrapped__ &= other
  266. return self
  267. def __ixor__(self, other):
  268. self.__wrapped__ ^= other
  269. return self
  270. def __ior__(self, other):
  271. self.__wrapped__ |= other
  272. return self
  273. def __neg__(self):
  274. return -self.__wrapped__
  275. def __pos__(self):
  276. return +self.__wrapped__
  277. def __abs__(self):
  278. return abs(self.__wrapped__)
  279. def __invert__(self):
  280. return ~self.__wrapped__
  281. def __int__(self):
  282. return int(self.__wrapped__)
  283. if PY2:
  284. def __long__(self):
  285. return long(self.__wrapped__) # noqa
  286. def __float__(self):
  287. return float(self.__wrapped__)
  288. def __oct__(self):
  289. return oct(self.__wrapped__)
  290. def __hex__(self):
  291. return hex(self.__wrapped__)
  292. def __index__(self):
  293. if hasattr(self.__wrapped__, '__index__'):
  294. return operator.index(self.__wrapped__)
  295. else:
  296. return int(self.__wrapped__)
  297. def __len__(self):
  298. return len(self.__wrapped__)
  299. def __contains__(self, value):
  300. return value in self.__wrapped__
  301. def __getitem__(self, key):
  302. return self.__wrapped__[key]
  303. def __setitem__(self, key, value):
  304. self.__wrapped__[key] = value
  305. def __delitem__(self, key):
  306. del self.__wrapped__[key]
  307. def __getslice__(self, i, j):
  308. return self.__wrapped__[i:j]
  309. def __setslice__(self, i, j, value):
  310. self.__wrapped__[i:j] = value
  311. def __delslice__(self, i, j):
  312. del self.__wrapped__[i:j]
  313. def __enter__(self):
  314. return self.__wrapped__.__enter__()
  315. def __exit__(self, *args, **kwargs):
  316. return self.__wrapped__.__exit__(*args, **kwargs)
  317. def __iter__(self):
  318. return iter(self.__wrapped__)
  319. def __next__(self):
  320. return next(self.__wrapped__)
  321. def __call__(self, *args, **kwargs):
  322. return self.__wrapped__(*args, **kwargs)
  323. def __reduce__(self):
  324. return identity, (self.__wrapped__,)
  325. def __reduce_ex__(self, protocol):
  326. return identity, (self.__wrapped__,)
  327. if await_:
  328. from .utils import __aenter__
  329. from .utils import __aexit__
  330. from .utils import __aiter__
  331. from .utils import __anext__
  332. from .utils import __await__
  333. __aiter__, __anext__, __await__, __aenter__, __aexit__ # noqa