The problem of anonymity with decorators

Michele Simionato michele.simionato at gmail.com
Tue Feb 7 11:40:48 EST 2006


Using my decorator module
(http://www.phyast.pitt.edu/~micheles/python/documentation.html,
http://www.phyast.pitt.edu/~micheles/python/decorator.zip) you solve
the problem of the
name, but the traceback is still not perfect. You get:

# dec_traceback.py
from ms.decorator import decorator

@decorator
def int_result (fun, *largs, **kwargs) :
    result = fun(*largs, **kwargs)
    if not isinstance(result, int) :
        raise TypeError, "should return int"
    return result

@int_result
def add (a, b) :
    return a+b

print add(1, 2)
print add("foo", "bar")

# end dec_traceback.py

$ python dec_traceback.py
3
Traceback (most recent call last):
  File "dec_traceback.py", line 15, in ?
    print add("foo", "bar")
  File "<string>", line 2, in add
  File "dec_traceback.py", line 7, in int_result
    raise TypeError, "should return int"
TypeError: should return int

The reason of <string> is that the module internally uses 'exec'. There
should be a way around
that, anyway, but I have no time to check it right now.
Still, the module may be of inspiration to you.

        Michele Simionato




More information about the Python-list mailing list