You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.getsource() returns incorrect source lines at the module level #50949
Confirmed in trunk and py3k. Also affects inspect.getsourcelines.
jamesls commented on May 23, 2013
jameslsmannequin
Mannequin
More actions
I confirmed the issue in tip. One of the issues with the original patch is
that it modifies the tokeneater method used by getblock which won't work
if the first token is any of the special cased tokens in the original patch
('@', 'def', 'class'). I've added additional tests that show where the
original patch fails.
An alternative approach is to check in getsourcelines whether or not we're
dealing with a traceback or frame object in the module scope, and if so,
return the lines of the entire module. I've attached an updated patch
that implements this along with additional tests.
changed the title [-]inspect.getsource() returns incorrect source lines[/-][+]inspect.getsource() returns incorrect source lines at the module level[/+]on Aug 17, 2018
New changeset 91cb298 by Tal Einat (Vladimir Matveev) in branch 'master': bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) 91cb298
New changeset 0e707b4 by Miss Islington (bot) in branch '3.6': bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) 0e707b4
New changeset 3e6020c by Miss Islington (bot) in branch '3.7': bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) 3e6020c
Activity
ggenellina commentedon Aug 14, 2009
inspect.getsource(obj) returns incorrect results when obj is a
traceback or frame object outside any function (that is, at the module
level).
This demo script shows the problem. The correct output should contain
all source lines in the module, but it only returns the first two:
D:\temp>type show_inspect_bug.py
def foo(x):
return y + z
D:\temp>python show_inspect_bug.py
def foo(x):
return y + z
The attached patch fixes the problem and adds some missing test cases.
Originally reported by Juanjo Conti at the local Python group.
devdanzin commentedon Apr 8, 2010
Confirmed in trunk and py3k. Also affects inspect.getsourcelines.
jamesls commentedon May 23, 2013
I confirmed the issue in tip. One of the issues with the original patch is
that it modifies the tokeneater method used by getblock which won't work
if the first token is any of the special cased tokens in the original patch
('@', 'def', 'class'). I've added additional tests that show where the
original patch fails.
An alternative approach is to check in getsourcelines whether or not we're
dealing with a traceback or frame object in the module scope, and if so,
return the lines of the entire module. I've attached an updated patch
that implements this along with additional tests.
[-]inspect.getsource() returns incorrect source lines[/-][+]inspect.getsource() returns incorrect source lines at the module level[/+]taleinat commentedon Aug 24, 2018
New changeset 91cb298 by Tal Einat (Vladimir Matveev) in branch 'master':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
91cb298
miss-islington commentedon Aug 24, 2018
New changeset 0e707b4 by Miss Islington (bot) in branch '3.6':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
0e707b4
miss-islington commentedon Aug 24, 2018
New changeset 3e6020c by Miss Islington (bot) in branch '3.7':
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
3e6020c
taleinat commentedon Aug 26, 2018
New changeset 491740f by Tal Einat in branch '2.7':
[2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
491740f
taleinat commentedon Aug 26, 2018
Thanks for reporting this, Gabriel!
Thanks for the PR, Vladimir!