]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[2.7] bpo-6700: Fix inspect.getsourcelines for module level frames/tracebacks (GH...
authorTal Einat <taleinat+github@gmail.com>
Sun, 26 Aug 2018 08:44:53 +0000 (11:44 +0300)
committerGitHub <noreply@github.com>
Sun, 26 Aug 2018 08:44:53 +0000 (11:44 +0300)
(cherry picked from commit 91cb298f811961277fd4cc4a32211899d48bedcb)

Co-authored-by: Vladimir Matveev <v2matveev@outlook.com>
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 0a6cfd795184d507548ad89ccfb72bb0f3904093..cbced17373042f599751249ecd9db023c3b74195 100644 (file)
@@ -688,8 +688,15 @@ def getsourcelines(object):
     raised if the source code cannot be retrieved."""
     lines, lnum = findsource(object)
 
-    if ismodule(object): return lines, 0
-    else: return getblock(lines[lnum:]), lnum + 1
+    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
 
 def getsource(object):
     """Return the text of the source code for an object.
index 5c87ae6f828be8ddb089c64bb49b27eedf092952..548765c7975b5d50b64d5eb714360a4005cac234 100644 (file)
@@ -56,3 +56,9 @@ class ParrotDroppings:
 
 class FesteringGob(MalodorousPervert, ParrotDroppings):
     pass
+
+currentframe = inspect.currentframe()
+try:
+    raise Exception()
+except:
+    tb = sys.exc_info()[2]
index 7d09c6fa0202f87cc9559d2b1debc7015cc83597..5063a30d2290c00fedfcd4da7cd0ae9180f04f5f 100644 (file)
@@ -209,7 +209,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),
@@ -331,6 +331,16 @@ class TestRetrievingSourceCode(GetSourceBase):
         finally:
             linecache.getlines = getlines
 
+class TestGettingSourceOfToplevelFrames(GetSourceBase):
+    fodderFile = 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):
     fodderFile = mod2
 
@@ -896,7 +906,8 @@ def test_main():
         TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
         TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
         TestGetcallargsFunctions, TestGetcallargsFunctionsCellVars,
-        TestGetcallargsMethods, TestGetcallargsUnboundMethods)
+        TestGetcallargsMethods, TestGetcallargsUnboundMethods,
+        TestGettingSourceOfToplevelFrames)
 
 if __name__ == "__main__":
     test_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.