From 3b96cc92e40e353c3af959f0587d54f51faf1cb0 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 10:32:44 +0530 Subject: [PATCH 1/8] Fix: guard added to inspect.getframeinfo to prevent TypeError when lineno is None --- Lib/inspect.py | 3 +++ Lib/test/test_inspect/test_inspect.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) 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..6271443de30455 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -28,6 +28,8 @@ import unittest.mock import warnings import weakref +import inspect +import sys try: @@ -6250,3 +6252,18 @@ def f(): if __name__ == "__main__": unittest.main() + +def test_getframeinfo_with_exceptiongroup(): + code = """ +try: + 1/0 +except* Exception: + raise +""" + try: + exec(code) + except: + tb = sys.exc_info()[2] + while tb: + info = inspect.getframeinfo(tb.tb_frame) + tb = tb.tb_next From 92e87c2107af23653e0609c1acd58ed26dcd7f19 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 10:49:48 +0530 Subject: [PATCH 2/8] Add NEWS entry for bpo-139531 fix --- Misc/NEWS.d/next/139531.bpo | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/139531.bpo diff --git a/Misc/NEWS.d/next/139531.bpo b/Misc/NEWS.d/next/139531.bpo new file mode 100644 index 00000000000000..f453df43dc97bf --- /dev/null +++ b/Misc/NEWS.d/next/139531.bpo @@ -0,0 +1 @@ +bpo-139531: Fixed TypeError in inspect.getframeinfo when lineno is None in exception groups. \ No newline at end of file From 24361e41bae32ef0ed4c79369dc9904268caade6 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 11:52:56 +0530 Subject: [PATCH 3/8] Add NEWS entry for bpo-139531 fix --- Misc/NEWS.d/next/139531.bpo | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/139531.bpo diff --git a/Misc/NEWS.d/next/139531.bpo b/Misc/NEWS.d/next/139531.bpo deleted file mode 100644 index f453df43dc97bf..00000000000000 --- a/Misc/NEWS.d/next/139531.bpo +++ /dev/null @@ -1 +0,0 @@ -bpo-139531: Fixed TypeError in inspect.getframeinfo when lineno is None in exception groups. \ No newline at end of file From e74e21061a8248daa8d86cfabd6fa300d50a4b85 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 12:43:28 +0530 Subject: [PATCH 4/8] Add NEWS entry for bpo-139531 fix --- Lib/test/test_inspect/test_inspect.py | 35 +++++++++++++++------------ Misc/NEWS.d/next/139531.bpo | 1 + 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 Misc/NEWS.d/next/139531.bpo diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 6271443de30455..4b223a94ff2ed8 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -30,6 +30,7 @@ import weakref import inspect import sys +import unittest try: @@ -6247,23 +6248,27 @@ def f(): expected = "The source is: <<>>" self.assertIn(expected, output) - - - -if __name__ == "__main__": - unittest.main() - -def test_getframeinfo_with_exceptiongroup(): - code = """ +class TestInspect(unittest.TestCase): + def test_getframeinfo_with_exceptiongroup(self): + code = """ try: 1/0 except* Exception: raise """ - try: - exec(code) - except: - tb = sys.exc_info()[2] - while tb: - info = inspect.getframeinfo(tb.tb_frame) - tb = tb.tb_next + 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() + + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/Misc/NEWS.d/next/139531.bpo b/Misc/NEWS.d/next/139531.bpo new file mode 100644 index 00000000000000..f453df43dc97bf --- /dev/null +++ b/Misc/NEWS.d/next/139531.bpo @@ -0,0 +1 @@ +bpo-139531: Fixed TypeError in inspect.getframeinfo when lineno is None in exception groups. \ No newline at end of file From fb3b712575347c63e9516e7422dd128709256f92 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 12:48:39 +0530 Subject: [PATCH 5/8] Add blank line at the end --- Lib/test/test_inspect/test_inspect.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 4b223a94ff2ed8..74d65807072f4c 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -6271,4 +6271,5 @@ def test_getframeinfo_with_exceptiongroup(self): if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main() + \ No newline at end of file From d33312354724a8246f4ef172a6df95bac570a446 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 07:24:37 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tests/2025-10-03-07-24-36.gh-issue-139531.jOs0fO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2025-10-03-07-24-36.gh-issue-139531.jOs0fO.rst 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 From 574461d0fcdd148fe2d3da8ac7e66a5464951b92 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 12:58:10 +0530 Subject: [PATCH 7/8] Fix lint, trailing whitespace, and end-of-file issues --- Lib/test/test_inspect/test_inspect.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 74d65807072f4c..518ad0e3786546 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -6267,9 +6267,3 @@ def test_getframeinfo_with_exceptiongroup(self): if __name__ == "__main__": unittest.main() - - - -if __name__ == "__main__": - unittest.main() - \ No newline at end of file From c1a282bae15be1102813c9a60e1fb8cc9aaf25c0 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Fri, 3 Oct 2025 13:10:27 +0530 Subject: [PATCH 8/8] Fix getframeinfo TypeError, add NEWS entry --- Lib/test/test_inspect/test_inspect.py | 2 -- Misc/NEWS.d/next/139531.bpo | 2 +- Misc/mypy/_colorize.py | 2 +- Misc/mypy/token.py | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_inspect/test_inspect.py b/Lib/test/test_inspect/test_inspect.py index 518ad0e3786546..8e3bd8e9f91bcb 100644 --- a/Lib/test/test_inspect/test_inspect.py +++ b/Lib/test/test_inspect/test_inspect.py @@ -28,8 +28,6 @@ import unittest.mock import warnings import weakref -import inspect -import sys import unittest diff --git a/Misc/NEWS.d/next/139531.bpo b/Misc/NEWS.d/next/139531.bpo index f453df43dc97bf..465c634cc5da61 100644 --- a/Misc/NEWS.d/next/139531.bpo +++ b/Misc/NEWS.d/next/139531.bpo @@ -1 +1 @@ -bpo-139531: Fixed TypeError in inspect.getframeinfo when lineno is None in exception groups. \ No newline at end of file +Fix TypeError in inspect.getframeinfo when lineno is None (gh-139531, #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