Skip to content

Commit 3779f2b

Browse files
authored
gh-139393: fix _CALL_LEN JIT tests for tuples (#139394)
Fix a regression introduced in 7ce25ed where `_PY_NSMALLPOSINTS` was changed from 257 to 1025.
1 parent 519bc47 commit 3779f2b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
_testinternalcapi = import_helper.import_module("_testinternalcapi")
1717

18-
from _testinternalcapi import TIER2_THRESHOLD
18+
from _testinternalcapi import _PY_NSMALLPOSINTS, TIER2_THRESHOLD
1919

2020
#For test of issue 136154
2121
GLOBAL_136154 = 42
@@ -2093,6 +2093,10 @@ def testfunc(n):
20932093
self.assertNotIn("_GUARD_TOS_INT", uops)
20942094

20952095
def test_call_len_known_length_small_int(self):
2096+
# Make sure that len(t) is optimized for a tuple of length 5.
2097+
# See https://github.com/python/cpython/issues/139393.
2098+
self.assertGreater(_PY_NSMALLPOSINTS, 5)
2099+
20962100
def testfunc(n):
20972101
x = 0
20982102
for _ in range(n):
@@ -2113,13 +2117,17 @@ def testfunc(n):
21132117
self.assertNotIn("_POP_TOP_LOAD_CONST_INLINE_BORROW", uops)
21142118

21152119
def test_call_len_known_length(self):
2120+
# Make sure that len(t) is not optimized for a tuple of length 2048.
2121+
# See https://github.com/python/cpython/issues/139393.
2122+
self.assertLess(_PY_NSMALLPOSINTS, 2048)
2123+
21162124
def testfunc(n):
21172125
class C:
2118-
t = tuple(range(300))
2126+
t = tuple(range(2048))
21192127

21202128
x = 0
21212129
for _ in range(n):
2122-
if len(C.t) == 300: # comparison + guard removed
2130+
if len(C.t) == 2048: # comparison + guard removed
21232131
x += 1
21242132
return x
21252133

Modules/_testinternalcapi.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
3535
#include "pycore_pylifecycle.h" // _PyInterpreterConfig_InitFromDict()
3636
#include "pycore_pystate.h" // _PyThreadState_GET()
37+
#include "pycore_runtime_structs.h" // _PY_NSMALLPOSINTS
3738
#include "pycore_unicodeobject.h" // _PyUnicode_TransformDecimalAndSpaceToASCII()
3839

3940
#include "clinic/_testinternalcapi.c.h"
@@ -2576,6 +2577,10 @@ module_exec(PyObject *module)
25762577
return 1;
25772578
}
25782579

2580+
if (PyModule_AddIntMacro(module, _PY_NSMALLPOSINTS) < 0) {
2581+
return 1;
2582+
}
2583+
25792584
return 0;
25802585
}
25812586

0 commit comments

Comments
 (0)