From 2c9a55e2cb4ba1a7a0f4d6b5731e19e3a12fb82c Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 22 Aug 2018 17:12:24 -0700 Subject: [PATCH 1/4] bpo-6700: fix inspect.getsourcelines for module level frames/tracebacks --- Lib/inspect.py | 7 ++++++- Lib/test/inspect_fodder.py | 6 ++++++ Lib/test/test_inspect.py | 11 ++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 717518614fc6d7..97ead6cc23ef3d 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -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 correspond to module return all source lines + if (ismodule(object) or + (isframe(object) and object.f_code.co_name == "")): return lines, 0 else: return getblock(lines[lnum:]), lnum + 1 diff --git a/Lib/test/inspect_fodder.py b/Lib/test/inspect_fodder.py index 711badad849211..ff3f0e4b73b9ad 100644 --- a/Lib/test/inspect_fodder.py +++ b/Lib/test/inspect_fodder.py @@ -74,3 +74,9 @@ def contradiction(self): async def lobbest(grenade): pass + +currentframe = inspect.currentframe() +try: + raise Exception() +except: + tb = sys.exc_info()[2] diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index cda8d5cf73ef11..13c594dbcf5364 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -527,6 +527,15 @@ 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.assertSourceEqual(mod.currentframe, 1, 82) + + def test_range_traceback_toplevel_frame(self): + self.assertSourceEqual(mod.tb, 1, 82) + class TestDecorators(GetSourceBase): fodderModule = mod2 @@ -3870,7 +3879,7 @@ def test_main(): TestBoundArguments, TestSignaturePrivateHelpers, TestSignatureDefinitions, TestIsDataDescriptor, TestGetClosureVars, TestUnwrap, TestMain, TestReload, - TestGetCoroutineState + TestGetCoroutineState, TestGettingSourceOfToplevelFrames ) if __name__ == "__main__": From b2b7917544941a9623e93122c5bfa8a4e138ccd9 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 22 Aug 2018 17:44:46 -0700 Subject: [PATCH 2/4] add entry to Misc/NEWS.d --- Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst diff --git a/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst new file mode 100644 index 00000000000000..30fcb9e925e7a0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst @@ -0,0 +1 @@ +Fix inspect.getsourcelines for module level frames/tracebacks From 0ceff9def2a3452641e19329933d6c9a137d9792 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 23 Aug 2018 10:01:15 -0700 Subject: [PATCH 3/4] pass None as generic pointer to last line --- Lib/inspect.py | 2 +- Lib/test/test_inspect.py | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 97ead6cc23ef3d..e799a83a74046f 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -957,7 +957,7 @@ def getsourcelines(object): if istraceback(object): object = object.tb_frame - # for module or frame that correspond to module return all source lines + # for module or frame that corresponds to module, return all source lines if (ismodule(object) or (isframe(object) and object.f_code.co_name == "")): return lines, 0 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 13c594dbcf5364..e523dd40636ce0 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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), @@ -531,10 +531,11 @@ class TestGettingSourceOfToplevelFrames(GetSourceBase): fodderModule = mod def test_range_toplevel_frame(self): - self.assertSourceEqual(mod.currentframe, 1, 82) + self.maxDiff = None + self.assertSourceEqual(mod.currentframe, 1, None) def test_range_traceback_toplevel_frame(self): - self.assertSourceEqual(mod.tb, 1, 82) + self.assertSourceEqual(mod.tb, 1, None) class TestDecorators(GetSourceBase): fodderModule = mod2 From 28bd987815f9c95f1e7933dfbdd894e414a46603 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Fri, 24 Aug 2018 14:23:13 +0300 Subject: [PATCH 4/4] add mention of contributor in NEWS --- .../next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst index 30fcb9e925e7a0..d95c737a2860d4 100644 --- a/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst +++ b/Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst @@ -1 +1,2 @@ -Fix inspect.getsourcelines for module level frames/tracebacks +Fix inspect.getsourcelines for module level frames/tracebacks. +Patch by Vladimir Matveev.