@@ -2679,7 +2679,14 @@ list_richcompare(PyObject *v, PyObject *w, int op)
26792679 }
26802680
26812681 /* Compare the final item again using the proper operator */
2682- return PyObject_RichCompare (vl -> ob_item [i ], wl -> ob_item [i ], op );
2682+ PyObject * vitem = vl -> ob_item [i ];
2683+ PyObject * witem = wl -> ob_item [i ];
2684+ Py_INCREF (vitem );
2685+ Py_INCREF (witem );
2686+ PyObject * result = PyObject_RichCompare (vl -> ob_item [i ], wl -> ob_item [i ], op );
2687+ Py_DECREF (vitem );
2688+ Py_DECREF (witem );
2689+ return result ;
26832690}
26842691
26852692/*[clinic input]
@@ -2863,6 +2870,23 @@ list_subscript(PyListObject* self, PyObject* item)
28632870 }
28642871}
28652872
2873+ static Py_ssize_t
2874+ adjust_slice_indexes (PyListObject * lst ,
2875+ Py_ssize_t * start , Py_ssize_t * stop ,
2876+ Py_ssize_t step )
2877+ {
2878+ Py_ssize_t slicelength = PySlice_AdjustIndices (Py_SIZE (lst ), start , stop ,
2879+ step );
2880+
2881+ /* Make sure s[5:2] = [..] inserts at the right place:
2882+ before 5, not before 2. */
2883+ if ((step < 0 && * start < * stop ) ||
2884+ (step > 0 && * start > * stop ))
2885+ * stop = * start ;
2886+
2887+ return slicelength ;
2888+ }
2889+
28662890static int
28672891list_ass_subscript (PyListObject * self , PyObject * item , PyObject * value )
28682892{
@@ -2875,22 +2899,11 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28752899 return list_ass_item (self , i , value );
28762900 }
28772901 else if (PySlice_Check (item )) {
2878- Py_ssize_t start , stop , step , slicelength ;
2902+ Py_ssize_t start , stop , step ;
28792903
28802904 if (PySlice_Unpack (item , & start , & stop , & step ) < 0 ) {
28812905 return -1 ;
28822906 }
2883- slicelength = PySlice_AdjustIndices (Py_SIZE (self ), & start , & stop ,
2884- step );
2885-
2886- if (step == 1 )
2887- return list_ass_slice (self , start , stop , value );
2888-
2889- /* Make sure s[5:2] = [..] inserts at the right place:
2890- before 5, not before 2. */
2891- if ((step < 0 && start < stop ) ||
2892- (step > 0 && start > stop ))
2893- stop = start ;
28942907
28952908 if (value == NULL ) {
28962909 /* delete slice */
@@ -2899,6 +2912,12 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
28992912 Py_ssize_t i ;
29002913 int res ;
29012914
2915+ Py_ssize_t slicelength = adjust_slice_indexes (self , & start , & stop ,
2916+ step );
2917+
2918+ if (step == 1 )
2919+ return list_ass_slice (self , start , stop , value );
2920+
29022921 if (slicelength <= 0 )
29032922 return 0 ;
29042923
@@ -2974,6 +2993,15 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
29742993 if (!seq )
29752994 return -1 ;
29762995
2996+ Py_ssize_t slicelength = adjust_slice_indexes (self , & start , & stop ,
2997+ step );
2998+
2999+ if (step == 1 ) {
3000+ int res = list_ass_slice (self , start , stop , seq );
3001+ Py_DECREF (seq );
3002+ return res ;
3003+ }
3004+
29773005 if (PySequence_Fast_GET_SIZE (seq ) != slicelength ) {
29783006 PyErr_Format (PyExc_ValueError ,
29793007 "attempt to assign sequence of "
0 commit comments