@@ -2707,7 +2707,14 @@ list_richcompare(PyObject *v, PyObject *w, int op)
27072707 }
27082708
27092709 /* Compare the final item again using the proper operator */
2710- return PyObject_RichCompare (vl -> ob_item [i ], wl -> ob_item [i ], op );
2710+ PyObject * vitem = vl -> ob_item [i ];
2711+ PyObject * witem = wl -> ob_item [i ];
2712+ Py_INCREF (vitem );
2713+ Py_INCREF (witem );
2714+ PyObject * result = PyObject_RichCompare (vl -> ob_item [i ], wl -> ob_item [i ], op );
2715+ Py_DECREF (vitem );
2716+ Py_DECREF (witem );
2717+ return result ;
27112718}
27122719
27132720/*[clinic input]
@@ -2878,6 +2885,23 @@ list_subscript(PyListObject* self, PyObject* item)
28782885 }
28792886}
28802887
2888+ static Py_ssize_t
2889+ adjust_slice_indexes (PyListObject * lst ,
2890+ Py_ssize_t * start , Py_ssize_t * stop ,
2891+ Py_ssize_t step )
2892+ {
2893+ Py_ssize_t slicelength = PySlice_AdjustIndices (Py_SIZE (lst ), start , stop ,
2894+ step );
2895+
2896+ /* Make sure s[5:2] = [..] inserts at the right place:
2897+ before 5, not before 2. */
2898+ if ((step < 0 && * start < * stop ) ||
2899+ (step > 0 && * start > * stop ))
2900+ * stop = * start ;
2901+
2902+ return slicelength ;
2903+ }
2904+
28812905static int
28822906list_ass_subscript (PyListObject * self , PyObject * item , PyObject * value )
28832907{
@@ -2890,22 +2914,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28902914 return list_ass_item (self , i , value );
28912915 }
28922916 else if (PySlice_Check (item )) {
2893- Py_ssize_t start , stop , step , slicelength ;
2917+ Py_ssize_t start , stop , step ;
28942918
28952919 if (PySlice_Unpack (item , & start , & stop , & step ) < 0 ) {
28962920 return -1 ;
28972921 }
2898- slicelength = PySlice_AdjustIndices (Py_SIZE (self ), & start , & stop ,
2899- step );
2900-
2901- if (step == 1 )
2902- return list_ass_slice (self , start , stop , value );
2903-
2904- /* Make sure s[5:2] = [..] inserts at the right place:
2905- before 5, not before 2. */
2906- if ((step < 0 && start < stop ) ||
2907- (step > 0 && start > stop ))
2908- stop = start ;
29092922
29102923 if (value == NULL ) {
29112924 /* delete slice */
@@ -2914,6 +2927,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29142927 Py_ssize_t i ;
29152928 int res ;
29162929
2930+ Py_ssize_t slicelength = adjust_slice_indexes (self , & start , & stop ,
2931+ step );
2932+
2933+ if (step == 1 )
2934+ return list_ass_slice (self , start , stop , value );
2935+
29172936 if (slicelength <= 0 )
29182937 return 0 ;
29192938
@@ -2989,6 +3008,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29893008 if (!seq )
29903009 return -1 ;
29913010
3011+ Py_ssize_t slicelength = adjust_slice_indexes (self , & start , & stop ,
3012+ step );
3013+
3014+ if (step == 1 ) {
3015+ int res = list_ass_slice (self , start , stop , seq );
3016+ Py_DECREF (seq );
3017+ return res ;
3018+ }
3019+
29923020 if (PySequence_Fast_GET_SIZE (seq ) != slicelength ) {
29933021 PyErr_Format (PyExc_ValueError ,
29943022 "attempt to assign sequence of "
0 commit comments