From 5bbcce1b39c5006018f66ff02675cd904dd8b4a5 Mon Sep 17 00:00:00 2001 From: Josh Hurst Date: Fri, 27 Nov 2026 19:23:38 -0500 Subject: [PATCH] gh-162784: Add list fast path for STORE_SLICE and slice helpers STORE_SLICE list fast path Skip the temporary slice for exact lists. Stage displaced references for equal-length replacements of up to 32 items and release them after all writes. Preserve bound finalization order and iterable conversion errors. Slice helpers Unpack compact integer bounds directly and use a difference to count unit-step slices after normalization. --- Include/internal/pycore_list.h | 2 + Lib/test/test_capi/test_slice.py | 11 ++ Lib/test/test_list.py | 114 ++++++++++++++++++ ...-11-27-19-20-00.gh-issue-162784.s1c3f8.rst | 1 + Modules/_testinternalcapi/test_cases.c.h | 54 ++++++--- Objects/listobject.c | 92 ++++++++++++++ Objects/sliceobject.c | 29 ++++- Python/bytecodes.c | 24 ++-- Python/executor_cases.c.h | 63 ++++++---- Python/generated_cases.c.h | 54 ++++++--- 10 files changed, 377 insertions(+), 67 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-19-20-00.gh-issue-162784.s1c3f8.rst diff --git a/Include/internal/pycore_list.h b/Include/internal/pycore_list.h index df0d00f752573b5..f27643e45ab86e8 100644 --- a/Include/internal/pycore_list.h +++ b/Include/internal/pycore_list.h @@ -15,6 +15,8 @@ extern "C" { PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject *, PyObject *); PyAPI_FUNC(PyObject) *_PyList_SliceSubscript(PyObject*, PyObject*); PyAPI_FUNC(PyObject *) _PyList_BinarySlice(PyObject *, PyObject *, PyObject *); +PyAPI_FUNC(int) _PyList_StoreSlice(PyObject *container, PyObject *start, + PyObject *stop, PyObject *value); PyAPI_FUNC(PyObject *) _PyList_Concat(PyObject *, PyObject *); extern void _PyList_DebugMallocStats(FILE *out); // _PyList_GetItemRef should be used only when the object is known as a list diff --git a/Lib/test/test_capi/test_slice.py b/Lib/test/test_capi/test_slice.py index 3f41b55a609f48f..0e2da80d01530e3 100644 --- a/Lib/test/test_capi/test_slice.py +++ b/Lib/test/test_capi/test_slice.py @@ -1,3 +1,4 @@ +import itertools import sys import unittest from test.support import import_helper @@ -180,6 +181,16 @@ def test_adjustindices(self): # CRASHES adjust(10, 0, 10, 0) # CRASHES adjust(10, 0, 10, SSIZE_MIN) + def test_adjustindices_unit_step(self): + adjust = _testlimitedcapi.slice_adjustindices + bounds = range(-12, 12) + for start, stop, end in itertools.product(bounds, repeat=3): + length = end - bounds.start # sequence lengths from 0 to 23 + for step in (-1, 1): + lo, hi, _ = slice(start, stop, step).indices(length) + self.assertEqual(adjust(length, start, stop, step), + (len(range(lo, hi, step)), lo, hi)) + class GetIndicesExMacroTest(unittest.TestCase): # PySlice_GetIndicesEx() is a macro using PySlice_Unpack() and diff --git a/Lib/test/test_list.py b/Lib/test/test_list.py index 260f9f4f607f152..6818a4f3100bbba 100644 --- a/Lib/test/test_list.py +++ b/Lib/test/test_list.py @@ -429,5 +429,119 @@ def foo(x): self.assertEqual(foo(list(range(10))), 45) +class ListStoreSliceTests(unittest.TestCase): + def test_equal_length(self): + for n in (0, 1, 8, 16, 32, 33, 40): + for rhs in (list, tuple): + with self.subTest(n=n, rhs=rhs): + a = list(range(n + 2)) + a[1:1+n] = rhs(a[n:0:-1]) + self.assertEqual(a, [0, *range(n, 0, -1), n + 1]) + + def test_self_assignment(self): + a = [1, 2, 3, 4] + a[:] = a + self.assertEqual(a, [1, 2, 3, 4]) + a[1:3] = a + self.assertEqual(a, [1, 1, 2, 3, 4, 4]) + + def test_index_protocol(self): + class Idx: + def __index__(self): + return -2 + + a = [0, 1, 2] + a[True:2] = [7] + self.assertEqual(a, [0, 7, 2]) + a[Idx():2] = (8,) + self.assertEqual(a, [0, 8, 2]) + + def test_bad_index(self): + a = [1, 2, 3] + msg = "slice indices must be integers or have an __index__ method" + for start, stop in ((object(), 2), (1, object())): + with self.assertRaises(TypeError) as cm: + a[start:stop] = None + self.assertEqual(str(cm.exception), msg) + self.assertEqual(a, [1, 2, 3]) + + def test_non_iterable(self): + a = [1, 2, 3] + with self.assertRaises(TypeError) as cm: + a[:2] = None + self.assertEqual(str(cm.exception), + "must assign iterable to extended slice") + self.assertEqual(a, [1, 2, 3]) + + def test_resize_and_insert(self): + a = list(range(6)) + a[5:2] = [99] + self.assertEqual(a, [0, 1, 2, 3, 4, 99, 5]) + a[1:2] = (7, 8) + self.assertEqual(a, [0, 7, 8, 2, 3, 4, 99, 5]) + a[1:4] = (9,) + self.assertEqual(a, [0, 9, 3, 4, 99, 5]) + + def test_iterator_mutates_target(self): + a = list(range(10)) + + def gen(): + yield 100 + a.clear() + yield 200 + + a[2:8] = gen() + self.assertEqual(a, [100, 200]) + + @cpython_only + def test_finalizers_see_replacement(self): + seen = [] + + class D: + def __init__(self, i): + self.i = i + + def __del__(self): + seen.append((self.i, tuple(a[:3]))) + a.append(self.i) + + a = [D(i) for i in range(3)] + a[:3] = [5, 6, 7] + self.assertEqual(seen, [(i, (5, 6, 7)) for i in (2, 1, 0)]) + self.assertEqual(a, [5, 6, 7, 2, 1, 0]) + + @cpython_only + def test_bound_finalizer_order(self): + seen = [] + + class Idx: + def __init__(self, i): + self.i = i + + def __index__(self): + return self.i + + def __del__(self): + seen.append(self.i) + + a = [0, 1, 2] + a[Idx(0):Idx(1)] = [8] + self.assertEqual(a, [8, 1, 2]) + self.assertEqual(seen, [0, 1]) + + def test_list_subclass(self): + seen = [] + + class MyList(list): + def __setitem__(self, key, value): + seen.append(key) + super().__setitem__(key, value) + + a = MyList([1, 2, 3, 4]) + a[1:3] = [8, 9] + self.assertEqual(a, [1, 8, 9, 4]) + self.assertEqual(seen, [slice(1, 3)]) + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-19-20-00.gh-issue-162784.s1c3f8.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-19-20-00.gh-issue-162784.s1c3f8.rst new file mode 100644 index 000000000000000..3dde00043cab1b3 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-27-19-20-00.gh-issue-162784.s1c3f8.rst @@ -0,0 +1 @@ +Speed up slice assignment on lists and index adjustment for unit-step slices. diff --git a/Modules/_testinternalcapi/test_cases.c.h b/Modules/_testinternalcapi/test_cases.c.h index 8bb3f857437792a..a2600d42546094f 100644 --- a/Modules/_testinternalcapi/test_cases.c.h +++ b/Modules/_testinternalcapi/test_cases.c.h @@ -12510,44 +12510,64 @@ start = stack_pointer[-2]; container = stack_pointer[-3]; v = stack_pointer[-4]; - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start), - PyStackRef_AsPyObjectSteal(stop), - Py_None); - _PyFrame_StackPointerInvalidate(frame); + PyObject *container_o = PyStackRef_AsPyObjectBorrow(container); + PyObject *start_o = PyStackRef_AsPyObjectSteal(start); + PyObject *stop_o = PyStackRef_AsPyObjectSteal(stop); + PyObject *v_o = PyStackRef_AsPyObjectBorrow(v); int err; - if (slice == NULL) { - err = 1; - } - else { + if (PyList_CheckExact(container_o)) { stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v)); + err = _PyList_StoreSlice(container_o, start_o, stop_o, v_o); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); - Py_DECREF(slice); + Py_DECREF(start_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(stop_o); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += 2; } - _PyFrame_SetStackPointer(frame, stack_pointer); + else { + stack_pointer += -2; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + _PyFrame_SetStackPointer(frame, stack_pointer); + _PyFrame_StackPointerValidate(frame); + PyObject *slice = _PyBuildSlice_ConsumeRefs(start_o, stop_o, + Py_None); + _PyFrame_StackPointerInvalidate(frame); + if (slice == NULL) { + err = 1; + } + else { + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + err = PyObject_SetItem(container_o, slice, v_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(slice); + _PyFrame_StackPointerInvalidate(frame); + } + } + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); _PyStackRef tmp = container; container = PyStackRef_NULL; - stack_pointer[-3] = container; + stack_pointer[-1] = container; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); tmp = v; v = PyStackRef_NULL; - stack_pointer[-4] = v; + stack_pointer[-2] = v; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -4; + stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); if (err) { JUMP_TO_LABEL(error); diff --git a/Objects/listobject.c b/Objects/listobject.c index c5fe704df0e23de..d75f49186013891 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -3975,6 +3975,98 @@ list_ass_subscript_lock_held(PyObject *_self, PyObject *item, PyObject *value) } } +#define LIST_SMALL_SLICE_INPLACE_MAX 32 + +static inline void +list_replace_small_slice_inplace(PyListObject *self, Py_ssize_t start, + Py_ssize_t len, PyObject *const *src) +{ + PyObject *garbage[LIST_SMALL_SLICE_INPLACE_MAX]; + PyObject **dst = self->ob_item + start; + for (Py_ssize_t i = 0; i < len; i++) { + garbage[i] = dst[i]; + FT_ATOMIC_STORE_PTR_RELEASE(dst[i], Py_NewRef(src[i])); + } + /* A finalizer can inspect or resize the list. */ + for (Py_ssize_t i = len - 1; i >= 0; i--) { + Py_DECREF(garbage[i]); + } +} + +static inline int +list_slice_index_fast(PyObject *obj, Py_ssize_t defval, Py_ssize_t *out) +{ + if (Py_IsNone(obj)) { + *out = defval; + return 1; + } + if (PyLong_CheckExact(obj) && _PyLong_IsCompact((PyLongObject *)obj)) { + *out = _PyLong_CompactValue((PyLongObject *)obj); + return 1; + } + return _PyEval_SliceIndex(obj, out); +} + +static int +list_store_slice_lock_held(PyListObject *self, Py_ssize_t start, + Py_ssize_t stop, PyObject *value) +{ + assert(value != NULL); + if ((PyObject *)self != value && + (PyList_CheckExact(value) || PyTuple_CheckExact(value))) + { + Py_ssize_t len = adjust_slice_indexes(self, &start, &stop, 1); + if (len > 0 && len <= LIST_SMALL_SLICE_INPLACE_MAX && + len == Py_SIZE(value)) + { + list_replace_small_slice_inplace(self, start, len, + PySequence_Fast_ITEMS(value)); + return 0; + } + return list_ass_slice_lock_held(self, start, stop, value); + } + + PyObject *seq = ((PyObject *)self == value) + ? list_slice_lock_held(self, 0, Py_SIZE(self)) + : PySequence_Fast(value, "must assign iterable to extended slice"); + if (seq == NULL) { + return -1; + } + /* The iterator may have changed the list length. */ + adjust_slice_indexes(self, &start, &stop, 1); + int res = list_ass_slice_lock_held(self, start, stop, seq); + Py_DECREF(seq); + return res; +} + +int +_PyList_StoreSlice(PyObject *container, PyObject *start, PyObject *stop, + PyObject *value) +{ + assert(value != NULL); + assert(PyList_CheckExact(container)); + Py_ssize_t istart, istop; + if (!list_slice_index_fast(start, 0, &istart) || + !list_slice_index_fast(stop, PY_SSIZE_T_MAX, &istop)) + { + return -1; + } + PyListObject *self = (PyListObject *)container; + int res; +#ifdef Py_GIL_DISABLED + if (PyList_CheckExact(value)) { + Py_BEGIN_CRITICAL_SECTION2(container, value); + res = list_store_slice_lock_held(self, istart, istop, value); + Py_END_CRITICAL_SECTION2(); + return res; + } +#endif + Py_BEGIN_CRITICAL_SECTION(container); + res = list_store_slice_lock_held(self, istart, istop, value); + Py_END_CRITICAL_SECTION(); + return res; +} + static int list_ass_subscript(PyObject *self, PyObject *item, PyObject *value) { diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 5ab7e42d9e7b76e..1537c597462b09b 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -214,6 +214,16 @@ PySlice_GetIndices(PyObject *_r, Py_ssize_t length, return 0; } +static inline int +slice_index_fast(PyObject *v, Py_ssize_t *pi) +{ + if (PyLong_CheckExact(v) && _PyLong_IsCompact((PyLongObject *)v)) { + *pi = _PyLong_CompactValue((PyLongObject *)v); + return 1; + } + return _PyEval_SliceIndex(v, pi); +} + int PySlice_Unpack(PyObject *_r, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) @@ -228,7 +238,9 @@ PySlice_Unpack(PyObject *_r, *step = 1; } else { - if (!_PyEval_SliceIndex(r->step, step)) return -1; + if (!slice_index_fast(r->step, step)) { + return -1; + } if (*step == 0) { PyErr_SetString(PyExc_ValueError, "slice step cannot be zero"); @@ -247,14 +259,18 @@ PySlice_Unpack(PyObject *_r, *start = *step < 0 ? PY_SSIZE_T_MAX : 0; } else { - if (!_PyEval_SliceIndex(r->start, start)) return -1; + if (!slice_index_fast(r->start, start)) { + return -1; + } } if (r->stop == Py_None) { *stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX; } else { - if (!_PyEval_SliceIndex(r->stop, stop)) return -1; + if (!slice_index_fast(r->stop, stop)) { + return -1; + } } return 0; @@ -289,6 +305,13 @@ PySlice_AdjustIndices(Py_ssize_t length, *stop = (step < 0) ? length - 1 : length; } + /* For unit steps the general formula reduces to a difference. */ + if (step == 1) { + return *start < *stop ? *stop - *start : 0; + } + if (step == -1) { + return *stop < *start ? *start - *stop : 0; + } if (step < 0) { if (*stop < *start) { return (*start - *stop - 1) / (-step) + 1; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index c6f1f61f3e08fe1..ae3f31381587dd8 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1122,16 +1122,26 @@ dummy_func( } op(_STORE_SLICE, (v, container, start, stop -- )) { - PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start), - PyStackRef_AsPyObjectSteal(stop), - Py_None); + PyObject *container_o = PyStackRef_AsPyObjectBorrow(container); + PyObject *start_o = PyStackRef_AsPyObjectSteal(start); + PyObject *stop_o = PyStackRef_AsPyObjectSteal(stop); + PyObject *v_o = PyStackRef_AsPyObjectBorrow(v); int err; - if (slice == NULL) { - err = 1; + if (PyList_CheckExact(container_o)) { + err = _PyList_StoreSlice(container_o, start_o, stop_o, v_o); + Py_DECREF(start_o); + Py_DECREF(stop_o); } else { - err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v)); - Py_DECREF(slice); + PyObject *slice = _PyBuildSlice_ConsumeRefs(start_o, stop_o, + Py_None); + if (slice == NULL) { + err = 1; + } + else { + err = PyObject_SetItem(container_o, slice, v_o); + Py_DECREF(slice); + } } DECREF_INPUTS(); ERROR_IF(err); diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5df8120e7424b93..c4ecf973005d084 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -6924,49 +6924,66 @@ start = _stack_item_1; container = _stack_item_0; v = stack_pointer[-1]; - stack_pointer[0] = container; - stack_pointer[1] = start; - stack_pointer[2] = stop; - stack_pointer += 3; - ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start), - PyStackRef_AsPyObjectSteal(stop), - Py_None); - _PyFrame_StackPointerInvalidate(frame); + PyObject *container_o = PyStackRef_AsPyObjectBorrow(container); + PyObject *start_o = PyStackRef_AsPyObjectSteal(start); + PyObject *stop_o = PyStackRef_AsPyObjectSteal(stop); + PyObject *v_o = PyStackRef_AsPyObjectBorrow(v); int err; - if (slice == NULL) { - err = 1; - } - else { - stack_pointer += -2; + if (PyList_CheckExact(container_o)) { + stack_pointer[0] = container; + stack_pointer += 1; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v)); + err = _PyList_StoreSlice(container_o, start_o, stop_o, v_o); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); - Py_DECREF(slice); + Py_DECREF(start_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(stop_o); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += 2; } - _PyFrame_SetStackPointer(frame, stack_pointer); + else { + stack_pointer[0] = container; + stack_pointer += 1; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + _PyFrame_SetStackPointer(frame, stack_pointer); + _PyFrame_StackPointerValidate(frame); + PyObject *slice = _PyBuildSlice_ConsumeRefs(start_o, stop_o, + Py_None); + _PyFrame_StackPointerInvalidate(frame); + if (slice == NULL) { + err = 1; + } + else { + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + err = PyObject_SetItem(container_o, slice, v_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(slice); + _PyFrame_StackPointerInvalidate(frame); + } + } + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); _PyStackRef tmp = container; container = PyStackRef_NULL; - stack_pointer[-3] = container; + stack_pointer[-1] = container; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); tmp = v; v = PyStackRef_NULL; - stack_pointer[-4] = v; + stack_pointer[-2] = v; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -4; + stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); if (err) { SET_CURRENT_CACHED_VALUES(0); diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 3af7863e80d2c6a..afe06013966d1b6 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -12507,44 +12507,64 @@ start = stack_pointer[-2]; container = stack_pointer[-3]; v = stack_pointer[-4]; - _PyFrame_SetStackPointer(frame, stack_pointer); - _PyFrame_StackPointerValidate(frame); - PyObject *slice = _PyBuildSlice_ConsumeRefs(PyStackRef_AsPyObjectSteal(start), - PyStackRef_AsPyObjectSteal(stop), - Py_None); - _PyFrame_StackPointerInvalidate(frame); + PyObject *container_o = PyStackRef_AsPyObjectBorrow(container); + PyObject *start_o = PyStackRef_AsPyObjectSteal(start); + PyObject *stop_o = PyStackRef_AsPyObjectSteal(stop); + PyObject *v_o = PyStackRef_AsPyObjectBorrow(v); int err; - if (slice == NULL) { - err = 1; - } - else { + if (PyList_CheckExact(container_o)) { stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); _PyFrame_SetStackPointer(frame, stack_pointer); _PyFrame_StackPointerValidate(frame); - err = PyObject_SetItem(PyStackRef_AsPyObjectBorrow(container), slice, PyStackRef_AsPyObjectBorrow(v)); + err = _PyList_StoreSlice(container_o, start_o, stop_o, v_o); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); - Py_DECREF(slice); + Py_DECREF(start_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(stop_o); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += 2; } - _PyFrame_SetStackPointer(frame, stack_pointer); + else { + stack_pointer += -2; + ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); + _PyFrame_SetStackPointer(frame, stack_pointer); + _PyFrame_StackPointerValidate(frame); + PyObject *slice = _PyBuildSlice_ConsumeRefs(start_o, stop_o, + Py_None); + _PyFrame_StackPointerInvalidate(frame); + if (slice == NULL) { + err = 1; + } + else { + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + err = PyObject_SetItem(container_o, slice, v_o); + _PyFrame_StackPointerInvalidate(frame); + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); + _PyFrame_StackPointerValidate(frame); + Py_DECREF(slice); + _PyFrame_StackPointerInvalidate(frame); + } + } + assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); _PyStackRef tmp = container; container = PyStackRef_NULL; - stack_pointer[-3] = container; + stack_pointer[-1] = container; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); assert(stack_pointer == _PyFrame_GetStackPointer(frame)); _PyFrame_StackPointerValidate(frame); tmp = v; v = PyStackRef_NULL; - stack_pointer[-4] = v; + stack_pointer[-2] = v; PyStackRef_CLOSE(tmp); _PyFrame_StackPointerInvalidate(frame); - stack_pointer += -4; + stack_pointer += -2; ASSERT_WITHIN_STACK_BOUNDS(__FILE__, __LINE__); if (err) { JUMP_TO_LABEL(error);