co_firstlineno on decorated functions

Eli Bendersky eliben at
Mon Aug 2 04:15:55 CEST 2010


I've been tinkering with __code__.co_firstlineno for testing the
module (Python Issue 9315), and ran into an interesting problem. Consider
this code:

def dummydecorator(f):
    return f

def foo(a):
    return a

def bar(a):
    return a

if __name__ == "__main__":
    print foo.__code__.co_firstlineno
    print bar.__code__.co_firstlineno

The first print out correctly specifies the line "def foo" is in. However,
the second one points to the line with "@dummydecorator" instead of "def
bar". [Python 2.6]

The side-effects of this behavior can be easily seen in the output of
modules like trace and profile. Would you say it's normal, or could this be
considered a bug?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list