Skip to content
Closed
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
30 changes: 30 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,36 @@ def __setitem__(self, key, value):
store_subscr_dict_subclass_override()
self.assert_no_opcode(store_subscr_dict_subclass_override, "STORE_SUBSCR_DICT")

@cpython_only
@requires_specialization
def test_binary_subscr_list_int_wide_int(self):
def load(lst, idx):
return lst[idx]

lst = [1, 2, 3]
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
load(lst, 0)
self.assert_specialized(load, "BINARY_OP_SUBSCR_LIST_INT")
# 2**30+2 is non-compact; ob_digit[0]=2 so _PyLong_CompactValue
# would silently return 2 and read lst[2] instead of raising IndexError.
with self.assertRaises(IndexError):
load(lst, 2**30 + 2)

@cpython_only
@requires_specialization
def test_store_subscr_list_int_wide_int(self):
def store(lst, idx):
lst[idx] = 99

lst = [1, 2, 3]
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
store(lst, 0)
self.assert_specialized(store, "STORE_SUBSCR_LIST_INT")
# 2**30+2 is non-compact; ob_digit[0]=2 so _PyLong_CompactValue
# would silently write to lst[2] instead of raising IndexError.
with self.assertRaises(IndexError):
store(lst, 2**30 + 2)

@cpython_only
@requires_specialization
def test_compare_op(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``BINARY_OP_SUBSCR_LIST_INT`` and ``STORE_SUBSCR_LIST_INT`` now verify that
the subscript is a compact non-negative int before calling the compact-only
internal helper, rather than relying solely on the shared ``_GUARD_TOS_INT``
guard to enforce that invariant.
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 @@ -1141,6 +1141,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 @@ -1419,6 +1420,7 @@ dummy_func(

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

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