]> granicus.if.org Git - python/commitdiff
The final tweaks before closing
authorMichael W. Hudson <mwh@python.net>
Thu, 5 Dec 2002 21:32:32 +0000 (21:32 +0000)
committerMichael W. Hudson <mwh@python.net>
Thu, 5 Dec 2002 21:32:32 +0000 (21:32 +0000)
[ 633152 ] list slice ass ignores subtypes of list

Allow arbitrary sequences on the RHS of extended slices.

Lib/test/test_types.py
Objects/listobject.c

index 1e982d14f94e5d2945cb498c4f865b22d1ac88f5..106d0a5b3b76ea7a4714e577333430c49d09f70b 100644 (file)
@@ -430,6 +430,10 @@ b[slice(2,3)] = ["two", "elements"]
 c[2:3:] = ["two", "elements"]
 vereq(a, b)
 vereq(a, c)
+a = range(10)
+a[::2] = tuple(range(5))
+vereq(a, [0, 1, 1, 3, 2, 5, 3, 7, 4, 9])
+
 
 print '6.6 Mappings == Dictionaries'
 d = {}
index 3e3b4d7c7b7764dcace56c6eae3445e340a796fc..25372d3484e941bee9a4bfdded99f3ac47b073e5 100644 (file)
@@ -2238,33 +2238,36 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
                }
                else {
                        /* assign slice */
-                       PyObject **garbage, *ins;
+                       PyObject **garbage, *ins, *seq;
                        int cur, i;
 
-                       if (!PyList_Check(value)) {
-                               PyErr_Format(PyExc_TypeError,
-                            "must assign list (not \"%.200s\") to slice",
-                                            value->ob_type->tp_name);
-                               return -1;
+                       /* protect against a[::-1] = a */
+                       if (self == (PyListObject*)value) {
+                               seq = list_slice((PyListObject*)value, 0,
+                                                  PyList_GET_SIZE(value));
+                       }
+                       else {
+                               char msg[256];
+                               PyOS_snprintf(msg, sizeof(msg),
+                     "must assign sequence (not \"%.200s\") to extended slice",
+                                             value->ob_type->tp_name);
+                               seq = PySequence_Fast(value, msg);
+                               if (!seq)
+                                       return -1;
                        }
 
-                       if (PyList_GET_SIZE(value) != slicelength) {
+                       if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
                                PyErr_Format(PyExc_ValueError,
-            "attempt to assign list of size %d to extended slice of size %d",
-                                            PyList_Size(value), slicelength);
+            "attempt to assign sequence of size %d to extended slice of size %d",
+                                            PySequence_Fast_GET_SIZE(seq),
+                                            slicelength);
+                               Py_DECREF(seq);
                                return -1;
                        }
 
-                       if (!slicelength)
+                       if (!slicelength) {
+                               Py_DECREF(seq);
                                return 0;
-
-                       /* protect against a[::-1] = a */
-                       if (self == (PyListObject*)value) {
-                               value = list_slice((PyListObject*)value, 0,
-                                                  PyList_GET_SIZE(value));
-                       }
-                       else {
-                               Py_INCREF(value);
                        }
 
                        garbage = (PyObject**)
@@ -2274,7 +2277,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
                             cur += step, i++) {
                                garbage[i] = PyList_GET_ITEM(self, cur);
 
-                               ins = PyList_GET_ITEM(value, i);
+                               ins = PySequence_Fast_GET_ITEM(seq, i);
                                Py_INCREF(ins);
                                PyList_SET_ITEM(self, cur, ins);
                        }
@@ -2284,7 +2287,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
                        }
 
                        PyMem_FREE(garbage);
-                       Py_DECREF(value);
+                       Py_DECREF(seq);
 
                        return 0;
                }