[ python-Bugs-1764286 ] inspect.getsource does not work with decorated functions
SourceForge.net
noreply at sourceforge.net
Tue Jul 31 12:16:40 CEST 2007
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-SourceForge-Tracker-unixname: python
X-SourceForge-Tracker-trackerid: 105470
X-SourceForge-Tracker-itemid: 1764286
X-SourceForge-Tracker-itemstatus: Open
X-SourceForge-Tracker-itemassignee: nobody
X-SourceForge-Tracker-itemupdate-reason: Tracker Item Submitted
X-SourceForge-Tracker-itemupdate-username: Item Submitter
Bugs item #1764286, was opened at 2007-07-31 06:16
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1764286&group_id=5470
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Michele Simionato (michele_s)
Assigned to: Nobody/Anonymous (nobody)
Summary: inspect.getsource does not work with decorated functions
Initial Comment:
Here is the issue:
$ cat example.py
import functools # I am using Python 2.5
def identity_dec(func):
def wrapper(*args, **kw):
return func(*args, **kw)
return functools.update_wrapper(wrapper, func)
@identity_dec
def example():
pass
>>> import inspect
>>> from example import example
>>> print inspect.getsource(example)
def wrapper(*args, **kw):
return func(*args, **kw)
You get the source code of the closure
and not what would be more meaningful, i.e.
the string
"""
@identity_dec
def example():
pass
"""
Of course one could argue that this is not a bug
(in a sense the inspect module is doing the right
thing) but still it is giving information which
is not very useful.
Looking at the guts of inspect.getsource, one discovers the origin
of the problem: inspect.findsource is looking at the attribute
.co_firstlineno of the decorated function code object.
Unfortunately .co_firstlineno is a read-only attribute, otherwise
it would be possibile to change functools.update_wrapper to set it to
the correct line number (i.e. the line where
the undecorated function is defined, -1). So
I don't think you can fix this in current
Python, but it is something to keep in mind for Python 2.6 and 3.0. It should also manage classmethods/
staticmethods and other decorators not implemented
as closures.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1764286&group_id=5470
More information about the Python-bugs-list
mailing list