Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wrappers.py 32KB

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