From 8d80db70f79bc313df1af9b75bd2d5b987e18771 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 19 Nov 2026 16:30:39 +0000 Subject: [PATCH] [mypyc] Fix flaky test The test case `testTracebackCodeObjectReuse` added in #22476 randomly failed when there was a hash collision. Use just one entry so that there can't be a collision. Run the updated test case 2000 times and it always passed locally, whereas the original test failed 4 times out of 2000. --- mypyc/test-data/run-exceptions.test | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/mypyc/test-data/run-exceptions.test b/mypyc/test-data/run-exceptions.test index aaf853ecd778..2fde364d9a2d 100644 --- a/mypyc/test-data/run-exceptions.test +++ b/mypyc/test-data/run-exceptions.test @@ -759,6 +759,16 @@ def catch(n: int) -> List[Any]: tbs.append(exc.__traceback__) return tbs +def catch_direct(n: int) -> List[Any]: + tbs = [] + for i in range(n): + try: + raise ValueError(str(i)) + except ValueError as e: + exc: Any = e + tbs.append(exc.__traceback__) + return tbs + def raise_and_catch(n: int) -> int: count = 0 for i in range(n): @@ -780,9 +790,17 @@ def test_traceback_entries() -> None: tbs = catch(2) assert entries(tbs[0]) == [(True, "catch", 11), (True, "fail", 5)] assert entries(tbs[1]) == entries(tbs[0]) - # Code objects of traceback entries are cached + +def test_code_object_reuse() -> None: + # Code objects of traceback entries are cached. The cache is direct-mapped + # and the slot depends on string addresses, so two distinct entries (such + # as those in catch()) can occasionally map to the same slot and evict each + # other. Use a single traceback entry per iteration so that the result + # doesn't depend on where the module is loaded. + tbs = catch_direct(2) + assert entries(tbs[0]) == [(True, "catch_direct", 21)] + assert tbs[0].tb_next is None assert tbs[0].tb_frame.f_code is tbs[1].tb_frame.f_code - assert tbs[0].tb_next.tb_frame.f_code is tbs[1].tb_next.tb_frame.f_code def count_code_objects() -> int: return sum(1 for o in gc.get_objects() if type(o).__name__ == "code")