documentation.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. from __future__ import print_function
  2. import sys
  3. import threading
  4. import time
  5. import functools
  6. import itertools
  7. import collections
  8. try:
  9. import collections.abc as c
  10. except ImportError:
  11. c = collections
  12. collections.abc = collections
  13. from decorator import (decorator, decorate, FunctionMaker, contextmanager,
  14. dispatch_on, __version__)
  15. doc = r"""Decorators for Humans
  16. ----------------------------------
  17. |Author | Michele Simionato|
  18. |---|---|
  19. |E-mail | michele.simionato@gmail.com|
  20. |Version| $VERSION ($DATE)|
  21. |Supports| Python 2.6, 2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8|
  22. |Download page| http://pypi.python.org/pypi/decorator/$VERSION|
  23. |Installation| ``pip install decorator``|
  24. |License | BSD license|
  25. Introduction
  26. -----------------------------------------
  27. The ``decorator`` module is over ten years old, but still alive and
  28. kicking. It is used by several frameworks (IPython, scipy, authkit,
  29. pylons, pycuda, sugar, ...) and has been stable for a *long*
  30. time. It is your best option if you want to preserve the signature of
  31. decorated functions in a consistent way across Python
  32. releases. Version 4 is fully compatible with the past, except for
  33. one thing: support for Python 2.4 and 2.5 has been dropped. That
  34. decision made it possible to use a single code base both for Python
  35. 2.X and Python 3.X. This is a *huge* bonus, since I could remove over
  36. 2,000 lines of duplicated documentation/doctests. Having to maintain
  37. separate docs for Python 2 and Python 3 effectively stopped any
  38. development on the module for several years. Moreover, it is now
  39. trivial to distribute the module as an universal
  40. [wheel](http://pythonwheels.com) since 2to3 is no more
  41. required. Since Python 2.5 has been released ages ago (in 2006), I felt that
  42. it was reasonable to drop the support for it. If you need to support
  43. ancient versions of Python, stick with the decorator module version
  44. 3.4.2. The current version supports all Python releases from 2.6 up.
  45. What's New in version 4
  46. -----------------------
  47. - **New documentation**
  48. There is now a single manual for all Python versions, so I took the
  49. opportunity to overhaul the documentation and to move it to readthedocs.org.
  50. Even if you are a long-time user, you may want to revisit the docs, since
  51. several examples have been improved.
  52. - **Packaging improvements**
  53. The code is now also available in wheel format. Integration with
  54. setuptools has improved and you can run the tests with the command
  55. ``python setup.py test`` too.
  56. - **Code changes**
  57. A new utility function ``decorate(func, caller)`` has been added.
  58. It does the same job that was performed by the older
  59. ``decorator(caller, func)``. The old functionality is now deprecated
  60. and no longer documented, but still available for now.
  61. - **Multiple dispatch**
  62. The decorator module now includes an implementation of generic
  63. functions (sometimes called "multiple dispatch functions").
  64. The API is designed to mimic ``functools.singledispatch`` (added
  65. in Python 3.4), but the implementation is much simpler.
  66. Moreover, all decorators involved preserve the signature of the
  67. decorated functions. For now, this exists mostly to demonstrate
  68. the power of the module. In the future it could be enhanced/optimized.
  69. In any case, it is very short and compact (less then 100 lines), so you
  70. can extract it for your own use. Take it as food for thought.
  71. - **Python 3.5 coroutines**
  72. From version 4.1 it is possible to decorate coroutines, i.e. functions
  73. defined with the `async def` syntax, and to maintain the
  74. `inspect.iscoroutinefunction` check working for the decorated function.
  75. - **Decorator factories**
  76. From version 4.2 there is facility to define factories of decorators in
  77. a simple way, a feature requested by the users since a long time.
  78. Usefulness of decorators
  79. ------------------------------------------------
  80. Python decorators are an interesting example of why syntactic sugar
  81. matters. In principle, their introduction in Python 2.4 changed
  82. nothing, since they did not provide any new functionality which was not
  83. already present in the language. In practice, their introduction has
  84. significantly changed the way we structure our programs in Python. I
  85. believe the change is for the best, and that decorators are a great
  86. idea since:
  87. * decorators help reducing boilerplate code;
  88. * decorators help separation of concerns;
  89. * decorators enhance readability and maintenability;
  90. * decorators are explicit.
  91. Still, as of now, writing custom decorators correctly requires
  92. some experience and it is not as easy as it could be. For instance,
  93. typical implementations of decorators involve nested functions, and
  94. we all know that flat is better than nested.
  95. The aim of the ``decorator`` module it to simplify the usage of
  96. decorators for the average programmer, and to popularize decorators by
  97. showing various non-trivial examples. Of course, as all techniques,
  98. decorators can be abused (I have seen that) and you should not try to
  99. solve every problem with a decorator, just because you can.
  100. You may find the source code for all the examples
  101. discussed here in the ``documentation.py`` file, which contains
  102. the documentation you are reading in the form of doctests.
  103. Definitions
  104. ------------------------------------
  105. Technically speaking, any Python object which can be called with one argument
  106. can be used as a decorator. However, this definition is somewhat too large
  107. to be really useful. It is more convenient to split the generic class of
  108. decorators in two subclasses:
  109. 1. **signature-preserving decorators**, callable objects which accept
  110. a function as input and return a function as output, *with the
  111. same signature*
  112. 2. **signature-changing** decorators, i.e. decorators
  113. which change the signature of their input function, or decorators
  114. that return non-callable objects
  115. Signature-changing decorators have their use: for instance, the
  116. builtin classes ``staticmethod`` and ``classmethod`` are in this
  117. group. They take functions and return descriptor objects which
  118. are neither functions, nor callables.
  119. Still, signature-preserving decorators are more common, and easier
  120. to reason about. In particular, they can be composed together,
  121. whereas other decorators generally cannot.
  122. Writing signature-preserving decorators from scratch is not that
  123. obvious, especially if one wants to define proper decorators that
  124. can accept functions with any signature. A simple example will clarify
  125. the issue.
  126. Statement of the problem
  127. ------------------------------
  128. A very common use case for decorators is the memoization of functions.
  129. A ``memoize`` decorator works by caching
  130. the result of the function call in a dictionary, so that the next time
  131. the function is called with the same input parameters the result is retrieved
  132. from the cache and not recomputed.
  133. There are many implementations of ``memoize`` in
  134. http://www.python.org/moin/PythonDecoratorLibrary,
  135. but they do not preserve the signature. In recent versions of
  136. Python you can find a sophisticated ``lru_cache`` decorator
  137. in the standard library's ``functools``. Here I am just
  138. interested in giving an example.
  139. Consider the following simple implementation (note that it is
  140. generally impossible to *correctly* memoize something
  141. that depends on non-hashable arguments):
  142. $$memoize_uw
  143. Here I used the functools.update_wrapper_ utility, which was added
  144. in Python 2.5 to simplify the writing of decorators.
  145. (Previously, you needed to manually copy the function attributes
  146. ``__name__``, ``__doc__``, ``__module__``, and ``__dict__``
  147. to the decorated function by hand).
  148. Here is an example of usage:
  149. $$f1
  150. This works insofar as the decorator accepts functions with generic signatures.
  151. Unfortunately, it is *not* a signature-preserving decorator, since
  152. ``memoize_uw`` generally returns a function with a *different signature*
  153. from the original.
  154. Consider for instance the following case:
  155. $$f1
  156. Here, the original function takes a single argument named ``x``,
  157. but the decorated function takes any number of arguments and
  158. keyword arguments:
  159. ```python
  160. >>> from decorator import getfullargspec
  161. >>> print(getfullargspec(f1))
  162. FullArgSpec(args=[], varargs='args', varkw='kw', defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
  163. ```
  164. This means that introspection tools (like ``pydoc``) will give false
  165. information about the signature of ``f1`` -- unless you are using
  166. Python 3.5. This is pretty bad: ``pydoc`` will tell you that the
  167. function accepts the generic signature ``*args, **kw``, but
  168. calling the function with more than one argument raises an error:
  169. ```python
  170. >>> f1(0, 1) # doctest: +IGNORE_EXCEPTION_DETAIL
  171. Traceback (most recent call last):
  172. ...
  173. TypeError: f1() takes exactly 1 positional argument (2 given)
  174. ```
  175. Notice that ``inspect.getfullargspec``
  176. will give the wrong signature, even in the latest Python, i.e. version 3.6
  177. at the time of writing.
  178. The solution
  179. -----------------------------------------
  180. The solution is to provide a generic factory of generators, which
  181. hides the complexity of making signature-preserving decorators
  182. from the application programmer. The ``decorate`` function in
  183. the ``decorator`` module is such a factory:
  184. ```python
  185. >>> from decorator import decorate
  186. ```
  187. ``decorate`` takes two arguments:
  188. 1. a caller function describing the functionality of the decorator, and
  189. 2. a function to be decorated.
  190. The caller function must have signature ``(f, *args, **kw)``, and it
  191. must call the original function ``f`` with arguments ``args`` and ``kw``,
  192. implementing the wanted capability (in this case, memoization):
  193. $$_memoize
  194. Now, you can define your decorator as follows:
  195. $$memoize
  196. The difference from the nested function approach of ``memoize_uw``
  197. is that the decorator module forces you to lift the inner function
  198. to the outer level. Moreover, you are forced to explicitly pass the
  199. function you want to decorate; there are no closures.
  200. Here is a test of usage:
  201. ```python
  202. >>> @memoize
  203. ... def heavy_computation():
  204. ... time.sleep(2)
  205. ... return "done"
  206. >>> print(heavy_computation()) # the first time it will take 2 seconds
  207. done
  208. >>> print(heavy_computation()) # the second time it will be instantaneous
  209. done
  210. ```
  211. The signature of ``heavy_computation`` is the one you would expect:
  212. ```python
  213. >>> print(getfullargspec(heavy_computation))
  214. FullArgSpec(args=[], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
  215. ```
  216. A ``trace`` decorator
  217. ------------------------------------------------------
  218. Here is an example of how to define a simple ``trace`` decorator,
  219. which prints a message whenever the traced function is called:
  220. $$_trace
  221. $$trace
  222. Here is an example of usage:
  223. ```python
  224. >>> @trace
  225. ... def f1(x):
  226. ... pass
  227. ```
  228. It is immediate to verify that ``f1`` works...
  229. ```python
  230. >>> f1(0)
  231. calling f1 with args (0,), {}
  232. ```
  233. ...and it that it has the correct signature:
  234. ```python
  235. >>> print(getfullargspec(f1))
  236. FullArgSpec(args=['x'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={})
  237. ```
  238. The decorator works with functions of any signature:
  239. ```python
  240. >>> @trace
  241. ... def f(x, y=1, z=2, *args, **kw):
  242. ... pass
  243. >>> f(0, 3)
  244. calling f with args (0, 3, 2), {}
  245. >>> print(getfullargspec(f))
  246. FullArgSpec(args=['x', 'y', 'z'], varargs='args', varkw='kw', defaults=(1, 2), kwonlyargs=[], kwonlydefaults=None, annotations={})
  247. ```
  248. $FUNCTION_ANNOTATIONS
  249. ``decorator.decorator``
  250. ---------------------------------------------
  251. It can become tedious to write a caller function (like the above
  252. ``_trace`` example) and then a trivial wrapper
  253. (``def trace(f): return decorate(f, _trace)``) every time.
  254. Not to worry! The ``decorator`` module provides an easy shortcut
  255. to convert the caller function into a signature-preserving decorator.
  256. It is the ``decorator`` function:
  257. ```python
  258. >>> from decorator import decorator
  259. >>> print(decorator.__doc__)
  260. decorator(caller) converts a caller function into a decorator
  261. ```
  262. The ``decorator`` function can be used as a signature-changing
  263. decorator, just like ``classmethod`` and ``staticmethod``.
  264. But ``classmethod`` and ``staticmethod`` return generic
  265. objects which are not callable. Instead, ``decorator`` returns
  266. signature-preserving decorators (i.e. functions with a single argument).
  267. For instance, you can write:
  268. ```python
  269. >>> @decorator
  270. ... def trace(f, *args, **kw):
  271. ... kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
  272. ... print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
  273. ... return f(*args, **kw)
  274. ```
  275. And ``trace`` is now a decorator!
  276. ```python
  277. >>> trace # doctest: +ELLIPSIS
  278. <function trace at 0x...>
  279. ```
  280. Here is an example of usage:
  281. ```python
  282. >>> @trace
  283. ... def func(): pass
  284. >>> func()
  285. calling func with args (), {}
  286. ```
  287. The `decorator` function can also be used to define factories of decorators,
  288. i.e. functions returning decorators. In general you can just write something
  289. like this:
  290. ```python
  291. def decfactory(param1, param2, ...):
  292. def caller(f, *args, **kw):
  293. return somefunc(f, param1, param2, .., *args, **kw)
  294. return decorator(caller)
  295. ```
  296. This is fully general but requires an additional level of nesting. For this
  297. reason since version 4.2 there is a facility to build
  298. decorator factories by using a single caller with default arguments i.e.
  299. writing something like this:
  300. ```python
  301. def caller(f, param1=default1, param2=default2, ..., *args, **kw):
  302. return somefunc(f, param1, param2, *args, **kw)
  303. decfactory = decorator(caller)
  304. ```
  305. Notice that this simplified approach *only works with default arguments*,
  306. i.e. `param1`, `param2` etc must have known defaults. Thanks to this
  307. restriction, there exists an unique default decorator, i.e. the member
  308. of the family which uses the default values for all parameters. Such
  309. decorator can be written as ``decfactory()`` with no parameters specified;
  310. moreover, as a shortcut, it is also possible to elide the parenthesis,
  311. a feature much requested by the users. For years I have been opposite
  312. to this feature request, since having explicit parenthesis to me is more clear
  313. and less magic; however once this feature entered in decorators of
  314. the Python standard library (I am referring to the [dataclass decorator](
  315. https://www.python.org/dev/peps/pep-0557/)) I finally gave up.
  316. The example below will show how it works in practice.
  317. Decorator factories
  318. -------------------------------------------
  319. Sometimes one has to deal with blocking resources, such as ``stdin``.
  320. Sometimes it is better to receive a "busy" message than just blocking
  321. everything.
  322. This can be accomplished with a suitable family of decorators (decorator
  323. factory), parameterize by a string, the busy message:
  324. $$blocking
  325. Functions decorated with ``blocking`` will return a busy message if
  326. the resource is unavailable, and the intended result if the resource is
  327. available. For instance:
  328. ```python
  329. >>> @blocking(msg="Please wait ...")
  330. ... def read_data():
  331. ... time.sleep(3) # simulate a blocking resource
  332. ... return "some data"
  333. >>> print(read_data()) # data is not available yet
  334. Please wait ...
  335. >>> time.sleep(1)
  336. >>> print(read_data()) # data is not available yet
  337. Please wait ...
  338. >>> time.sleep(1)
  339. >>> print(read_data()) # data is not available yet
  340. Please wait ...
  341. >>> time.sleep(1.1) # after 3.1 seconds, data is available
  342. >>> print(read_data())
  343. some data
  344. ```
  345. Decorator factories are most useful to framework builders. Here is an example
  346. that gives an idea of how you could manage permissions in a framework:
  347. $$Action
  348. where ``restricted`` is a decorator factory defined as follows
  349. $$restricted
  350. Notice that if you forget to use the keyword argument notation, i.e. if you
  351. write ``restricted(User)`` instead of ``restricted(user_class=User)`` you
  352. will get an error
  353. ```python
  354. TypeError: You are decorating a non function: <class '__main__.User'>
  355. ```
  356. Be careful!
  357. ``decorator(cls)``
  358. --------------------------------------------
  359. The ``decorator`` facility can also produce a decorator starting
  360. from a class with the signature of a caller. In such a case the
  361. produced generator is able to convert functions into factories
  362. to create instances of that class.
  363. As an example, here is a decorator which can convert a
  364. blocking function into an asynchronous function. When
  365. the function is called, it is executed in a separate thread.
  366. (This is similar to the approach used in the ``concurrent.futures`` package.
  367. But I don't recommend that you implement futures this way; this is just an
  368. example.)
  369. $$Future
  370. The decorated function returns a ``Future`` object. It has a ``.result()``
  371. method which blocks until the underlying thread finishes and returns
  372. the final result.
  373. Here is the minimalistic usage:
  374. ```python
  375. >>> @decorator(Future)
  376. ... def long_running(x):
  377. ... time.sleep(.5)
  378. ... return x
  379. >>> fut1 = long_running(1)
  380. >>> fut2 = long_running(2)
  381. >>> fut1.result() + fut2.result()
  382. 3
  383. ```
  384. contextmanager
  385. -------------------------------------
  386. Python's standard library has the ``contextmanager`` decorator,
  387. which converts a generator function into a ``GeneratorContextManager``
  388. factory. For instance, if you write this...
  389. ```python
  390. >>> from contextlib import contextmanager
  391. >>> @contextmanager
  392. ... def before_after(before, after):
  393. ... print(before)
  394. ... yield
  395. ... print(after)
  396. ```
  397. ...then ``before_after`` is a factory function that returns
  398. ``GeneratorContextManager`` objects, which provide the
  399. use of the ``with`` statement:
  400. ```python
  401. >>> with before_after('BEFORE', 'AFTER'):
  402. ... print('hello')
  403. BEFORE
  404. hello
  405. AFTER
  406. ```
  407. Basically, it is as if the content of the ``with`` block was executed
  408. in the place of the ``yield`` expression in the generator function.
  409. In Python 3.2, ``GeneratorContextManager`` objects were enhanced with
  410. a ``__call__`` method, so that they can be used as decorators, like so:
  411. ```python
  412. >>> ba = before_after('BEFORE', 'AFTER')
  413. >>>
  414. >>> @ba # doctest: +SKIP
  415. ... def hello():
  416. ... print('hello')
  417. ...
  418. >>> hello() # doctest: +SKIP
  419. BEFORE
  420. hello
  421. AFTER
  422. ```
  423. The ``ba`` decorator basically inserts a ``with ba:`` block
  424. inside the function.
  425. However, there are two issues:
  426. 1. ``GeneratorContextManager`` objects are only callable in Python 3.2,
  427. so the previous example breaks in older versions of Python.
  428. (You can solve this by installing ``contextlib2``, which backports
  429. the Python 3 functionality to Python 2.)
  430. 2. ``GeneratorContextManager`` objects do not preserve the signature of
  431. the decorated functions. The decorated ``hello`` function above will
  432. have the generic signature ``hello(*args, **kwargs)``, but fails if
  433. called with more than zero arguments.
  434. For these reasons, the `decorator` module, starting from release 3.4, offers a
  435. ``decorator.contextmanager`` decorator that solves both problems,
  436. *and* works in all supported Python versions. Its usage is identical,
  437. and factories decorated with ``decorator.contextmanager`` will return
  438. instances of ``ContextManager``, a subclass of the standard library's
  439. ``contextlib.GeneratorContextManager`` class. The subclass includes
  440. an improved ``__call__`` method, which acts as a signature-preserving
  441. decorator.
  442. The ``FunctionMaker`` class
  443. ---------------------------------------------------------------
  444. You may wonder how the functionality of the ``decorator`` module
  445. is implemented. The basic building block is
  446. a ``FunctionMaker`` class. It generates on-the-fly functions
  447. with a given name and signature from a function template
  448. passed as a string.
  449. If you're just writing ordinary decorators, then you probably won't
  450. need to use ``FunctionMaker`` directly. But in some circumstances, it
  451. can be handy. You will see an example shortly--in
  452. the implementation of a cool decorator utility (``decorator_apply``).
  453. ``FunctionMaker`` provides the ``.create`` classmethod, which
  454. accepts the *name*, *signature*, and *body* of the function
  455. you want to generate, as well as the execution environment
  456. where the function is generated by ``exec``.
  457. Here's an example:
  458. ```python
  459. >>> def f(*args, **kw): # a function with a generic signature
  460. ... print(args, kw)
  461. >>> f1 = FunctionMaker.create('f1(a, b)', 'f(a, b)', dict(f=f))
  462. >>> f1(1,2)
  463. (1, 2) {}
  464. ```
  465. It is important to notice that the function body is interpolated
  466. before being executed; **be careful** with the ``%`` sign!
  467. ``FunctionMaker.create`` also accepts keyword arguments.
  468. The keyword arguments are attached to the generated function.
  469. This is useful if you want to set some function attributes
  470. (e.g., the docstring ``__doc__``).
  471. For debugging/introspection purposes, it may be useful to see
  472. the source code of the generated function. To do this, just
  473. pass ``addsource=True``, and the generated function will get
  474. a ``__source__`` attribute:
  475. ```python
  476. >>> f1 = FunctionMaker.create(
  477. ... 'f1(a, b)', 'f(a, b)', dict(f=f), addsource=True)
  478. >>> print(f1.__source__)
  479. def f1(a, b):
  480. f(a, b)
  481. <BLANKLINE>
  482. ```
  483. The first argument to ``FunctionMaker.create`` can be a string (as above),
  484. or a function. This is the most common usage, since you typically decorate
  485. pre-existing functions.
  486. If you're writing a framework, however, you may want to use
  487. ``FunctionMaker.create`` directly, rather than ``decorator``, because it gives
  488. you direct access to the body of the generated function.
  489. For instance, suppose you want to instrument the ``__init__`` methods of a
  490. set of classes, by preserving their signature.
  491. (This use case is not made up. This is done by SQAlchemy, and other frameworks,
  492. too.)
  493. Here is what happens:
  494. - If first argument of ``FunctionMaker.create`` is a function,
  495. an instance of ``FunctionMaker`` is created with the attributes
  496. ``args``, ``varargs``, ``keywords``, and ``defaults``.
  497. (These mirror the return values of the standard library's
  498. ``inspect.getfullargspec``.)
  499. - For each item in ``args`` (a list of strings of the names of all required
  500. arguments), an attribute ``arg0``, ``arg1``, ..., ``argN`` is also generated.
  501. - Finally, there is a ``signature`` attribute, which is a string with the
  502. signature of the original function.
  503. **NOTE:** You should not pass signature strings with default arguments
  504. (e.g., something like ``'f1(a, b=None)'``). Just pass ``'f1(a, b)'``,
  505. followed by a tuple of defaults:
  506. ```python
  507. >>> f1 = FunctionMaker.create(
  508. ... 'f1(a, b)', 'f(a, b)', dict(f=f), addsource=True, defaults=(None,))
  509. >>> print(getfullargspec(f1))
  510. FullArgSpec(args=['a', 'b'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={})
  511. ```
  512. Getting the source code
  513. ---------------------------------------------------
  514. Internally, ``FunctionMaker.create`` uses ``exec`` to generate the
  515. decorated function. Therefore ``inspect.getsource`` will not work for
  516. decorated functions. In IPython, this means that the usual ``??`` trick
  517. will give you the (right on the spot) message ``Dynamically generated
  518. function. No source code available``.
  519. In the past, I considered this acceptable, since ``inspect.getsource``
  520. does not really work with "regular" decorators. In those cases,
  521. ``inspect.getsource`` gives you the wrapper source code, which is probably
  522. not what you want:
  523. $$identity_dec
  524. $$example
  525. ```python
  526. >>> import inspect
  527. >>> print(inspect.getsource(example))
  528. def wrapper(*args, **kw):
  529. return func(*args, **kw)
  530. <BLANKLINE>
  531. ```
  532. (See bug report [1764286](http://bugs.python.org/issue1764286)
  533. for an explanation of what is happening).
  534. Unfortunately the bug still exists in all versions of Python < 3.5.
  535. However, there is a workaround. The decorated function has the ``__wrapped__``
  536. attribute, pointing to the original function. The simplest way to get the
  537. source code is to call ``inspect.getsource`` on the undecorated function:
  538. ```python
  539. >>> print(inspect.getsource(factorial.__wrapped__))
  540. @tail_recursive
  541. def factorial(n, acc=1):
  542. "The good old factorial"
  543. if n == 0:
  544. return acc
  545. return factorial(n-1, n*acc)
  546. <BLANKLINE>
  547. ```
  548. Dealing with third-party decorators
  549. -----------------------------------------------------------------
  550. Sometimes on the net you find some cool decorator that you would
  551. like to include in your code. However, more often than not, the cool
  552. decorator is not signature-preserving. What you need is an easy way to
  553. upgrade third party decorators to signature-preserving decorators...
  554. *without* having to rewrite them in terms of ``decorator``.
  555. You can use a ``FunctionMaker`` to implement that functionality as follows:
  556. $$decorator_apply
  557. ``decorator_apply`` sets the generated function's ``__wrapped__`` attribute
  558. to the original function, so you can get the right source code.
  559. If you are using a Python later than 3.2, you should also set the
  560. ``__qualname__`` attribute to preserve the qualified name of the original
  561. function.
  562. Notice that I am not providing this functionality in the ``decorator``
  563. module directly, since I think it is best to rewrite the decorator instead
  564. of adding another level of indirection. However, practicality
  565. beats purity, so you can add ``decorator_apply`` to your toolbox and
  566. use it if you need to.
  567. To give a good example for ``decorator_apply``, I will show a pretty slick
  568. decorator that converts a tail-recursive function into an iterative function.
  569. I have shamelessly stolen the core concept from Kay Schluehr's recipe
  570. in the Python Cookbook,
  571. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691.
  572. $$TailRecursive
  573. Here the decorator is implemented as a class returning callable
  574. objects.
  575. $$tail_recursive
  576. Here is how you apply the upgraded decorator to the good old factorial:
  577. $$factorial
  578. ```python
  579. >>> print(factorial(4))
  580. 24
  581. ```
  582. This decorator is pretty impressive, and should give you some food for
  583. thought! ;)
  584. Notice that there is no recursion limit now; you can easily compute
  585. ``factorial(1001)`` (or larger) without filling the stack frame.
  586. Notice also that the decorator will *not* work on functions which
  587. are not tail recursive, such as the following:
  588. $$fact
  589. **Reminder:** A function is *tail recursive* if it does either of the
  590. following:
  591. - returns a value without making a recursive call; or,
  592. - returns directly the result of a recursive call.
  593. Python 3.5 coroutines
  594. -----------------------
  595. I am personally not using Python 3.5 coroutines yet, because at work we are
  596. still maintaining compatibility with Python 2.7. However, some users requested
  597. support for coroutines and since version 4.1 the decorator module has it.
  598. You should consider the support experimental and kindly report issues if
  599. you find any.
  600. Here I will give a single example of usage. Suppose you want to log the moment
  601. a coroutine starts and the moment it stops for debugging purposes. You could
  602. write code like the following:
  603. ```python
  604. import time
  605. import logging
  606. from asyncio import get_event_loop, sleep, wait
  607. from decorator import decorator
  608. @decorator
  609. async def log_start_stop(coro, *args, **kwargs):
  610. logging.info('Starting %s%s', coro.__name__, args)
  611. t0 = time.time()
  612. await coro(*args, **kwargs)
  613. dt = time.time() - t0
  614. logging.info('Ending %s%s after %d seconds', coro.__name__, args, dt)
  615. @log_start_stop
  616. async def make_task(n):
  617. for i in range(n):
  618. await sleep(1)
  619. if __name__ == '__main__':
  620. logging.basicConfig(level=logging.INFO)
  621. tasks = [make_task(3), make_task(2), make_task(1)]
  622. get_event_loop().run_until_complete(wait(tasks))
  623. ```
  624. and you will get an output like this:
  625. ```bash
  626. INFO:root:Starting make_task(1,)
  627. INFO:root:Starting make_task(3,)
  628. INFO:root:Starting make_task(2,)
  629. INFO:root:Ending make_task(1,) after 1 seconds
  630. INFO:root:Ending make_task(2,) after 2 seconds
  631. INFO:root:Ending make_task(3,) after 3 seconds
  632. ```
  633. This may be handy if you have trouble understanding what it going on
  634. with a particularly complex chain of coroutines. With a single line you
  635. can decorate the troubling coroutine function, understand what happens, fix the
  636. issue and then remove the decorator (or keep it if continuous monitoring
  637. of the coroutines makes sense). Notice that
  638. ``inspect.iscoroutinefunction(make_task)``
  639. will return the right answer (i.e. ``True``).
  640. It is also possible to define decorators converting coroutine functions
  641. into regular functions, such as the following:
  642. ```python
  643. @decorator
  644. def coro_to_func(coro, *args, **kw):
  645. "Convert a coroutine into a function"
  646. return get_event_loop().run_until_complete(coro(*args, **kw))
  647. ```
  648. Notice the diffence: the caller in ``log_start_stop`` was a coroutine
  649. function and the associate decorator was converting coroutines->coroutines;
  650. the caller in ``coro_to_func`` is a regular function and converts
  651. coroutines -> functions.
  652. Multiple dispatch
  653. -------------------------------------------
  654. There has been talk of implementing multiple dispatch functions
  655. (i.e. "generic functions") in Python for over ten years. Last year,
  656. something concrete was done for the first time. As of Python 3.4,
  657. we have the decorator ``functools.singledispatch`` to implement generic
  658. functions!
  659. As its name implies, it is limited to *single dispatch*; in other words,
  660. it is able to dispatch on the first argument of the function only.
  661. The ``decorator`` module provides the decorator factory ``dispatch_on``,
  662. which can be used to implement generic functions dispatching on *any* argument.
  663. Moreover, it can manage dispatching on more than one argument.
  664. (And, of course, it is signature-preserving.)
  665. Here is a concrete example (from a real-life use case) where it is desiderable
  666. to dispatch on the second argument.
  667. Suppose you have an ``XMLWriter`` class, which is instantiated
  668. with some configuration parameters, and has the ``.write`` method which
  669. serializes objects to XML:
  670. $$XMLWriter
  671. Here, you want to dispatch on the *second* argument; the first is already
  672. taken by ``self``. The ``dispatch_on`` decorator factory allows you to specify
  673. the dispatch argument simply by passing its name as a string. (Note
  674. that if you misspell the name you will get an error.)
  675. The decorated function `write` is turned into a generic function (
  676. `write` is a function at the idea it is decorated; it will be turned
  677. into a method later, at class instantiation time),
  678. and it is called if there are no more specialized implementations.
  679. Usually, default functions should raise a ``NotImplementedError``, thus
  680. forcing people to register some implementation.
  681. You can perform the registration with a decorator:
  682. $$writefloat
  683. Now ``XMLWriter`` can serialize floats:
  684. ```python
  685. >>> writer = XMLWriter()
  686. >>> writer.write(2.3)
  687. '<float>2.3</float>'
  688. ```
  689. I could give a down-to-earth example of situations in which it is desiderable
  690. to dispatch on more than one argument--for instance, I once implemented
  691. a database-access library where the first dispatching argument was the
  692. the database driver, and the second was the database record--but here
  693. I will follow tradition, and show the time-honored Rock-Paper-Scissors example:
  694. $$Rock
  695. $$Paper
  696. $$Scissors
  697. I have added an ordinal to the Rock-Paper-Scissors classes to simplify
  698. the implementation. The idea is to define a generic function (``win(a,
  699. b)``) of two arguments corresponding to the *moves* of the first and
  700. second players. The *moves* are instances of the classes
  701. Rock, Paper, and Scissors:
  702. - Paper wins over Rock
  703. - Scissors wins over Paper
  704. - Rock wins over Scissors
  705. The function will return +1 for a win, -1 for a loss, and 0 for parity.
  706. There are 9 combinations, but combinations with the same ordinal
  707. (i.e. the same class) return 0. Moreover, by exchanging the order of the
  708. arguments, the sign of the result changes. Therefore, it is sufficient to
  709. directly specify only 3 implementations:
  710. $$win
  711. $$winRockPaper
  712. $$winPaperScissors
  713. $$winRockScissors
  714. Here is the result:
  715. ```python
  716. >>> win(Paper(), Rock())
  717. 1
  718. >>> win(Scissors(), Paper())
  719. 1
  720. >>> win(Rock(), Scissors())
  721. 1
  722. >>> win(Paper(), Paper())
  723. 0
  724. >>> win(Rock(), Rock())
  725. 0
  726. >>> win(Scissors(), Scissors())
  727. 0
  728. >>> win(Rock(), Paper())
  729. -1
  730. >>> win(Paper(), Scissors())
  731. -1
  732. >>> win(Scissors(), Rock())
  733. -1
  734. ```
  735. The point of generic functions is that they play well with subclassing.
  736. For instance, suppose we define a ``StrongRock``, which does not lose against
  737. Paper:
  738. $$StrongRock
  739. $$winStrongRockPaper
  740. Then you do not need to define other implementations; they are
  741. inherited from the parent:
  742. ```python
  743. >>> win(StrongRock(), Scissors())
  744. 1
  745. ```
  746. You can introspect the precedence used by the dispath algorithm by
  747. calling ``.dispatch_info(*types)``:
  748. ```python
  749. >>> win.dispatch_info(StrongRock, Scissors)
  750. [('StrongRock', 'Scissors'), ('Rock', 'Scissors')]
  751. ```
  752. Since there is no direct implementation for (``StrongRock``, ``Scissors``),
  753. the dispatcher will look at the implementation for (``Rock``, ``Scissors``)
  754. which is available. Internally, the algorithm is doing a cross
  755. product of the class precedence lists (or *Method Resolution Orders*,
  756. [MRO](http://www.python.org/2.3/mro.html) for short) of ``StrongRock``
  757. and ``Scissors``, respectively.
  758. Generic functions and virtual ancestors
  759. -------------------------------------------------
  760. In Python, generic functions are complicated by the existence of
  761. "virtual ancestors": superclasses which are not in the class hierarchy.
  762. Consider this class:
  763. $$WithLength
  764. This class defines a ``__len__`` method, and is therefore
  765. considered to be a subclass of the abstract base class
  766. ``collections.abc.Sized`` (``collections.Sized`` on Python 2):
  767. ```python
  768. >>> issubclass(WithLength, collections.abc.Sized)
  769. True
  770. ```
  771. However, ``collections.abc.Sized`` is not in the MRO_ of ``WithLength``; it
  772. is not a true ancestor. Any implementation of generic functions (even
  773. with single dispatch) must go through some contorsion to take into
  774. account the virtual ancestors.
  775. In particular, if we define a generic function...
  776. $$get_length
  777. ...implemented on all classes with a length...
  778. $$get_length_sized
  779. ...then ``get_length`` must be defined on ``WithLength`` instances...
  780. ```python
  781. >>> get_length(WithLength())
  782. 0
  783. ```
  784. ...even if ``collections.abc.Sized`` is not a true ancestor of ``WithLength``.
  785. Of course, this is a contrived example--you could just use the
  786. builtin ``len``--but you should get the idea.
  787. Since in Python it is possible to consider any instance of ``ABCMeta``
  788. as a virtual ancestor of any other class (it is enough to register it
  789. as ``ancestor.register(cls)``), any implementation of generic functions
  790. must be aware of the registration mechanism.
  791. For example, suppose you are using a third-party set-like class, like
  792. the following:
  793. $$SomeSet
  794. Here, the author of ``SomeSet`` made a mistake by inheriting from
  795. ``collections.abc.Sized`` (instead of ``collections.abc.Set``).
  796. This is not a problem. You can register *a posteriori*
  797. ``collections.abc.Set`` as a virtual ancestor of ``SomeSet``:
  798. ```python
  799. >>> _ = collections.abc.Set.register(SomeSet)
  800. >>> issubclass(SomeSet, collections.abc.Set)
  801. True
  802. ```
  803. Now, let's define an implementation of ``get_length`` specific to set:
  804. $$get_length_set
  805. The current implementation (and ``functools.singledispatch`` too)
  806. is able to discern that a ``Set`` is a ``Sized`` object, by looking at
  807. the class registry, so it uses the more specific implementation for ``Set``:
  808. ```python
  809. >>> get_length(SomeSet()) # NB: the implementation for Sized would give 0
  810. 1
  811. ```
  812. Sometimes it is not clear how to dispatch. For instance, consider a
  813. class ``C`` registered both as ``collections.abc.Iterable`` and
  814. ``collections.abc.Sized``, and defines a generic function ``g`` with
  815. implementations for both ``collections.abc.Iterable`` *and*
  816. ``collections.abc.Sized``:
  817. $$singledispatch_example1
  818. It is impossible to decide which implementation to use, since the ancestors
  819. are independent. The following function will raise a ``RuntimeError``
  820. when called. This is consistent with the "refuse the temptation to guess"
  821. philosophy. ``functools.singledispatch`` would raise a similar error.
  822. It would be easy to rely on the order of registration to decide the
  823. precedence order. This is reasonable, but also fragile:
  824. - if, during some refactoring, you change the registration order by mistake,
  825. a different implementation could be taken;
  826. - if implementations of the generic functions are distributed across modules,
  827. and you change the import order, a different implementation could be taken.
  828. So the ``decorator`` module prefers to raise an error in the face of ambiguity.
  829. This is the same approach taken by the standard library.
  830. However, it should be noted that the *dispatch algorithm* used by the decorator
  831. module is different from the one used by the standard library, so in certain
  832. cases you will get different answers. The difference is that
  833. ``functools.singledispatch`` tries to insert the virtual ancestors *before* the
  834. base classes, whereas ``decorator.dispatch_on`` tries to insert them *after*
  835. the base classes.
  836. Here's an example that shows the difference:
  837. $$singledispatch_example2
  838. If you play with this example and replace the ``singledispatch`` definition
  839. with ``functools.singledispatch``, the assertion will break: ``g`` will return
  840. ``"container"`` instead of ``"s"``, because ``functools.singledispatch``
  841. will insert the ``Container`` class right before ``S``.
  842. Notice that here I am not making any bold claim such as "the standard
  843. library algorithm is wrong and my algorithm is right" or viceversa. It
  844. just point out that there are some subtle differences. The only way to
  845. understand what is really happening here is to scratch your head by
  846. looking at the implementations. I will just notice that
  847. ``.dispatch_info`` is quite essential to see the class precedence
  848. list used by algorithm:
  849. ```python
  850. >>> g, V = singledispatch_example2()
  851. >>> g.dispatch_info(V)
  852. [('V',), ('Sized',), ('S',), ('Container',)]
  853. ```
  854. The current implementation does not implement any kind of cooperation
  855. between implementations. In other words, nothing is akin either to
  856. call-next-method in Lisp, or to ``super`` in Python.
  857. Finally, let me notice that the decorator module implementation does
  858. not use any cache, whereas the ``singledispatch`` implementation does.
  859. Caveats and limitations
  860. -------------------------------------------
  861. One thing you should be aware of, is the performance penalty of decorators.
  862. The worse case is shown by the following example:
  863. ```bash
  864. $ cat performance.sh
  865. python3 -m timeit -s "
  866. from decorator import decorator
  867. @decorator
  868. def do_nothing(func, *args, **kw):
  869. return func(*args, **kw)
  870. @do_nothing
  871. def f():
  872. pass
  873. " "f()"
  874. python3 -m timeit -s "
  875. def f():
  876. pass
  877. " "f()"
  878. ```
  879. On my laptop, using the ``do_nothing`` decorator instead of the
  880. plain function is five times slower:
  881. ```bash
  882. $ bash performance.sh
  883. 1000000 loops, best of 3: 1.39 usec per loop
  884. 1000000 loops, best of 3: 0.278 usec per loop
  885. ```
  886. Of course, a real life function probably does something more useful
  887. than the function ``f`` here, so the real life performance penalty
  888. *could* be negligible. As always, the only way to know if there is a
  889. penalty in your specific use case is to measure it.
  890. More importantly, you should be aware that decorators will make your
  891. tracebacks longer and more difficult to understand.
  892. Consider this example:
  893. ```python
  894. >>> @trace
  895. ... def f():
  896. ... 1/0
  897. ```
  898. Calling ``f()`` gives you a ``ZeroDivisionError``.
  899. But since the function is decorated, the traceback is longer:
  900. ```python
  901. >>> f() # doctest: +ELLIPSIS
  902. Traceback (most recent call last):
  903. ...
  904. File "<string>", line 2, in f
  905. File "<doctest __main__[22]>", line 4, in trace
  906. return f(*args, **kw)
  907. File "<doctest __main__[51]>", line 3, in f
  908. 1/0
  909. ZeroDivisionError: ...
  910. ```
  911. You see here the inner call to the decorator ``trace``, which calls
  912. ``f(*args, **kw)``, and a reference to ``File "<string>", line 2, in f``.
  913. This latter reference is due to the fact that, internally, the decorator
  914. module uses ``exec`` to generate the decorated function. Notice that
  915. ``exec`` is *not* responsible for the performance penalty, since is the
  916. called *only once* (at function decoration time); it is *not* called
  917. each time the decorated function is called.
  918. Presently, there is no clean way to avoid ``exec``. A clean solution
  919. would require changing the CPython implementation, by
  920. adding a hook to functions (to allow changing their signature directly).
  921. Even in Python 3.5, it is impossible to change the
  922. function signature directly. Thus, the ``decorator`` module is
  923. still useful! As a matter of fact, this is the main reason why I still
  924. maintain the module and release new versions.
  925. It should be noted that in Python 3.5, a *lot* of improvements have
  926. been made: you can decorate a function with
  927. ``func_tools.update_wrapper``, and ``pydoc`` will see the correct
  928. signature. Unfortunately, the function will still have an incorrect
  929. signature internally, as you can see by using
  930. ``inspect.getfullargspec``; so, all documentation tools using
  931. ``inspect.getfullargspec`` - which has been rightly deprecated -
  932. will see the wrong signature.
  933. In the present implementation, decorators generated by ``decorator``
  934. can only be used on user-defined Python functions or methods.
  935. They cannot be used on generic callable objects or built-in functions,
  936. due to limitations of the standard library's ``inspect`` module, especially
  937. for Python 2. In Python 3.5, many such limitations have been removed, but
  938. I still think that it is cleaner and safer to decorate only functions and
  939. coroutines. If you want to decorate things like classmethods/staticmethods
  940. and general callables - which I will never support in the decorator module -
  941. I suggest you to look at the [wrapt](https://wrapt.readthedocs.io/en/latest/)
  942. project by Graeme Dumpleton.
  943. There is a strange quirk when decorating functions with keyword
  944. arguments, if one of the arguments has the same name used in the
  945. caller function for the first argument. The quirk was reported by
  946. David Goldstein.
  947. Here is an example where it is manifest:
  948. ```python
  949. >>> @memoize
  950. ... def getkeys(**kw):
  951. ... return kw.keys()
  952. >>> getkeys(func='a') # doctest: +ELLIPSIS
  953. Traceback (most recent call last):
  954. ...
  955. TypeError: _memoize() got multiple values for ... 'func'
  956. ```
  957. The error message looks really strange... until you realize that
  958. the caller function `_memoize` uses `func` as first argument,
  959. so there is a confusion between the positional argument and the
  960. keywork arguments.
  961. The solution is to change the name of the first argument in `_memoize`,
  962. or to change the implementation like so:
  963. ```python
  964. def _memoize(*all_args, **kw):
  965. func = all_args[0]
  966. args = all_args[1:]
  967. if kw: # frozenset is used to ensure hashability
  968. key = args, frozenset(kw.items())
  969. else:
  970. key = args
  971. cache = func.cache # attribute added by memoize
  972. if key not in cache:
  973. cache[key] = func(*args, **kw)
  974. return cache[key]
  975. ```
  976. This avoids the need to name the first argument, so the problem
  977. simply disappears. This is a technique that you should keep in mind
  978. when writing decorators for functions with keyword arguments. Also,
  979. notice that lately I have come to believe that decorating functions with
  980. keyword arguments is not such a good idea, and you may want not to do
  981. that.
  982. On a similar note, there is a restriction on argument names. For instance,
  983. if you name an argument ``_call_`` or ``_func_``, you will get a ``NameError``:
  984. ```python
  985. >>> @trace
  986. ... def f(_func_): print(f)
  987. ...
  988. Traceback (most recent call last):
  989. ...
  990. NameError: _func_ is overridden in
  991. def f(_func_):
  992. return _call_(_func_, _func_)
  993. ```
  994. Finally, the implementation is such that the decorated function makes
  995. a (shallow) copy of the original function dictionary:
  996. ```python
  997. >>> def f(): pass # the original function
  998. >>> f.attr1 = "something" # setting an attribute
  999. >>> f.attr2 = "something else" # setting another attribute
  1000. >>> traced_f = trace(f) # the decorated function
  1001. >>> traced_f.attr1
  1002. 'something'
  1003. >>> traced_f.attr2 = "something different" # setting attr
  1004. >>> f.attr2 # the original attribute did not change
  1005. 'something else'
  1006. ```
  1007. LICENSE (2-clause BSD)
  1008. ---------------------------------------------
  1009. Copyright (c) 2005-2020, Michele Simionato
  1010. All rights reserved.
  1011. Redistribution and use in source and binary forms, with or without
  1012. modification, are permitted provided that the following conditions are
  1013. met:
  1014. Redistributions of source code must retain the above copyright
  1015. notice, this list of conditions and the following disclaimer.
  1016. Redistributions in bytecode form must reproduce the above copyright
  1017. notice, this list of conditions and the following disclaimer in
  1018. the documentation and/or other materials provided with the
  1019. distribution.
  1020. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  1021. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  1022. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  1023. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  1024. HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  1025. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  1026. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  1027. OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  1028. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  1029. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  1030. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  1031. DAMAGE.
  1032. If you use this software and you are happy with it, consider sending me a
  1033. note, just to gratify my ego. On the other hand, if you use this software and
  1034. you are unhappy with it, send me a patch!
  1035. """
  1036. function_annotations = """Function annotations
  1037. ---------------------------------------------
  1038. Python 3 introduced the concept of [function annotations](
  1039. http://www.python.org/dev/peps/pep-3107/): the ability
  1040. to annotate the signature of a function with additional information,
  1041. stored in a dictionary named ``__annotations__``. The ``decorator`` module
  1042. (starting from release 3.3) will understand and preserve these annotations.
  1043. Here is an example:
  1044. ```python
  1045. >>> @trace
  1046. ... def f(x: 'the first argument', y: 'default argument'=1, z=2,
  1047. ... *args: 'varargs', **kw: 'kwargs'):
  1048. ... pass
  1049. ```
  1050. In order to introspect functions with annotations, one needs the
  1051. utility ``inspect.getfullargspec`` (introduced in Python 3, then
  1052. deprecated in Python 3.5, then undeprecated in Python 3.6):
  1053. ```python
  1054. >>> from inspect import getfullargspec
  1055. >>> argspec = getfullargspec(f)
  1056. >>> argspec.args
  1057. ['x', 'y', 'z']
  1058. >>> argspec.varargs
  1059. 'args'
  1060. >>> argspec.varkw
  1061. 'kw'
  1062. >>> argspec.defaults
  1063. (1, 2)
  1064. >>> argspec.kwonlyargs
  1065. []
  1066. >>> argspec.kwonlydefaults
  1067. ```
  1068. You can check that the ``__annotations__`` dictionary is preserved:
  1069. ```python
  1070. >>> f.__annotations__ is f.__wrapped__.__annotations__
  1071. True
  1072. ```
  1073. Here ``f.__wrapped__`` is the original undecorated function.
  1074. This attribute exists for consistency with the behavior of
  1075. ``functools.update_wrapper``.
  1076. Another attribute copied from the original function is ``__qualname__``,
  1077. the qualified name. This attribute was introduced in Python 3.3.
  1078. """
  1079. if sys.version_info < (3,):
  1080. function_annotations = ''
  1081. today = time.strftime('%Y-%m-%d')
  1082. __doc__ = (doc.replace('$VERSION', __version__).replace('$DATE', today)
  1083. .replace('$FUNCTION_ANNOTATIONS', function_annotations))
  1084. def decorator_apply(dec, func):
  1085. """
  1086. Decorate a function by preserving the signature even if dec
  1087. is not a signature-preserving decorator.
  1088. """
  1089. return FunctionMaker.create(
  1090. func, 'return decfunc(%(signature)s)',
  1091. dict(decfunc=dec(func)), __wrapped__=func)
  1092. def _trace(f, *args, **kw):
  1093. kwstr = ', '.join('%r: %r' % (k, kw[k]) for k in sorted(kw))
  1094. print("calling %s with args %s, {%s}" % (f.__name__, args, kwstr))
  1095. return f(*args, **kw)
  1096. def trace(f):
  1097. return decorate(f, _trace)
  1098. class Future(threading.Thread):
  1099. """
  1100. A class converting blocking functions into asynchronous
  1101. functions by using threads.
  1102. """
  1103. def __init__(self, func, *args, **kw):
  1104. try:
  1105. counter = func.counter
  1106. except AttributeError: # instantiate the counter at the first call
  1107. counter = func.counter = itertools.count(1)
  1108. name = '%s-%s' % (func.__name__, next(counter))
  1109. def func_wrapper():
  1110. self._result = func(*args, **kw)
  1111. super(Future, self).__init__(target=func_wrapper, name=name)
  1112. self.start()
  1113. def result(self):
  1114. self.join()
  1115. return self._result
  1116. def identity_dec(func):
  1117. def wrapper(*args, **kw):
  1118. return func(*args, **kw)
  1119. return wrapper
  1120. @identity_dec
  1121. def example():
  1122. pass
  1123. def memoize_uw(func):
  1124. func.cache = {}
  1125. def memoize(*args, **kw):
  1126. if kw: # frozenset is used to ensure hashability
  1127. key = args, frozenset(kw.items())
  1128. else:
  1129. key = args
  1130. if key not in func.cache:
  1131. func.cache[key] = func(*args, **kw)
  1132. return func.cache[key]
  1133. return functools.update_wrapper(memoize, func)
  1134. @memoize_uw
  1135. def f1(x):
  1136. "Simulate some long computation"
  1137. time.sleep(1)
  1138. return x
  1139. def _memoize(func, *args, **kw):
  1140. if kw: # frozenset is used to ensure hashability
  1141. key = args, frozenset(kw.items())
  1142. else:
  1143. key = args
  1144. cache = func.cache # attribute added by memoize
  1145. if key not in cache:
  1146. cache[key] = func(*args, **kw)
  1147. return cache[key]
  1148. def memoize(f):
  1149. """
  1150. A simple memoize implementation. It works by adding a .cache dictionary
  1151. to the decorated function. The cache will grow indefinitely, so it is
  1152. your responsibility to clear it, if needed.
  1153. """
  1154. f.cache = {}
  1155. return decorate(f, _memoize)
  1156. @decorator
  1157. def blocking(f, msg='blocking', *args, **kw):
  1158. if not hasattr(f, "thread"): # no thread running
  1159. def set_result():
  1160. f.result = f(*args, **kw)
  1161. f.thread = threading.Thread(None, set_result)
  1162. f.thread.start()
  1163. return msg
  1164. elif f.thread.is_alive():
  1165. return msg
  1166. else: # the thread is ended, return the stored result
  1167. del f.thread
  1168. return f.result
  1169. class User(object):
  1170. "Will just be able to see a page"
  1171. class PowerUser(User):
  1172. "Will be able to add new pages too"
  1173. class Admin(PowerUser):
  1174. "Will be able to delete pages too"
  1175. class PermissionError(Exception):
  1176. """
  1177. >>> a = Action()
  1178. >>> a.user = User()
  1179. >>> a.view() # ok
  1180. >>> a.insert() # doctest: +IGNORE_EXCEPTION_DETAIL
  1181. Traceback (most recent call last):
  1182. ...
  1183. PermissionError: User does not have the permission to run insert!
  1184. """
  1185. @decorator
  1186. def restricted(func, user_class=User, *args, **kw):
  1187. "Restrict access to a given class of users"
  1188. self = args[0]
  1189. if isinstance(self.user, user_class):
  1190. return func(*args, **kw)
  1191. else:
  1192. raise PermissionError(
  1193. '%s does not have the permission to run %s!'
  1194. % (self.user, func.__name__))
  1195. class Action(object):
  1196. @restricted(user_class=User)
  1197. def view(self):
  1198. "Any user can view objects"
  1199. @restricted(user_class=PowerUser)
  1200. def insert(self):
  1201. "Only power users can insert objects"
  1202. @restricted(user_class=Admin)
  1203. def delete(self):
  1204. "Only the admin can delete objects"
  1205. class TailRecursive(object):
  1206. """
  1207. tail_recursive decorator based on Kay Schluehr's recipe
  1208. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
  1209. with improvements by me and George Sakkis.
  1210. """
  1211. def __init__(self, func):
  1212. self.func = func
  1213. self.firstcall = True
  1214. self.CONTINUE = object() # sentinel
  1215. def __call__(self, *args, **kwd):
  1216. CONTINUE = self.CONTINUE
  1217. if self.firstcall:
  1218. func = self.func
  1219. self.firstcall = False
  1220. try:
  1221. while True:
  1222. result = func(*args, **kwd)
  1223. if result is CONTINUE: # update arguments
  1224. args, kwd = self.argskwd
  1225. else: # last call
  1226. return result
  1227. finally:
  1228. self.firstcall = True
  1229. else: # return the arguments of the tail call
  1230. self.argskwd = args, kwd
  1231. return CONTINUE
  1232. def tail_recursive(func):
  1233. return decorator_apply(TailRecursive, func)
  1234. @tail_recursive
  1235. def factorial(n, acc=1):
  1236. "The good old factorial"
  1237. if n == 0:
  1238. return acc
  1239. return factorial(n-1, n*acc)
  1240. def fact(n): # this is not tail-recursive
  1241. if n == 0:
  1242. return 1
  1243. return n * fact(n-1)
  1244. def a_test_for_pylons():
  1245. """
  1246. In version 3.1.0 decorator(caller) returned a nameless partial
  1247. object, thus breaking Pylons. That must not happen again.
  1248. >>> decorator(_memoize).__name__
  1249. '_memoize'
  1250. Here is another bug of version 3.1.1 missing the docstring:
  1251. >>> factorial.__doc__
  1252. 'The good old factorial'
  1253. """
  1254. if sys.version_info >= (3,): # tests for signatures specific to Python 3
  1255. def test_kwonlydefaults():
  1256. """
  1257. >>> @trace
  1258. ... def f(arg, defarg=1, *args, kwonly=2): pass
  1259. ...
  1260. >>> f.__kwdefaults__
  1261. {'kwonly': 2}
  1262. """
  1263. def test_kwonlyargs():
  1264. """
  1265. >>> @trace
  1266. ... def func(a, b, *args, y=2, z=3, **kwargs):
  1267. ... return y, z
  1268. ...
  1269. >>> func('a', 'b', 'c', 'd', 'e', y='y', z='z', cat='dog')
  1270. calling func with args ('a', 'b', 'c', 'd', 'e'), {'cat': 'dog', 'y': 'y', 'z': 'z'}
  1271. ('y', 'z')
  1272. """
  1273. def test_kwonly_no_args():
  1274. """# this was broken with decorator 3.3.3
  1275. >>> @trace
  1276. ... def f(**kw): pass
  1277. ...
  1278. >>> f()
  1279. calling f with args (), {}
  1280. """
  1281. def test_kwonly_star_notation():
  1282. """
  1283. >>> @trace
  1284. ... def f(*, a=1, **kw): pass
  1285. ...
  1286. >>> import inspect
  1287. >>> inspect.getfullargspec(f)
  1288. FullArgSpec(args=[], varargs=None, varkw='kw', defaults=None, kwonlyargs=['a'], kwonlydefaults={'a': 1}, annotations={})
  1289. """
  1290. @contextmanager
  1291. def before_after(before, after):
  1292. print(before)
  1293. yield
  1294. print(after)
  1295. ba = before_after('BEFORE', 'AFTER') # ContextManager instance
  1296. @ba
  1297. def hello(user):
  1298. """
  1299. >>> ba.__class__.__name__
  1300. 'ContextManager'
  1301. >>> hello('michele')
  1302. BEFORE
  1303. hello michele
  1304. AFTER
  1305. """
  1306. print('hello %s' % user)
  1307. # ####################### multiple dispatch ############################ #
  1308. class XMLWriter(object):
  1309. def __init__(self, **config):
  1310. self.cfg = config
  1311. @dispatch_on('obj')
  1312. def write(self, obj):
  1313. raise NotImplementedError(type(obj))
  1314. @XMLWriter.write.register(float)
  1315. def writefloat(self, obj):
  1316. return '<float>%s</float>' % obj
  1317. class Rock(object):
  1318. ordinal = 0
  1319. class Paper(object):
  1320. ordinal = 1
  1321. class Scissors(object):
  1322. ordinal = 2
  1323. class StrongRock(Rock):
  1324. pass
  1325. @dispatch_on('a', 'b')
  1326. def win(a, b):
  1327. if a.ordinal == b.ordinal:
  1328. return 0
  1329. elif a.ordinal > b.ordinal:
  1330. return -win(b, a)
  1331. raise NotImplementedError((type(a), type(b)))
  1332. @win.register(Rock, Paper)
  1333. def winRockPaper(a, b):
  1334. return -1
  1335. @win.register(Rock, Scissors)
  1336. def winRockScissors(a, b):
  1337. return 1
  1338. @win.register(Paper, Scissors)
  1339. def winPaperScissors(a, b):
  1340. return -1
  1341. @win.register(StrongRock, Paper)
  1342. def winStrongRockPaper(a, b):
  1343. return 0
  1344. class WithLength(object):
  1345. def __len__(self):
  1346. return 0
  1347. class SomeSet(collections.abc.Sized):
  1348. # methods that make SomeSet set-like
  1349. # not shown ...
  1350. def __len__(self):
  1351. return 0
  1352. @dispatch_on('obj')
  1353. def get_length(obj):
  1354. raise NotImplementedError(type(obj))
  1355. @get_length.register(collections.abc.Sized)
  1356. def get_length_sized(obj):
  1357. return len(obj)
  1358. @get_length.register(collections.abc.Set)
  1359. def get_length_set(obj):
  1360. return 1
  1361. class C(object):
  1362. "Registered as Sized and Iterable"
  1363. collections.abc.Sized.register(C)
  1364. collections.abc.Iterable.register(C)
  1365. def singledispatch_example1():
  1366. singledispatch = dispatch_on('obj')
  1367. @singledispatch
  1368. def g(obj):
  1369. raise NotImplementedError(type(g))
  1370. @g.register(collections.abc.Sized)
  1371. def g_sized(object):
  1372. return "sized"
  1373. @g.register(collections.abc.Iterable)
  1374. def g_iterable(object):
  1375. return "iterable"
  1376. g(C()) # RuntimeError: Ambiguous dispatch: Iterable or Sized?
  1377. def singledispatch_example2():
  1378. # adapted from functools.singledispatch test case
  1379. singledispatch = dispatch_on('arg')
  1380. class S(object):
  1381. pass
  1382. class V(c.Sized, S):
  1383. def __len__(self):
  1384. return 0
  1385. @singledispatch
  1386. def g(arg):
  1387. return "base"
  1388. @g.register(S)
  1389. def g_s(arg):
  1390. return "s"
  1391. @g.register(c.Container)
  1392. def g_container(arg):
  1393. return "container"
  1394. v = V()
  1395. assert g(v) == "s"
  1396. c.Container.register(V) # add c.Container to the virtual mro of V
  1397. assert g(v) == "s" # since the virtual mro is V, Sized, S, Container
  1398. return g, V
  1399. @decorator
  1400. def warn_slow(func, duration=0, *args, **kwargs):
  1401. t0 = time.time()
  1402. res = func(*args, **kwargs)
  1403. dt = time.time() - t0
  1404. if dt >= duration:
  1405. print('%s is slow' % func.__name__)
  1406. return res
  1407. @warn_slow() # with parens
  1408. def operation1():
  1409. """
  1410. >>> operation1()
  1411. operation1 is slow
  1412. """
  1413. time.sleep(.1)
  1414. @warn_slow # without parens
  1415. def operation2():
  1416. """
  1417. >>> operation2()
  1418. operation2 is slow
  1419. """
  1420. time.sleep(.1)
  1421. if __name__ == '__main__':
  1422. import doctest
  1423. doctest.testmod()