wrappers.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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 properly 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 __subclasscheck__(self, subclass):
  435. # This is a special method used by issubclass() to make checks
  436. # about inheritance of classes. We need to upwrap any object
  437. # proxy. Not wanting to add this to ObjectProxy as not sure of
  438. # broader implications of doing that. Thus restrict to
  439. # FunctionWrapper used by decorators.
  440. if hasattr(subclass, "__wrapped__"):
  441. return issubclass(subclass.__wrapped__, self.__wrapped__)
  442. else:
  443. return issubclass(subclass, self.__wrapped__)
  444. class BoundFunctionWrapper(_FunctionWrapperBase):
  445. def __call__(self, *args, **kwargs):
  446. # If enabled has been specified, then evaluate it at this point
  447. # and if the wrapper is not to be executed, then simply return
  448. # the bound function rather than a bound wrapper for the bound
  449. # function. When evaluating enabled, if it is callable we call
  450. # it, otherwise we evaluate it as a boolean.
  451. if self._self_enabled is not None:
  452. if callable(self._self_enabled):
  453. if not self._self_enabled():
  454. return self.__wrapped__(*args, **kwargs)
  455. elif not self._self_enabled:
  456. return self.__wrapped__(*args, **kwargs)
  457. # We need to do things different depending on whether we are
  458. # likely wrapping an instance method vs a static method or class
  459. # method.
  460. if self._self_binding == 'function':
  461. if self._self_instance is None:
  462. # This situation can occur where someone is calling the
  463. # instancemethod via the class type and passing the instance
  464. # as the first argument. We need to shift the args before
  465. # making the call to the wrapper and effectively bind the
  466. # instance to the wrapped function using a partial so the
  467. # wrapper doesn't see anything as being different.
  468. if not args:
  469. raise TypeError('missing 1 required positional argument')
  470. instance, args = args[0], args[1:]
  471. wrapped = PartialCallableObjectProxy(self.__wrapped__, instance)
  472. return self._self_wrapper(wrapped, instance, args, kwargs)
  473. return self._self_wrapper(self.__wrapped__, self._self_instance,
  474. args, kwargs)
  475. else:
  476. # As in this case we would be dealing with a classmethod or
  477. # staticmethod, then _self_instance will only tell us whether
  478. # when calling the classmethod or staticmethod they did it via an
  479. # instance of the class it is bound to and not the case where
  480. # done by the class type itself. We thus ignore _self_instance
  481. # and use the __self__ attribute of the bound function instead.
  482. # For a classmethod, this means instance will be the class type
  483. # and for a staticmethod it will be None. This is probably the
  484. # more useful thing we can pass through even though we loose
  485. # knowledge of whether they were called on the instance vs the
  486. # class type, as it reflects what they have available in the
  487. # decoratored function.
  488. instance = getattr(self.__wrapped__, '__self__', None)
  489. return self._self_wrapper(self.__wrapped__, instance, args,
  490. kwargs)
  491. class FunctionWrapper(_FunctionWrapperBase):
  492. __bound_function_wrapper__ = BoundFunctionWrapper
  493. def __init__(self, wrapped, wrapper, enabled=None):
  494. # What it is we are wrapping here could be anything. We need to
  495. # try and detect specific cases though. In particular, we need
  496. # to detect when we are given something that is a method of a
  497. # class. Further, we need to know when it is likely an instance
  498. # method, as opposed to a class or static method. This can
  499. # become problematic though as there isn't strictly a fool proof
  500. # method of knowing.
  501. #
  502. # The situations we could encounter when wrapping a method are:
  503. #
  504. # 1. The wrapper is being applied as part of a decorator which
  505. # is a part of the class definition. In this case what we are
  506. # given is the raw unbound function, classmethod or staticmethod
  507. # wrapper objects.
  508. #
  509. # The problem here is that we will not know we are being applied
  510. # in the context of the class being set up. This becomes
  511. # important later for the case of an instance method, because in
  512. # that case we just see it as a raw function and can't
  513. # distinguish it from wrapping a normal function outside of
  514. # a class context.
  515. #
  516. # 2. The wrapper is being applied when performing monkey
  517. # patching of the class type afterwards and the method to be
  518. # wrapped was retrieved direct from the __dict__ of the class
  519. # type. This is effectively the same as (1) above.
  520. #
  521. # 3. The wrapper is being applied when performing monkey
  522. # patching of the class type afterwards and the method to be
  523. # wrapped was retrieved from the class type. In this case
  524. # binding will have been performed where the instance against
  525. # which the method is bound will be None at that point.
  526. #
  527. # This case is a problem because we can no longer tell if the
  528. # method was a static method, plus if using Python3, we cannot
  529. # tell if it was an instance method as the concept of an
  530. # unnbound method no longer exists.
  531. #
  532. # 4. The wrapper is being applied when performing monkey
  533. # patching of an instance of a class. In this case binding will
  534. # have been perfomed where the instance was not None.
  535. #
  536. # This case is a problem because we can no longer tell if the
  537. # method was a static method.
  538. #
  539. # Overall, the best we can do is look at the original type of the
  540. # object which was wrapped prior to any binding being done and
  541. # see if it is an instance of classmethod or staticmethod. In
  542. # the case where other decorators are between us and them, if
  543. # they do not propagate the __class__ attribute so that the
  544. # isinstance() checks works, then likely this will do the wrong
  545. # thing where classmethod and staticmethod are used.
  546. #
  547. # Since it is likely to be very rare that anyone even puts
  548. # decorators around classmethod and staticmethod, likelihood of
  549. # that being an issue is very small, so we accept it and suggest
  550. # that those other decorators be fixed. It is also only an issue
  551. # if a decorator wants to actually do things with the arguments.
  552. #
  553. # As to not being able to identify static methods properly, we
  554. # just hope that that isn't something people are going to want
  555. # to wrap, or if they do suggest they do it the correct way by
  556. # ensuring that it is decorated in the class definition itself,
  557. # or patch it in the __dict__ of the class type.
  558. #
  559. # So to get the best outcome we can, whenever we aren't sure what
  560. # it is, we label it as a 'function'. If it was already bound and
  561. # that is rebound later, we assume that it will be an instance
  562. # method and try an cope with the possibility that the 'self'
  563. # argument it being passed as an explicit argument and shuffle
  564. # the arguments around to extract 'self' for use as the instance.
  565. if isinstance(wrapped, classmethod):
  566. binding = 'classmethod'
  567. elif isinstance(wrapped, staticmethod):
  568. binding = 'staticmethod'
  569. elif hasattr(wrapped, '__self__'):
  570. if inspect.isclass(wrapped.__self__):
  571. binding = 'classmethod'
  572. else:
  573. binding = 'function'
  574. else:
  575. binding = 'function'
  576. super(FunctionWrapper, self).__init__(wrapped, None, wrapper,
  577. enabled, binding)
  578. try:
  579. if not os.environ.get('WRAPT_DISABLE_EXTENSIONS'):
  580. from ._wrappers import (ObjectProxy, CallableObjectProxy,
  581. PartialCallableObjectProxy, FunctionWrapper,
  582. BoundFunctionWrapper, _FunctionWrapperBase)
  583. except ImportError:
  584. pass
  585. # Helper functions for applying wrappers to existing functions.
  586. def resolve_path(module, name):
  587. if isinstance(module, string_types):
  588. __import__(module)
  589. module = sys.modules[module]
  590. parent = module
  591. path = name.split('.')
  592. attribute = path[0]
  593. # We can't just always use getattr() because in doing
  594. # that on a class it will cause binding to occur which
  595. # will complicate things later and cause some things not
  596. # to work. For the case of a class we therefore access
  597. # the __dict__ directly. To cope though with the wrong
  598. # class being given to us, or a method being moved into
  599. # a base class, we need to walk the class hierarchy to
  600. # work out exactly which __dict__ the method was defined
  601. # in, as accessing it from __dict__ will fail if it was
  602. # not actually on the class given. Fallback to using
  603. # getattr() if we can't find it. If it truly doesn't
  604. # exist, then that will fail.
  605. def lookup_attribute(parent, attribute):
  606. if inspect.isclass(parent):
  607. for cls in inspect.getmro(parent):
  608. if attribute in vars(cls):
  609. return vars(cls)[attribute]
  610. else:
  611. return getattr(parent, attribute)
  612. else:
  613. return getattr(parent, attribute)
  614. original = lookup_attribute(parent, attribute)
  615. for attribute in path[1:]:
  616. parent = original
  617. original = lookup_attribute(parent, attribute)
  618. return (parent, attribute, original)
  619. def apply_patch(parent, attribute, replacement):
  620. setattr(parent, attribute, replacement)
  621. def wrap_object(module, name, factory, args=(), kwargs={}):
  622. (parent, attribute, original) = resolve_path(module, name)
  623. wrapper = factory(original, *args, **kwargs)
  624. apply_patch(parent, attribute, wrapper)
  625. return wrapper
  626. # Function for applying a proxy object to an attribute of a class
  627. # instance. The wrapper works by defining an attribute of the same name
  628. # on the class which is a descriptor and which intercepts access to the
  629. # instance attribute. Note that this cannot be used on attributes which
  630. # are themselves defined by a property object.
  631. class AttributeWrapper(object):
  632. def __init__(self, attribute, factory, args, kwargs):
  633. self.attribute = attribute
  634. self.factory = factory
  635. self.args = args
  636. self.kwargs = kwargs
  637. def __get__(self, instance, owner):
  638. value = instance.__dict__[self.attribute]
  639. return self.factory(value, *self.args, **self.kwargs)
  640. def __set__(self, instance, value):
  641. instance.__dict__[self.attribute] = value
  642. def __delete__(self, instance):
  643. del instance.__dict__[self.attribute]
  644. def wrap_object_attribute(module, name, factory, args=(), kwargs={}):
  645. path, attribute = name.rsplit('.', 1)
  646. parent = resolve_path(module, path)[2]
  647. wrapper = AttributeWrapper(attribute, factory, args, kwargs)
  648. apply_patch(parent, attribute, wrapper)
  649. return wrapper
  650. # Functions for creating a simple decorator using a FunctionWrapper,
  651. # plus short cut functions for applying wrappers to functions. These are
  652. # for use when doing monkey patching. For a more featured way of
  653. # creating decorators see the decorator decorator instead.
  654. def function_wrapper(wrapper):
  655. def _wrapper(wrapped, instance, args, kwargs):
  656. target_wrapped = args[0]
  657. if instance is None:
  658. target_wrapper = wrapper
  659. elif inspect.isclass(instance):
  660. target_wrapper = wrapper.__get__(None, instance)
  661. else:
  662. target_wrapper = wrapper.__get__(instance, type(instance))
  663. return FunctionWrapper(target_wrapped, target_wrapper)
  664. return FunctionWrapper(wrapper, _wrapper)
  665. def wrap_function_wrapper(module, name, wrapper):
  666. return wrap_object(module, name, FunctionWrapper, (wrapper,))
  667. def patch_function_wrapper(module, name):
  668. def _wrapper(wrapper):
  669. return wrap_object(module, name, FunctionWrapper, (wrapper,))
  670. return _wrapper
  671. def transient_function_wrapper(module, name):
  672. def _decorator(wrapper):
  673. def _wrapper(wrapped, instance, args, kwargs):
  674. target_wrapped = args[0]
  675. if instance is None:
  676. target_wrapper = wrapper
  677. elif inspect.isclass(instance):
  678. target_wrapper = wrapper.__get__(None, instance)
  679. else:
  680. target_wrapper = wrapper.__get__(instance, type(instance))
  681. def _execute(wrapped, instance, args, kwargs):
  682. (parent, attribute, original) = resolve_path(module, name)
  683. replacement = FunctionWrapper(original, target_wrapper)
  684. setattr(parent, attribute, replacement)
  685. try:
  686. return wrapped(*args, **kwargs)
  687. finally:
  688. setattr(parent, attribute, original)
  689. return FunctionWrapper(target_wrapped, _execute)
  690. return FunctionWrapper(wrapper, _wrapper)
  691. return _decorator
  692. # A weak function proxy. This will work on instance methods, class
  693. # methods, static methods and regular functions. Special treatment is
  694. # needed for the method types because the bound method is effectively a
  695. # transient object and applying a weak reference to one will immediately
  696. # result in it being destroyed and the weakref callback called. The weak
  697. # reference is therefore applied to the instance the method is bound to
  698. # and the original function. The function is then rebound at the point
  699. # of a call via the weak function proxy.
  700. def _weak_function_proxy_callback(ref, proxy, callback):
  701. if proxy._self_expired:
  702. return
  703. proxy._self_expired = True
  704. # This could raise an exception. We let it propagate back and let
  705. # the weakref.proxy() deal with it, at which point it generally
  706. # prints out a short error message direct to stderr and keeps going.
  707. if callback is not None:
  708. callback(proxy)
  709. class WeakFunctionProxy(ObjectProxy):
  710. __slots__ = ('_self_expired', '_self_instance')
  711. def __init__(self, wrapped, callback=None):
  712. # We need to determine if the wrapped function is actually a
  713. # bound method. In the case of a bound method, we need to keep a
  714. # reference to the original unbound function and the instance.
  715. # This is necessary because if we hold a reference to the bound
  716. # function, it will be the only reference and given it is a
  717. # temporary object, it will almost immediately expire and
  718. # the weakref callback triggered. So what is done is that we
  719. # hold a reference to the instance and unbound function and
  720. # when called bind the function to the instance once again and
  721. # then call it. Note that we avoid using a nested function for
  722. # the callback here so as not to cause any odd reference cycles.
  723. _callback = callback and functools.partial(
  724. _weak_function_proxy_callback, proxy=self,
  725. callback=callback)
  726. self._self_expired = False
  727. if isinstance(wrapped, _FunctionWrapperBase):
  728. self._self_instance = weakref.ref(wrapped._self_instance,
  729. _callback)
  730. if wrapped._self_parent is not None:
  731. super(WeakFunctionProxy, self).__init__(
  732. weakref.proxy(wrapped._self_parent, _callback))
  733. else:
  734. super(WeakFunctionProxy, self).__init__(
  735. weakref.proxy(wrapped, _callback))
  736. return
  737. try:
  738. self._self_instance = weakref.ref(wrapped.__self__, _callback)
  739. super(WeakFunctionProxy, self).__init__(
  740. weakref.proxy(wrapped.__func__, _callback))
  741. except AttributeError:
  742. self._self_instance = None
  743. super(WeakFunctionProxy, self).__init__(
  744. weakref.proxy(wrapped, _callback))
  745. def __call__(self, *args, **kwargs):
  746. # We perform a boolean check here on the instance and wrapped
  747. # function as that will trigger the reference error prior to
  748. # calling if the reference had expired.
  749. instance = self._self_instance and self._self_instance()
  750. function = self.__wrapped__ and self.__wrapped__
  751. # If the wrapped function was originally a bound function, for
  752. # which we retained a reference to the instance and the unbound
  753. # function we need to rebind the function and then call it. If
  754. # not just called the wrapped function.
  755. if instance is None:
  756. return self.__wrapped__(*args, **kwargs)
  757. return function.__get__(instance, type(instance))(*args, **kwargs)