]> granicus.if.org Git - python/commitdiff
bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH-8864)
authorVladimir Matveev <v2matveev@outlook.com>
Fri, 24 Aug 2018 14:18:00 +0000 (07:18 -0700)
committerTal Einat <taleinat+github@gmail.com>
Fri, 24 Aug 2018 14:18:00 +0000 (17:18 +0300)
Lib/inspect.py
Lib/test/inspect_fodder.py
Lib/test/test_inspect.py
Misc/NEWS.d/next/Library/2018-08-22-17-43-52.bpo-6700.hp7C4B.rst [new file with mode: 0644]

index 717518614fc6d7488962b245f818b944742a2e7e..e799a83a74046fa59df71789611f5610e3e9fbe2 100644 (file)
@@ -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
index 711badad849211f6b390fb68c8d7c3d9acd157a4..ff3f0e4b73b9ad644fed7ba42951d2850d6d6e8c 100644 (file)
@@ -74,3 +74,9 @@ class FesteringGob(MalodorousPervert, ParrotDroppings):
 
 async def lobbest(grenade):
     pass
+
+currentframe = inspect.currentframe()
+try:
+    raise Exception()
+except:
+    tb = sys.exc_info()[2]
index cda8d5cf73ef11230bd7726a6941b36122089d78..e523dd40636ce00735f44ee56653ea0c06f49246 100644 (file)
@@ -344,7 +344,7 @@ class GetSourceBase(unittest.TestCase):
 
     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),
@@ -527,6 +527,16 @@ class TestRetrievingSourceCode(GetSourceBase):
     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
 
@@ -3870,7 +3880,7 @@ def test_main():
         TestBoundArguments, TestSignaturePrivateHelpers,
         TestSignatureDefinitions, TestIsDataDescriptor,
         TestGetClosureVars, TestUnwrap, TestMain, TestReload,
-        TestGetCoroutineState
+        TestGetCoroutineState, TestGettingSourceOfToplevelFrames
     )
 
 if __name__ == "__main__":
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 (file)
index 0000000..d95c737
--- /dev/null
@@ -0,0 +1,2 @@
+Fix inspect.getsourcelines for module level frames/tracebacks.
+Patch by Vladimir Matveev.