Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 7 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 6 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 14 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.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.