slots.py 12 KB

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