Skip to content
3 changes: 3 additions & 0 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,9 @@ def getframeinfo(frame, context=1):

lineno = positions[0]

if lineno is None:
lineno = 1
Comment on lines +1585 to +1586
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? it might happen that lineno is None and should remains so.


if not isframe(frame):
raise TypeError('{!r} is not a frame or traceback object'.format(frame))

Expand Down
19 changes: 17 additions & 2 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import unittest.mock
import warnings
import weakref
import unittest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why reimporting this?



try:
Expand Down Expand Up @@ -6245,8 +6246,22 @@ def f():
expected = "The source is: <<<def f():\n print(0)\n return 1 + 2\n>>>"
self.assertIn(expected, output)



class TestInspect(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a new class?

def test_getframeinfo_with_exceptiongroup(self):
code = """
try:
1/0
except* Exception:
raise
"""
with self.assertRaises(TypeError): # expect the bug to raise TypeError
try:
exec(code)
except:
tb = sys.exc_info()[2]
while tb:
info = inspect.getframeinfo(tb.tb_frame)
tb = tb.tb_next
Comment on lines +6257 to +6264
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this test? we should fix the issue, not show that there is one.


if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions Misc/NEWS.d/next/139531.bpo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix TypeError in inspect.getframeinfo when lineno is None (gh-139531, #139535).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gh-139531: Fix TypeError in inspect.getframeinfo when lineno is None #139535
2 changes: 1 addition & 1 deletion Misc/mypy/_colorize.py
2 changes: 1 addition & 1 deletion Misc/mypy/token.py
Loading