Issue #15812: inspect.getframeinfo() now correctly shows the first line of a context
Patch by Sam Breese.
diff --git a/Lib/inspect.py b/Lib/inspect.py
index e6dae1e..6b9e0b0 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1416,7 +1416,7 @@
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
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 671e05a..d33de9e 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -391,6 +391,11 @@
# 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)
diff --git a/Misc/NEWS b/Misc/NEWS
index ad67b1d..3c79cd6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -140,6 +140,9 @@
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.