wrappers.py 35 KB

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