https://github.com/python/cpython/commit/3779f2b95ebf68e2d5f8ea27c7a7baa0879f5496
commit: 3779f2b95ebf68e2d5f8ea27c7a7baa0879f5496
branch: main
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-09-28T19:30:44+02:00
summary:

gh-139393:  fix `_CALL_LEN` JIT tests for tuples (#139394)

Fix a regression introduced in 7ce25edb8f41e527ed479bf61ef36dc9841b4ac5
where `_PY_NSMALLPOSINTS` was changed from 257 to 1025.

files:
M Lib/test/test_capi/test_opt.py
M Modules/_testinternalcapi.c

diff --git a/Lib/test/test_capi/test_opt.py b/Lib/test/test_capi/test_opt.py
index c3fed50cee9736..f121f27174875e 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 not 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..a4348e7e1497cd 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
 #include "pycore_unicodeobject.h" // 
_PyUnicode_TransformDecimalAndSpaceToASCII()
 
 #include "clinic/_testinternalcapi.c.h"
@@ -2576,6 +2577,10 @@ module_exec(PyObject *module)
         return 1;
     }
 
+    if (PyModule_AddIntMacro(module, _PY_NSMALLPOSINTS) < 0) {
+        return 1;
+    }
+
     return 0;
 }
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to