From 0d607186660a8de51d9ab8eb0ab6fc821a5e6568 Mon Sep 17 00:00:00 2001 From: Isaac Haro Date: Thu, 19 Feb 2026 16:48:54 -0500 Subject: [PATCH] Add regression test for recursionindex with equal code objects --- testing/code/test_excinfo.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/testing/code/test_excinfo.py b/testing/code/test_excinfo.py index 70499fec893..a8dda85f459 100644 --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -279,6 +279,31 @@ def f(n: int) -> None: recindex = traceback.recursionindex() assert recindex is None + def test_traceback_no_false_recursion_for_equal_code_objects(self) -> None: + import types + + def template(n: int) -> int: + return next_func(n) # type: ignore[name-defined] + + f = template + # Use a distinct code object with equal contents to avoid false recursion detection. + g = types.FunctionType(template.__code__.replace(), globals(), "g") + + def leaf(n: int) -> int: + raise ValueError("boom") + + def call_g(n: int) -> int: + globals()["next_func"] = leaf + return g(n) + + globals()["next_func"] = call_g + try: + excinfo = pytest.raises(ValueError, f, 1) + finally: + globals().pop("next_func", None) + + assert excinfo.traceback.recursionindex() is None + def test_traceback_messy_recursion(self): # XXX: simplified locally testable version decorator = pytest.importorskip("decorator").decorator