From 3162b3e09d13069b54346e137e735c21a08febd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 28 Sep 2025 14:35:48 +0200 Subject: [PATCH 1/3] fix `_CALL_LEN` JIT tests for tuples --- Lib/test/test_capi/test_opt.py | 14 +++++++++++--- Modules/_testinternalcapi.c | 9 +++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index c3fed50cee9736..675839cc561aa7 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -15,7 +15,7 @@ _testinternalcapi = import_helper.import_module("_testinternalcapi") -from _testinternalcapi import TIER2_THRESHOLD +from _testinternalcapi import _PY_NSMALLPOSINTS, TIER2_THRESHOLD #For test of issue 136154 GLOBAL_136154 = 42 @@ -2093,6 +2093,10 @@ def testfunc(n): self.assertNotIn("_GUARD_TOS_INT", uops) def test_call_len_known_length_small_int(self): + # Make sure that len(t) is optimized for a tuple of length 5. + # See https://github.com/python/cpython/issues/139393. + self.assertGreater(_PY_NSMALLPOSINTS, 5) + def testfunc(n): x = 0 for _ in range(n): @@ -2113,13 +2117,17 @@ def testfunc(n): self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops) def test_call_len_known_length(self): + # Make sure that len(t) is optimized for a tuple of length 2048. + # See https://github.com/python/cpython/issues/139393. + self.assertLess(_PY_NSMALLPOSINTS, 2048) + def testfunc(n): class C: - t = tuple(range(300)) + t = tuple(range(2048)) x = 0 for _ in range(n): - if len(C.t) == 300: # comparison + guard removed + if len(C.t) == 2048: # comparison + guard removed x += 1 return x diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index d680711e5d828a..d117914e9b7c18 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -34,6 +34,7 @@ #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() #include "pycore_pylifecycle.h" // _PyInterpreterConfig_InitFromDict() #include "pycore_pystate.h" // _PyThreadState_GET() +#include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS, _PY_NSMALLNEGINTS #include "pycore_unicodeobject.h" // _PyUnicode_TransformDecimalAndSpaceToASCII() #include "clinic/_testinternalcapi.c.h" @@ -2576,6 +2577,14 @@ module_exec(PyObject *module) return 1; } + if (PyModule_AddIntMacro(module, _PY_NSMALLPOSINTS) < 0) { + return 1; + } + + if (PyModule_AddIntMacro(module, _PY_NSMALLNEGINTS) < 0) { + return 1; + } + return 0; } From ea1f0e96f674ba373730aa71c25aad6f632fa163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 28 Sep 2025 16:57:23 +0200 Subject: [PATCH 2/3] remove unused export --- Modules/_testinternalcapi.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index d117914e9b7c18..a4348e7e1497cd 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -34,7 +34,7 @@ #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() #include "pycore_pylifecycle.h" // _PyInterpreterConfig_InitFromDict() #include "pycore_pystate.h" // _PyThreadState_GET() -#include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS, _PY_NSMALLNEGINTS +#include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS #include "pycore_unicodeobject.h" // _PyUnicode_TransformDecimalAndSpaceToASCII() #include "clinic/_testinternalcapi.c.h" @@ -2581,10 +2581,6 @@ module_exec(PyObject *module) return 1; } - if (PyModule_AddIntMacro(module, _PY_NSMALLNEGINTS) < 0) { - return 1; - } - return 0; } From be5e820359037222e84f25989af22b090f2ba374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Sun, 28 Sep 2025 17:02:24 +0200 Subject: [PATCH 3/3] Update Lib/test/test_capi/test_opt.py --- Lib/test/test_capi/test_opt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py index 675839cc561aa7..f121f27174875e 100644 --- a/Lib/test/test_capi/test_opt.py +++ b/Lib/test/test_capi/test_opt.py @@ -2117,7 +2117,7 @@ def testfunc(n): self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops) def test_call_len_known_length(self): - # Make sure that len(t) is optimized for a tuple of length 2048. + # Make sure that len(t) is not optimized for a tuple of length 2048. # See https://github.com/python/cpython/issues/139393. self.assertLess(_PY_NSMALLPOSINTS, 2048)