Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,19 @@ def binary_subscr_list_int():
"BINARY_OP_SUBSCR_LIST_INT")
self.assert_no_opcode(binary_subscr_list_int, "BINARY_OP")

def binary_subscr_list_int_noncompact(index):
a = [1, 2, 3]
return a[index]

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
self.assertEqual(binary_subscr_list_int_noncompact(0), 1)
self.assert_specialized(binary_subscr_list_int_noncompact,
"BINARY_OP_SUBSCR_LIST_INT")
# 2**30 + 2 is non-compact, but its low digit is 2. The specialized
# path must fall back instead of reading the low digit as the index.
with self.assertRaises(IndexError):
binary_subscr_list_int_noncompact(2**30 + 2)

def binary_subscr_tuple_int():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = (1, 2, 3)
Expand Down Expand Up @@ -1995,6 +2008,19 @@ def __getitem__(self, item):
@cpython_only
@requires_specialization
def test_store_subscr(self):
def store_subscr_list_int(index):
a = [None]
a[index] = 1
return a

for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
self.assertEqual(store_subscr_list_int(0), [1])
self.assert_specialized(store_subscr_list_int, "STORE_SUBSCR_LIST_INT")
# 2**30 + 2 is non-compact, but its low digit is 2. The specialized
# path must fall back instead of writing through the low digit index.
with self.assertRaises(IndexError):
store_subscr_list_int(2**30 + 2)

def store_subscr_dict():
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
a = {1: 2, 2: 3}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix specialized list subscript and store-subscript paths so they fall back for
non-compact ``int`` indices instead of passing them to
``_PyLong_CompactValue()``.
10 changes: 10 additions & 0 deletions Modules/_testinternalcapi/test_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,7 @@ dummy_func(

assert(PyLong_CheckExact(sub));
assert(PyList_CheckExact(list));
EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));

Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
if (index < 0) {
Expand Down Expand Up @@ -1420,6 +1421,7 @@ dummy_func(

assert(PyLong_CheckExact(sub));
assert(PyList_CheckExact(list));
DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use EXIT_IF here?


Py_ssize_t index = _PyLong_CompactValue((PyLongObject *)sub);
DEOPT_IF(!LOCK_OBJECT(list));
Expand Down
15 changes: 15 additions & 0 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading