wrappers.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. import os
  2. import sys
  3. import functools
  4. import operator
  5. import weakref
  6. import inspect
  7. PY2 = sys.version_info[0] == 2
  8. if PY2:
  9. string_types = basestring,
  10. else:
  11. string_types = str,
  12. def with_metaclass(meta, *bases):
  13. """Create a base class with a metaclass."""
  14. return meta("NewBase", bases, {})
  15. class _ObjectProxyMethods(object):
  16. # We use properties to override the values of __module__ and
  17. # __doc__. If we add these in ObjectProxy, the derived class
  18. # __dict__ will still be setup to have string variants of these
  19. # attributes and the rules of descriptors means that they appear to
  20. # take precedence over the properties in the base class. To avoid
  21. # that, we copy the properties into the derived class type itself
  22. # via a meta class. In that way the properties will always take
  23. # precedence.
  24. @property
  25. def __module__(self):
  26. return self.__wrapped__.__module__
  27. @__module__.setter
  28. def __module__(self, value):
  29. self.__wrapped__.__module__ = value
  30. @property
  31. def __doc__(self):
  32. return self.__wrapped__.__doc__
  33. @__doc__.setter
  34. def __doc__(self, value):
  35. self.__wrapped__.__doc__ = value
  36. # We similar use a property for __dict__. We need __dict__ to be
  37. # explicit to ensure that vars() works as expected.
  38. @property
  39. def __dict__(self):
  40. return self.__wrapped__.__dict__
  41. # Need to also propagate the special __weakref__ attribute for case
  42. # where decorating classes which will define this. If do not define
  43. # it and use a function like inspect.getmembers() on a decorator
  44. # class it will fail. This can't be in the derived classes.
  45. @property
  46. def __weakref__(self):
  47. return self.__wrapped__.__weakref__
  48. class _ObjectProxyMetaType(type):
  49. def __new__(cls, name, bases, dictionary):
  50. # Copy our special properties into the class so that they
  51. # always take precedence over attributes of the same name added
  52. # during construction of a derived class. This is to save
  53. # duplicating the implementation for them in all derived classes.
  54. dictionary.update(vars(_ObjectProxyMethods))
  55. return type.__new__(cls, name, bases, dictionary)
  56. class ObjectProxy(with_metaclass(_ObjectProxyMetaType)):
  57. __slots__ = '__wrapped__'
  58. def __init__(self, wrapped):
  59. object.__setattr__(self, '__wrapped__', wrapped)
  60. # Python 3.2+ has the __qualname__ attribute, but it does not
  61. # allow it to be overridden using a property and it must instead
  62. # be an actual string object instead.
  63. try:
  64. object.__setattr__(self, '__qualname__', wrapped.__qualname__)
  65. except AttributeError:
  66. pass
  67. # Python 3.10 onwards also does not allow itself to be overridden
  68. # using a property and it must instead be set explicitly.
  69. try:
  70. object.__setattr__(self, '__annotations__', wrapped.__annotations__)
  71. except AttributeError:
  72. pass
  73. @property
  74. def __name__(self):
  75. return self.__wrapped__.__name__
  76. @__name__.setter
  77. def __name__(self, value):
  78. self.__wrapped__.__name__ = value
  79. @property
  80. def __class__(self):
  81. return self.__wrapped__.__class__
  82. @__class__.setter
  83. def __class__(self, value):
  84. self.__wrapped__.__class__ = value
  85. def __dir__(self):
  86. return dir(self.__wrapped__)
  87. def __str__(self):
  88. return str(self.__wrapped__)
  89. if not PY2:
  90. def __bytes__(self):
  91. return bytes(self.__wrapped__)
  92. def __repr__(self):
  93. return '<{} at 0x{:x} for {} at 0x{:x}>'.format(
  94. type(self).__name__, id(self),
  95. type(self.__wrapped__).__name__,
  96. id(self.__wrapped__))
  97. def __reversed__(self):
  98. return reversed(self.__wrapped__)
  99. if not PY2:
  100. def __round__(self):
  101. return round(self.__wrapped__)
  102. if sys.hexversion >= 0x03070000:
  103. def __mro_entries__(self, bases):
  104. return (self.__wrapped__,)
  105. def __lt__(self, other):
  106. return self.__wrapped__ < other
  107. def __le__(self, other):
  108. return self.__wrapped__ <= other
  109. def __eq__(self, other):
  110. return self.__wrapped__ == other
  111. def __ne__(self, other):
  112. return self.__wrapped__ != other
  113. def __gt__(self, other):
  114. return self.__wrapped__ > other
  115. def __ge__(self, other):
  116. return self.__wrapped__ >= other
  117. def __hash__(self):
  118. return hash(self.__wrapped__)
  119. def __nonzero__(self):
  120. return bool(self.__wrapped__)
  121. def __bool__(self):
  122. return bool(self.__wrapped__)
  123. def __setattr__(self, name, value):
  124. if name.startswith('_self_'):
  125. object.__setattr__(self, name, value)
  126. elif name == '__wrapped__':
  127. object.__setattr__(self, name, value)
  128. try:
  129. object.__delattr__(self, '__qualname__')
  130. except AttributeError:
  131. pass
  132. try:
  133. object.__setattr__(self, '__qualname__', value.__qualname__)
  134. except AttributeError:
  135. pass
  136. try:
  137. object.__delattr__(self, '__annotations__')
  138. except AttributeError:
  139. pass
  140. try:
  141. object.__setattr__(self, '__annotations__', value.__annotations__)
  142. except AttributeError:
  143. pass
  144. elif name == '__qualname__':
  145. setattr(self.__wrapped__, name, value)
  146. object.__setattr__(self, name, value)
  147. elif name == '__annotations__':
  148. setattr(self.__wrapped__, name, value)
  149. object.__setattr__(self, name, value)
  150. elif hasattr(type(self), name):
  151. object.__setattr__(self, name, value)
  152. else:
  153. setattr(self.__wrapped__, name, value)
  154. def __getattr__(self, name):
  155. # If we are being to lookup '__wrapped__' then the
  156. # '__init__()' method cannot have been called.
  157. if name == '__wrapped__':
  158. raise ValueError('wrapper has not been initialised')
  159. return getattr(self.__wrapped__, name)
  160. def __delattr__(self, name):
  161. if name.startswith('_self_'):
  162. object.__delattr__(self, name)
  163. elif name == '__wrapped__':
  164. raise TypeError('__wrapped__ must be an object')
  165. elif name == '__qualname__':
  166. object.__delattr__(self, name)
  167. delattr(self.__wrapped__, name)
  168. elif hasattr(type(self), name):
  169. object.__delattr__(self, name)
  170. else:
  171. delattr(self.__wrapped__, name)
  172. def __add__(self, other):
  173. return self.__wrapped__ + other
  174. def __sub__(self, other):
  175. return self.__wrapped__ - other
  176. def __mul__(self, other):
  177. return self.__wrapped__ * other
  178. def __div__(self, other):
  179. return operator.div(self.__wrapped__, other)
  180. def __truediv__(self, other):
  181. return operator.truediv(self.__wrapped__, other)
  182. def __floordiv__(self, other):
  183. return self.__wrapped__ // other
  184. def __mod__(self, other):
  185. return self.__wrapped__ % other
  186. def __divmod__(self, other):
  187. return divmod(self.__wrapped__, other)
  188. def __pow__(self, other, *args):
  189. return pow(self.__wrapped__, other, *args)
  190. def __lshift__(self, other):
  191. return self.__wrapped__ << other
  192. def __rshift__(self, other):
  193. return self.__wrapped__ >> other
  194. def __and__(self, other):
  195. return self.__wrapped__ & other
  196. def __xor__(self, other):
  197. return self.__wrapped__ ^ other
  198. def __or__(self, other):
  199. return self.__wrapped__ | other
  200. def __radd__(self, other):
  201. return other + self.__wrapped__
  202. def __rsub__(self, other):
  203. return other - self.__wrapped__
  204. def __rmul__(self, other):
  205. return other * self.__wrapped__
  206. def __rdiv__(self, other):
  207. return operator.div(other, self.__wrapped__)
  208. def __rtruediv__(self, other):
  209. return operator.truediv(other, self.__wrapped__)
  210. def __rfloordiv__(self, other):
  211. return other // self.__wrapped__
  212. def __rmod__(self, other):
  213. return other % self.__wrapped__
  214. def __rdivmod__(self, other):
  215. return divmod(other, self.__wrapped__)
  216. def __rpow__(self, other, *args):
  217. return pow(other, self.__wrapped__, *args)
  218. def __rlshift__(self, other):
  219. return other << self.__wrapped__
  220. def __rrshift__(self, other):
  221. return other >> self.__wrapped__
  222. def __rand__(self, other):
  223. return other & self.__wrapped__
  224. def __rxor__(self, other):
  225. return other ^ self.__wrapped__
  226. def __ror__(self, other):
  227. return other | self.__wrapped__
  228. def __iadd__(self, other):
  229. self.__wrapped__ += other
  230. return self
  231. def __isub__(self, other):
  232. self.__wrapped__ -= other
  233. return self
  234. def __imul__(self, other):
  235. self.__wrapped__ *= other
  236. return self
  237. def __idiv__(self, other):
  238. self.__wrapped__ = operator.idiv(self.__wrapped__, other)
  239. return self
  240. def __itruediv__(self, other):
  241. self.__wrapped__ = operator.itruediv(self.__wrapped__, other)
  242. return self
  243. def __ifloordiv__(self, other):
  244. self.__wrapped__ //= other
  245. return self
  246. def __imod__(self, other):
  247. self.__wrapped__ %= other
  248. return self
  249. def __ipow__(self, other):
  250. self.__wrapped__ **= other
  251. return self
  252. def __ilshift__(self, other):
  253. self.__wrapped__ <<= other
  254. return self
  255. def __irshift__(self, other):
  256. self.__wrapped__ >>= other
  257. return self
  258. def __iand__(self, other):
  259. self.__wrapped__ &= other
  260. return self
  261. def __ixor__(self, other):
  262. self.__wrapped__ ^= other
  263. return self
  264. def __ior__(self, other):
  265. self.__wrapped__ |= other
  266. return self
  267. def __neg__(self):
  268. return -self.__wrapped__
  269. def __pos__(self):
  270. return +self.__wrapped__
  271. def __abs__(self):
  272. return abs(self.__wrapped__)
  273. def __invert__(self):
  274. return ~self.__wrapped__
  275. def __int__(self):
  276. return int(self.__wrapped__)
  277. def __long__(self):
  278. return long(self.__wrapped__)
  279. def __float__(self):
  280. return float(self.__wrapped__)
  281. def __complex__(self):
  282. return complex(self.__wrapped__)
  283. def __oct__(self):
  284. return oct(self.__wrapped__)
  285. def __hex__(self):
  286. return hex(self.__wrapped__)
  287. def __index__(self):
  288. return operator.index(self.__wrapped__)
  289. def __len__(self):
  290. return len(self.__wrapped__)
  291. def __contains__(self, value):
  292. return value in self.__wrapped__
  293. def __getitem__(self, key):
  294. return self.__wrapped__[key]
  295. def __setitem__(self, key, value):
  296. self.__wrapped__[key] = value
  297. def __delitem__(self, key):
  298. del self.__wrapped__[key]
  299. def __getslice__(self, i, j):
  300. return self.__wrapped__[i:j]
  301. def __setslice__(self, i, j, value):
  302. self.__wrapped__[i:j] = value
  303. def __delslice__(self, i, j):
  304. del self.__wrapped__[i:j]
  305. def __enter__(self):
  306. return self.__wrapped__.__enter__()
  307. def __exit__(self, *args, **kwargs):
  308. return self.__wrapped__.__exit__(*args, **kwargs)
  309. def __iter__(self):
  310. return iter(self.__wrapped__)
  311. def __copy__(self):
  312. raise NotImplementedError('object proxy must define __copy__()')
  313. def __deepcopy__(self, memo):
  314. raise NotImplementedError('object proxy must define __deepcopy__()')
  315. def __reduce__(self):
  316. raise NotImplementedError(
  317. 'object proxy must define __reduce_ex__()')
  318. def __reduce_ex__(self, protocol):
  319. raise NotImplementedError(
  320. 'object proxy must define __reduce_ex__()')
  321. class CallableObjectProxy(ObjectProxy):
  322. def __call__(self, *args, **kwargs):
  323. return self.__wrapped__(*args, **kwargs)
  324. class PartialCallableObjectProxy(ObjectProxy):
  325. def __init__(self, *args, **kwargs):
  326. if len(args) < 1:
  327. raise TypeError('partial type takes at least one argument')
  328. wrapped, args = args[0], args[1:]
  329. if not callable(wrapped):
  330. raise TypeError('the first argument must be callable')
  331. super(PartialCallableObjectProxy, self).__init__(wrapped)
  332. self._self_args = args
  333. self._self_kwargs = kwargs
  334. def __call__(self, *args, **kwargs):
  335. _args = self._self_args + args
  336. _kwargs = dict(self._self_kwargs)
  337. _kwargs.update(kwargs)
  338. return self.__wrapped__(*_args, **_kwargs)
  339. class _FunctionWrapperBase(ObjectProxy):
  340. __slots__ = ('_self_instance', '_self_wrapper', '_self_enabled',
  341. '_self_binding', '_self_parent')
  342. def __init__(self, wrapped, instance, wrapper, enabled=None,
  343. binding='function', parent=None):
  344. super(_FunctionWrapperBase, self).__init__(wrapped)
  345. object.__setattr__(self, '_self_instance', instance)
  346. object.__setattr__(self, '_self_wrapper', wrapper)
  347. object.__setattr__(self, '_self_enabled', enabled)
  348. object.__setattr__(self, '_self_binding', binding)
  349. object.__setattr__(self, '_self_parent', parent)
  350. def __get__(self, instance, owner):
  351. # This method is actually doing double duty for both unbound and
  352. # bound derived wrapper classes. It should possibly be broken up
  353. # and the distinct functionality moved into the derived classes.
  354. # Can't do that straight away due to some legacy code which is
  355. # relying on it being here in this base class.
  356. #
  357. # The distinguishing attribute which determines whether we are
  358. # being called in an unbound or bound wrapper is the parent
  359. # attribute. If binding has never occurred, then the parent will
  360. # be None.
  361. #
  362. # First therefore, is if we are called in an unbound wrapper. In
  363. # this case we perform the binding.
  364. #
  365. # We have one special case to worry about here. This is where we
  366. # are decorating a nested class. In this case the wrapped class
  367. # would not have a __get__() method to call. In that case we
  368. # simply return self.
  369. #
  370. # Note that we otherwise still do binding even if instance is
  371. # None and accessing an unbound instance method from a class.
  372. # This is because we need to be able to later detect that
  373. # specific case as we will need to extract the instance from the
  374. # first argument of those passed in.
  375. if self._self_parent is None:
  376. if not inspect.isclass(self.__wrapped__):
  377. descriptor = self.__wrapped__.__get__(instance, owner)
  378. return self.__bound_function_wrapper__(descriptor, instance,
  379. self._self_wrapper, self._self_enabled,
  380. self._self_binding, self)
  381. return self
  382. # Now we have the case of binding occurring a second time on what
  383. # was already a bound function. In this case we would usually
  384. # return ourselves again. This mirrors what Python does.
  385. #
  386. # The special case this time is where we were originally bound
  387. # with an instance of None and we were likely an instance
  388. # method. In that case we rebind against the original wrapped
  389. # function from the parent again.
  390. if self._self_instance is None and self._self_binding == 'function':
  391. descriptor = self._self_parent.__wrapped__.__get__(
  392. instance, owner)
  393. return self._self_parent.__bound_function_wrapper__(
  394. descriptor, instance, self._self_wrapper,
  395. self._self_enabled, self._self_binding,
  396. self._self_parent)
  397. return self
  398. def __call__(self, *args, **kwargs):
  399. # If enabled has been specified, then evaluate it at this point
  400. # and if the wrapper is not to be executed, then simply return
  401. # the bound function rather than a bound wrapper for the bound
  402. # function. When evaluating enabled, if it is callable we call
  403. # it, otherwise we evaluate it as a boolean.
  404. if self._self_enabled is not None:
  405. if callable(self._self_enabled):
  406. if not self._self_enabled():
  407. return self.__wrapped__(*args, **kwargs)
  408. elif not self._self_enabled:
  409. return self.__wrapped__(*args, **kwargs)
  410. # This can occur where initial function wrapper was applied to
  411. # a function that was already bound to an instance. In that case
  412. # we want to extract the instance from the function and use it.
  413. if self._self_binding in ('function', 'classmethod'):
  414. if self._self_instance is None:
  415. instance = getattr(self.__wrapped__, '__self__', None)
  416. if instance is not None:
  417. return self._self_wrapper(self.__wrapped__, instance,
  418. args, kwargs)
  419. # This is generally invoked when the wrapped function is being
  420. # called as a normal function and is not bound to a class as an
  421. # instance method. This is also invoked in the case where the
  422. # wrapped function was a method, but this wrapper was in turn
  423. # wrapped using the staticmethod decorator.
  424. return self._self_wrapper(self.__wrapped__, self._self_instance,
  425. args, kwargs)
  426. def __set_name__(self, owner, name):
  427. # This is a special method use to supply information to
  428. # descriptors about what the name of variable in a class
  429. # definition is. Not wanting to add this to ObjectProxy as not
  430. # sure of broader implications of doing that. Thus restrict to
  431. # FunctionWrapper used by decorators.
  432. if hasattr(self.__wrapped__, "__set_name__"):
  433. self.__wrapped__.__set_name__(owner, name)
  434. def __instancecheck__(self, instance):
  435. # This is a special method used by isinstance() to make checks
  436. # instance of the `__wrapped__`.
  437. return isinstance(instance, self.__wrapped__)
  438. def __subclasscheck__(self, subclass):
  439. # This is a special method used by issubclass() to make checks
  440. # about inheritance of classes. We need to upwrap any object
  441. # proxy. Not wanting to add this to ObjectProxy as not sure of
  442. # broader implications of doing that. Thus restrict to
  443. # FunctionWrapper used by decorators.
  444. if hasattr(subclass, "__wrapped__"):
  445. return issubclass(subclass.__wrapped__, self.__wrapped__)
  446. else:
  447. return issubclass(subclass, self.__wrapped__)
  448. class BoundFunctionWrapper(_FunctionWrapperBase):
  449. def __call__(self, *args, **kwargs):
  450. # If enabled has been specified, then evaluate it at this point
  451. # and if the wrapper is not to be executed, then simply return
  452. # the bound function rather than a bound wrapper for the bound
  453. # function. When evaluating enabled, if it is callable we call
  454. # it, otherwise we evaluate it as a boolean.
  455. if self._self_enabled is not None:
  456. if callable(self._self_enabled):
  457. if not self._self_enabled():
  458. return self.__wrapped__(*args, **kwargs)
  459. elif not self._self_enabled:
  460. return self.__wrapped__(*args, **kwargs)
  461. # We need to do things different depending on whether we are
  462. # likely wrapping an instance method vs a static method or class
  463. # method.
  464. if self._self_binding == 'function':
  465. if self._self_instance is None:
  466. # This situation can occur where someone is calling the
  467. # instancemethod via the class type and passing the instance
  468. # as the first argument. We need to shift the args before
  469. # making the call to the wrapper and effectively bind the
  470. # instance to the wrapped function using a partial so the
  471. # wrapper doesn't see anything as being different.
  472. if not args:
  473. raise TypeError('missing 1 required positional argument')
  474. instance, args = args[0], args[1:]
  475. wrapped = PartialCallableObjectProxy(self.__wrapped__, instance)
  476. return self._self_wrapper(wrapped, instance, args, kwargs)
  477. return self._self_wrapper(self.__wrapped__, self._self_instance,
  478. args, kwargs)
  479. else:
  480. # As in this case we would be dealing with a classmethod or
  481. # staticmethod, then _self_instance will only tell us whether
  482. # when calling the classmethod or staticmethod they did it via an
  483. # instance of the class it is bound to and not the case where
  484. # done by the class type itself. We thus ignore _self_instance
  485. # and use the __self__ attribute of the bound function instead.
  486. # For a classmethod, this means instance will be the class type
  487. # and for a staticmethod it will be None. This is probably the
  488. # more useful thing we can pass through even though we loose
  489. # knowledge of whether they were called on the instance vs the
  490. # class type, as it reflects what they have available in the
  491. # decoratored function.
  492. instance = getattr(self.__wrapped__, '__self__', None)
  493. return self._self_wrapper(self.__wrapped__, instance, args,
  494. kwargs)
  495. class FunctionWrapper(_FunctionWrapperBase):
  496. __bound_function_wrapper__ = BoundFunctionWrapper
  497. def __init__(self, wrapped, wrapper, enabled=None):
  498. # What it is we are wrapping here could be anything. We need to
  499. # try and detect specific cases though. In particular, we need
  500. # to detect when we are given something that is a method of a
  501. # class. Further, we need to know when it is likely an instance
  502. # method, as opposed to a class or static method. This can
  503. # become problematic though as there isn't strictly a fool proof
  504. # method of knowing.
  505. #
  506. # The situations we could encounter when wrapping a method are:
  507. #
  508. # 1. The wrapper is being applied as part of a decorator which
  509. # is a part of the class definition. In this case what we are
  510. # given is the raw unbound function, classmethod or staticmethod
  511. # wrapper objects.
  512. #
  513. # The problem here is that we will not know we are being applied
  514. # in the context of the class being set up. This becomes
  515. # important later for the case of an instance method, because in
  516. # that case we just see it as a raw function and can't
  517. # distinguish it from wrapping a normal function outside of
  518. # a class context.
  519. #
  520. # 2. The wrapper is being applied when performing monkey
  521. # patching of the class type afterwards and the method to be
  522. # wrapped was retrieved direct from the __dict__ of the class
  523. # type. This is effectively the same as (1) above.
  524. #
  525. # 3. The wrapper is being applied when performing monkey
  526. # patching of the class type afterwards and the method to be
  527. # wrapped was retrieved from the class type. In this case
  528. # binding will have been performed where the instance against
  529. # which the method is bound will be None at that point.
  530. #
  531. # This case is a problem because we can no longer tell if the
  532. # method was a static method, plus if using Python3, we cannot
  533. # tell if it was an instance method as the concept of an
  534. # unnbound method no longer exists.
  535. #
  536. # 4. The wrapper is being applied when performing monkey
  537. # patching of an instance of a class. In this case binding will
  538. # have been perfomed where the instance was not None.
  539. #
  540. # This case is a problem because we can no longer tell if the
  541. # method was a static method.
  542. #
  543. # Overall, the best we can do is look at the original type of the
  544. # object which was wrapped prior to any binding being done and
  545. # see if it is an instance of classmethod or staticmethod. In
  546. # the case where other decorators are between us and them, if
  547. # they do not propagate the __class__ attribute so that the
  548. # isinstance() checks works, then likely this will do the wrong
  549. # thing where classmethod and staticmethod are used.
  550. #
  551. # Since it is likely to be very rare that anyone even puts
  552. # decorators around classmethod and staticmethod, likelihood of
  553. # that being an issue is very small, so we accept it and suggest
  554. # that those other decorators be fixed. It is also only an issue
  555. # if a decorator wants to actually do things with the arguments.
  556. #
  557. # As to not being able to identify static methods properly, we
  558. # just hope that that isn't something people are going to want
  559. # to wrap, or if they do suggest they do it the correct way by
  560. # ensuring that it is decorated in the class definition itself,
  561. # or patch it in the __dict__ of the class type.
  562. #
  563. # So to get the best outcome we can, whenever we aren't sure what
  564. # it is, we label it as a 'function'. If it was already bound and
  565. # that is rebound later, we assume that it will be an instance
  566. # method and try an cope with the possibility that the 'self'
  567. # argument it being passed as an explicit argument and shuffle
  568. # the arguments around to extract 'self' for use as the instance.
  569. if isinstance(wrapped, classmethod):
  570. binding = 'classmethod'
  571. elif isinstance(wrapped, staticmethod):
  572. binding = 'staticmethod'
  573. elif hasattr(wrapped, '__self__'):
  574. if inspect.isclass(wrapped.__self__):
  575. binding = 'classmethod'
  576. else:
  577. binding = 'function'
  578. else:
  579. binding = 'function'
  580. super(FunctionWrapper, self).__init__(wrapped, None, wrapper,
  581. enabled, binding)
  582. try:
  583. if not os.environ.get('WRAPT_DISABLE_EXTENSIONS'):
  584. from ._wrappers import (ObjectProxy, CallableObjectProxy,
  585. PartialCallableObjectProxy, FunctionWrapper,
  586. BoundFunctionWrapper, _FunctionWrapperBase)
  587. except ImportError:
  588. print("Not find the file (_wrappers.cpython-310-x86_64-linux-gnu.so). You need find it in https://colab.research.google.com/github/deepmind/graphcast/blob/master/graphcast_demo.ipynb, or you can rewrite _wrappers.py by our way. Added by 20231229 S.F. Sune, https://github.com/sfsun67")
  589. pass
  590. # Helper functions for applying wrappers to existing functions.
  591. def resolve_path(module, name):
  592. if isinstance(module, string_types):
  593. __import__(module)
  594. module = sys.modules[module]
  595. parent = module
  596. path = name.split('.')
  597. attribute = path[0]
  598. # We can't just always use getattr() because in doing
  599. # that on a class it will cause binding to occur which
  600. # will complicate things later and cause some things not
  601. # to work. For the case of a class we therefore access
  602. # the __dict__ directly. To cope though with the wrong
  603. # class being given to us, or a method being moved into
  604. # a base class, we need to walk the class hierarchy to
  605. # work out exactly which __dict__ the method was defined
  606. # in, as accessing it from __dict__ will fail if it was
  607. # not actually on the class given. Fallback to using
  608. # getattr() if we can't find it. If it truly doesn't
  609. # exist, then that will fail.
  610. def lookup_attribute(parent, attribute):
  611. if inspect.isclass(parent):
  612. for cls in inspect.getmro(parent):
  613. if attribute in vars(cls):
  614. return vars(cls)[attribute]
  615. else:
  616. return getattr(parent, attribute)
  617. else:
  618. return getattr(parent, attribute)
  619. original = lookup_attribute(parent, attribute)
  620. for attribute in path[1:]:
  621. parent = original
  622. original = lookup_attribute(parent, attribute)
  623. return (parent, attribute, original)
  624. def apply_patch(parent, attribute, replacement):
  625. setattr(parent, attribute, replacement)
  626. def wrap_object(module, name, factory, args=(), kwargs={}):
  627. (parent, attribute, original) = resolve_path(module, name)
  628. wrapper = factory(original, *args, **kwargs)
  629. apply_patch(parent, attribute, wrapper)
  630. return wrapper
  631. # Function for applying a proxy object to an attribute of a class
  632. # instance. The wrapper works by defining an attribute of the same name
  633. # on the class which is a descriptor and which intercepts access to the
  634. # instance attribute. Note that this cannot be used on attributes which
  635. # are themselves defined by a property object.
  636. class AttributeWrapper(object):
  637. def __init__(self, attribute, factory, args, kwargs):
  638. self.attribute = attribute
  639. self.factory = factory
  640. self.args = args
  641. self.kwargs = kwargs
  642. def __get__(self, instance, owner):
  643. value = instance.__dict__[self.attribute]
  644. return self.factory(value, *self.args, **self.kwargs)
  645. def __set__(self, instance, value):
  646. instance.__dict__[self.attribute] = value
  647. def __delete__(self, instance):
  648. del instance.__dict__[self.attribute]
  649. def wrap_object_attribute(module, name, factory, args=(), kwargs={}):
  650. path, attribute = name.rsplit('.', 1)
  651. parent = resolve_path(module, path)[2]
  652. wrapper = AttributeWrapper(attribute, factory, args, kwargs)
  653. apply_patch(parent, attribute, wrapper)
  654. return wrapper
  655. # Functions for creating a simple decorator using a FunctionWrapper,
  656. # plus short cut functions for applying wrappers to functions. These are
  657. # for use when doing monkey patching. For a more featured way of
  658. # creating decorators see the decorator decorator instead.
  659. def function_wrapper(wrapper):
  660. def _wrapper(wrapped, instance, args, kwargs):
  661. target_wrapped = args[0]
  662. if instance is None:
  663. target_wrapper = wrapper
  664. elif inspect.isclass(instance):
  665. target_wrapper = wrapper.__get__(None, instance)
  666. else:
  667. target_wrapper = wrapper.__get__(instance, type(instance))
  668. return FunctionWrapper(target_wrapped, target_wrapper)
  669. return FunctionWrapper(wrapper, _wrapper)
  670. def wrap_function_wrapper(module, name, wrapper):
  671. return wrap_object(module, name, FunctionWrapper, (wrapper,))
  672. def patch_function_wrapper(module, name):
  673. def _wrapper(wrapper):
  674. return wrap_object(module, name, FunctionWrapper, (wrapper,))
  675. return _wrapper
  676. def transient_function_wrapper(module, name):
  677. def _decorator(wrapper):
  678. def _wrapper(wrapped, instance, args, kwargs):
  679. target_wrapped = args[0]
  680. if instance is None:
  681. target_wrapper = wrapper
  682. elif inspect.isclass(instance):
  683. target_wrapper = wrapper.__get__(None, instance)
  684. else:
  685. target_wrapper = wrapper.__get__(instance, type(instance))
  686. def _execute(wrapped, instance, args, kwargs):
  687. (parent, attribute, original) = resolve_path(module, name)
  688. replacement = FunctionWrapper(original, target_wrapper)
  689. setattr(parent, attribute, replacement)
  690. try:
  691. return wrapped(*args, **kwargs)
  692. finally:
  693. setattr(parent, attribute, original)
  694. return FunctionWrapper(target_wrapped, _execute)
  695. return FunctionWrapper(wrapper, _wrapper)
  696. return _decorator
  697. # A weak function proxy. This will work on instance methods, class
  698. # methods, static methods and regular functions. Special treatment is
  699. # needed for the method types because the bound method is effectively a
  700. # transient object and applying a weak reference to one will immediately
  701. # result in it being destroyed and the weakref callback called. The weak
  702. # reference is therefore applied to the instance the method is bound to
  703. # and the original function. The function is then rebound at the point
  704. # of a call via the weak function proxy.
  705. def _weak_function_proxy_callback(ref, proxy, callback):
  706. if proxy._self_expired:
  707. return
  708. proxy._self_expired = True
  709. # This could raise an exception. We let it propagate back and let
  710. # the weakref.proxy() deal with it, at which point it generally
  711. # prints out a short error message direct to stderr and keeps going.
  712. if callback is not None:
  713. callback(proxy)
  714. class WeakFunctionProxy(ObjectProxy):
  715. __slots__ = ('_self_expired', '_self_instance')
  716. def __init__(self, wrapped, callback=None):
  717. # We need to determine if the wrapped function is actually a
  718. # bound method. In the case of a bound method, we need to keep a
  719. # reference to the original unbound function and the instance.
  720. # This is necessary because if we hold a reference to the bound
  721. # function, it will be the only reference and given it is a
  722. # temporary object, it will almost immediately expire and
  723. # the weakref callback triggered. So what is done is that we
  724. # hold a reference to the instance and unbound function and
  725. # when called bind the function to the instance once again and
  726. # then call it. Note that we avoid using a nested function for
  727. # the callback here so as not to cause any odd reference cycles.
  728. _callback = callback and functools.partial(
  729. _weak_function_proxy_callback, proxy=self,
  730. callback=callback)
  731. self._self_expired = False
  732. if isinstance(wrapped, _FunctionWrapperBase):
  733. self._self_instance = weakref.ref(wrapped._self_instance,
  734. _callback)
  735. if wrapped._self_parent is not None:
  736. super(WeakFunctionProxy, self).__init__(
  737. weakref.proxy(wrapped._self_parent, _callback))
  738. else:
  739. super(WeakFunctionProxy, self).__init__(
  740. weakref.proxy(wrapped, _callback))
  741. return
  742. try:
  743. self._self_instance = weakref.ref(wrapped.__self__, _callback)
  744. super(WeakFunctionProxy, self).__init__(
  745. weakref.proxy(wrapped.__func__, _callback))
  746. except AttributeError:
  747. self._self_instance = None
  748. super(WeakFunctionProxy, self).__init__(
  749. weakref.proxy(wrapped, _callback))
  750. def __call__(self, *args, **kwargs):
  751. # We perform a boolean check here on the instance and wrapped
  752. # function as that will trigger the reference error prior to
  753. # calling if the reference had expired.
  754. instance = self._self_instance and self._self_instance()
  755. function = self.__wrapped__ and self.__wrapped__
  756. # If the wrapped function was originally a bound function, for
  757. # which we retained a reference to the instance and the unbound
  758. # function we need to rebind the function and then call it. If
  759. # not just called the wrapped function.
  760. if instance is None:
  761. return self.__wrapped__(*args, **kwargs)
  762. return function.__get__(instance, type(instance))(*args, **kwargs)