Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks #8864

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 4 commits into from
Aug 24, 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
7 changes: 6 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,12 @@ def getsourcelines(object):
object = unwrap(object)
lines, lnum = findsource(object)

if ismodule(object):
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
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 @@ -74,3 +74,9 @@ def contradiction(self):

async def lobbest(grenade):
pass

currentframe = inspect.currentframe()
try:
raise Exception()
except:
tb = sys.exc_info()[2]
14 changes: 12 additions & 2 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def setUp(self):

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 @@ -527,6 +527,16 @@ def monkey(filename, module_globals=None):
def test_getsource_on_code_object(self):
self.assertSourceEqual(mod.eggs.__code__, 12, 18)

class TestGettingSourceOfToplevelFrames(GetSourceBase):
fodderModule = 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):
fodderModule = mod2

Expand Down Expand Up @@ -3870,7 +3880,7 @@ def test_main():
TestBoundArguments, TestSignaturePrivateHelpers,
TestSignatureDefinitions, TestIsDataDescriptor,
TestGetClosureVars, TestUnwrap, TestMain, TestReload,
TestGetCoroutineState
TestGetCoroutineState, TestGettingSourceOfToplevelFrames
)

if __name__ == "__main__":
Expand Down
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.