Skip to content

[2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864) #8900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,15 @@ def getsourcelines(object):
raised if the source code cannot be retrieved."""
lines, lnum = findsource(object)

if ismodule(object): return lines, 0
else: return getblock(lines[lnum:]), lnum + 1
if istraceback(object):
object = object.tb_frame

# for module or frame that corresponds to module, return all source lines
if (ismodule(object) or
(isframe(object) and object.f_code.co_name == "<module>")):
return lines, 0
else:
return getblock(lines[lnum:]), lnum + 1

def getsource(object):
"""Return the text of the source code for an object.
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/inspect_fodder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ class ParrotDroppings:

class FesteringGob(MalodorousPervert, ParrotDroppings):
pass

currentframe = inspect.currentframe()
try:
raise Exception()
except:
tb = sys.exc_info()[2]
15 changes: 13 additions & 2 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __init__(self, *args, **kwargs):

def sourcerange(self, top, bottom):
lines = self.source.split("\n")
return "\n".join(lines[top-1:bottom]) + "\n"
return "\n".join(lines[top-1:bottom]) + ("\n" if bottom else "")

def assertSourceEqual(self, obj, top, bottom):
self.assertEqual(inspect.getsource(obj),
Expand Down Expand Up @@ -331,6 +331,16 @@ def monkey(filename, module_globals=None):
finally:
linecache.getlines = getlines

class TestGettingSourceOfToplevelFrames(GetSourceBase):
fodderFile = mod

def test_range_toplevel_frame(self):
self.maxDiff = None
self.assertSourceEqual(mod.currentframe, 1, None)

def test_range_traceback_toplevel_frame(self):
self.assertSourceEqual(mod.tb, 1, None)

class TestDecorators(GetSourceBase):
fodderFile = mod2

Expand Down Expand Up @@ -896,7 +906,8 @@ def test_main():
TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
TestGetcallargsFunctions, TestGetcallargsFunctionsCellVars,
TestGetcallargsMethods, TestGetcallargsUnboundMethods)
TestGetcallargsMethods, TestGetcallargsUnboundMethods,
TestGettingSourceOfToplevelFrames)

if __name__ == "__main__":
test_main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix inspect.getsourcelines for module level frames/tracebacks.
Patch by Vladimir Matveev.