Commit 91cb298f authored by Vladimir Matveev's avatar Vladimir Matveev Committed by Tal Einat

bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)

parent 075b3c32
...@@ -954,7 +954,12 @@ def getsourcelines(object): ...@@ -954,7 +954,12 @@ def getsourcelines(object):
object = unwrap(object) object = unwrap(object)
lines, lnum = findsource(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 return lines, 0
else: else:
return getblock(lines[lnum:]), lnum + 1 return getblock(lines[lnum:]), lnum + 1
......
...@@ -74,3 +74,9 @@ class FesteringGob(MalodorousPervert, ParrotDroppings): ...@@ -74,3 +74,9 @@ class FesteringGob(MalodorousPervert, ParrotDroppings):
async def lobbest(grenade): async def lobbest(grenade):
pass pass
currentframe = inspect.currentframe()
try:
raise Exception()
except:
tb = sys.exc_info()[2]
...@@ -344,7 +344,7 @@ class GetSourceBase(unittest.TestCase): ...@@ -344,7 +344,7 @@ class GetSourceBase(unittest.TestCase):
def sourcerange(self, top, bottom): def sourcerange(self, top, bottom):
lines = self.source.split("\n") 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): def assertSourceEqual(self, obj, top, bottom):
self.assertEqual(inspect.getsource(obj), self.assertEqual(inspect.getsource(obj),
...@@ -527,6 +527,16 @@ class TestRetrievingSourceCode(GetSourceBase): ...@@ -527,6 +527,16 @@ class TestRetrievingSourceCode(GetSourceBase):
def test_getsource_on_code_object(self): def test_getsource_on_code_object(self):
self.assertSourceEqual(mod.eggs.__code__, 12, 18) 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): class TestDecorators(GetSourceBase):
fodderModule = mod2 fodderModule = mod2
...@@ -3870,7 +3880,7 @@ def test_main(): ...@@ -3870,7 +3880,7 @@ def test_main():
TestBoundArguments, TestSignaturePrivateHelpers, TestBoundArguments, TestSignaturePrivateHelpers,
TestSignatureDefinitions, TestIsDataDescriptor, TestSignatureDefinitions, TestIsDataDescriptor,
TestGetClosureVars, TestUnwrap, TestMain, TestReload, TestGetClosureVars, TestUnwrap, TestMain, TestReload,
TestGetCoroutineState TestGetCoroutineState, TestGettingSourceOfToplevelFrames
) )
if __name__ == "__main__": if __name__ == "__main__":
......
Fix inspect.getsourcelines for module level frames/tracebacks.
Patch by Vladimir Matveev.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment