]> granicus.if.org Git - python/commitdiff
Issue #15812: inspect.getframeinfo() now correctly shows the first line of a context
authorBerker Peksag <berker.peksag@gmail.com>
Mon, 2 Jan 2017 03:57:43 +0000 (06:57 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Mon, 2 Jan 2017 03:57:43 +0000 (06:57 +0300)
Patch by Sam Breese.

Lib/inspect.py
Lib/test/test_inspect.py
Misc/NEWS

index e6dae1e0489dd67f774bd88cf56c94a1b720ed1d..6b9e0b00b543da23e8a9a9a78f129ac5a57134f2 100644 (file)
@@ -1416,7 +1416,7 @@ def getframeinfo(frame, context=1):
         except OSError:
             lines = index = None
         else:
-            start = max(start, 1)
+            start = max(start, 0)
             start = max(0, min(start, len(lines) - context))
             lines = lines[start:start+context]
             index = lineno - 1 - start
index 671e05a7b5b73b988c2d5ec85c44f711bc08ae49..d33de9e5bc3f78f1adc805db1f5712f35ff3c8a8 100644 (file)
@@ -391,6 +391,11 @@ class TestRetrievingSourceCode(GetSourceBase):
         # Check filename override
         self.assertEqual(inspect.getmodule(None, modfile), mod)
 
+    def test_getframeinfo_get_first_line(self):
+        frame_info = inspect.getframeinfo(self.fodderModule.fr, 50)
+        self.assertEqual(frame_info.code_context[0], "# line 1\n")
+        self.assertEqual(frame_info.code_context[1], "'A module docstring.'\n")
+
     def test_getsource(self):
         self.assertSourceEqual(git.abuse, 29, 39)
         self.assertSourceEqual(mod.StupidGit, 21, 51)
index ad67b1da62e63dc600843ca68e797b5a74413b50..3c79cd62ccc6ecfe5e884de928231cfb469c56ed 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -140,6 +140,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #15812: inspect.getframeinfo() now correctly shows the first line of
+  a context.  Patch by Sam Breese.
+
 - Issue #29094: Offsets in a ZIP file created with extern file object and modes
   "w" and "x" now are relative to the start of the file.