summaryrefslogtreecommitdiff
path: root/decorator/documentation.py
diff options
context:
space:
mode:
authormichele.simionato <devnull@localhost>2009-02-16 06:14:07 +0000
committermichele.simionato <devnull@localhost>2009-02-16 06:14:07 +0000
commit051d16a19789582deae94dbfeff783bdf4ecba96 (patch)
tree1ef9e8e35ff5cab676354822693be60f698f595b /decorator/documentation.py
parentd0914b1725039946e4e58609c94cd6ce6290a41c (diff)
downloadmicheles-051d16a19789582deae94dbfeff783bdf4ecba96.tar.gz
Version 3.0.1 released
Diffstat (limited to 'decorator/documentation.py')
-rw-r--r--decorator/documentation.py51
1 files changed, 41 insertions, 10 deletions
diff --git a/decorator/documentation.py b/decorator/documentation.py
index 82b4fa4..665e45c 100644
--- a/decorator/documentation.py
+++ b/decorator/documentation.py
@@ -491,7 +491,7 @@ $$identity_dec
@identity_dec
def example(): pass
- >>> print getsource(example)
+ >>> print inspect.getsource(example)
def wrapper(*args, **kw):
return func(*args, **kw)
<BLANKLINE>
@@ -506,7 +506,7 @@ undecorated function:
.. code-block:: python
- >>> print getsource(factorial.undecorated)
+ >>> print inspect.getsource(factorial.undecorated)
@tail_recursive
def factorial(n, acc=1):
"The good old factorial"
@@ -625,13 +625,13 @@ function is decorated the traceback will be longer:
.. code-block:: python
>>> f()
- calling f with args (), {}
Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "<string>", line 2, in f
- File "documentation.py", line 799, in _trace
- return f(*args, **kw)
- File "<stdin>", line 3, in f
+ ...
+ File "<string>", line 2, in f
+ File "<doctest __main__[18]>", line 4, in trace
+ return f(*args, **kw)
+ File "<doctest __main__[47]>", line 3, in f
+ 1/0
ZeroDivisionError: integer division or modulo by zero
You see here the inner call to the decorator ``trace``, which calls
@@ -656,8 +656,39 @@ Actually, this is one of the main reasons why I am releasing version 3.0.
In the present implementation, decorators generated by ``decorator``
can only be used on user-defined Python functions or methods, not on generic
callable objects, nor on built-in functions, due to limitations of the
-``inspect`` module in the standard library.
-
+``inspect`` module in the standard library. Moreover, notice
+that you can decorate a method, but only before if becomes a bound or unbound
+method, i.e. inside the class.
+Here is an example of valid decoration:
+
+.. code-block:: python
+
+ >>> class C(object):
+ ... @trace
+ ... def meth(self):
+ ... pass
+
+Here is an example of invalid decoration, when the decorator in
+called too late:
+
+.. code-block:: python
+
+ >>> class C(object):
+ ... def meth(self):
+ ... pass
+ ...
+ >>> trace(C.meth)
+ Traceback (most recent call last):
+ ...
+ TypeError: You are decorating a non function: <unbound method C.meth>
+
+The solution is to extract the inner function from the unbound method:
+
+.. code-block:: python
+
+ >>> trace(C.meth.im_func) # doctest: +ELLIPSIS
+ <function meth at 0x...>
+
There is a restriction on the names of the arguments: for instance,
if try to call an argument ``_call_`` or ``_func_``
you will get a ``NameError``: