diff --git a/Lib/inspect.py b/Lib/inspect.py index d7814bfeb2b885..51b33303ff5825 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1582,6 +1582,9 @@ def getframeinfo(frame, context=1): lineno = positions[0] + if lineno is None: + lineno = 1 + if not isframe(frame): raise TypeError('{!r} is not a frame or traceback object'.format(frame)) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index c8c1a5226114b9..8e3bd8e9f91bcb 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -28,6 +28,7 @@ import unittest.mock import warnings import weakref +import unittest try: @@ -6245,8 +6246,22 @@ def f(): expected = "The source is: <<>>" self.assertIn(expected, output) - - +class TestInspect(unittest.TestCase): + 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 if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/139531.bpo b/Misc/NEWS.d/next/139531.bpo new file mode 100644 index 00000000000000..465c634cc5da61 --- /dev/null +++ b/Misc/NEWS.d/next/139531.bpo @@ -0,0 +1 @@ +Fix TypeError in inspect.getframeinfo when lineno is None (gh-139531, #139535). diff --git a/Misc/NEWS.d/next/Tests/2025-10-03-07-24-36.gh-issue-139531.jOs0fO.rst b/Misc/NEWS.d/next/Tests/2025-10-03-07-24-36.gh-issue-139531.jOs0fO.rst new file mode 100644 index 00000000000000..43427e20121594 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2025-10-03-07-24-36.gh-issue-139531.jOs0fO.rst @@ -0,0 +1 @@ +gh-139531: Fix TypeError in inspect.getframeinfo when lineno is None #139535 diff --git a/Misc/mypy/_colorize.py b/Misc/mypy/_colorize.py index 9b7304769ec30b..5d2723595daa8d 120000 --- a/Misc/mypy/_colorize.py +++ b/Misc/mypy/_colorize.py @@ -1 +1 @@ -../../Lib/_colorize.py \ No newline at end of file +../../Lib/_colorize.py diff --git a/Misc/mypy/token.py b/Misc/mypy/token.py index 0a39f726dda1aa..9f38e9c897fb0c 120000 --- a/Misc/mypy/token.py +++ b/Misc/mypy/token.py @@ -1 +1 @@ -../../Lib/token.py \ No newline at end of file +../../Lib/token.py