Commit 0a222b78 authored by Michael W. Hudson's avatar Michael W. Hudson

This is Alex Martelli's patch

[ 633870 ] allow any seq assignment to a list slice

plus a very silly little test case of my own.
parent 74bb4c1b
...@@ -297,6 +297,10 @@ a *= 0 ...@@ -297,6 +297,10 @@ a *= 0
if a != []: if a != []:
raise TestFailed, "list inplace repeat" raise TestFailed, "list inplace repeat"
a = []
a[:] = tuple(range(10))
if a != range(10):
raise TestFailed, "assigning tuple to slice"
print '6.5.3a Additional list operations' print '6.5.3a Additional list operations'
a = [0,1,2,3,4] a = [0,1,2,3,4]
......
...@@ -448,14 +448,22 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -448,14 +448,22 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
list. :-( */ list. :-( */
PyObject **recycle, **p; PyObject **recycle, **p;
PyObject **item; PyObject **item;
PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
int n; /* Size of replacement list */ int n; /* Size of replacement list */
int d; /* Change in size */ int d; /* Change in size */
int k; /* Loop index */ int k; /* Loop index */
#define b ((PyListObject *)v) #define b ((PyListObject *)v)
if (v == NULL) if (v == NULL)
n = 0; n = 0;
else if (PyList_Check(v)) { else {
n = b->ob_size; char msg[256];
sprintf(msg, "must assign sequence (not \"%.200s\") to slice",
v->ob_type->tp_name);
v_as_SF = PySequence_Fast(v, msg);
if(v_as_SF == NULL)
return -1;
n = PySequence_Fast_GET_SIZE(v_as_SF);
if (a == b) { if (a == b) {
/* Special case "a[i:j] = a" -- copy b first */ /* Special case "a[i:j] = a" -- copy b first */
int ret; int ret;
...@@ -465,12 +473,6 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -465,12 +473,6 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
return ret; return ret;
} }
} }
else {
PyErr_Format(PyExc_TypeError,
"must assign list (not \"%.200s\") to slice",
v->ob_type->tp_name);
return -1;
}
if (ilow < 0) if (ilow < 0)
ilow = 0; ilow = 0;
else if (ilow > a->ob_size) else if (ilow > a->ob_size)
...@@ -512,7 +514,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -512,7 +514,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
a->ob_size += d; a->ob_size += d;
} }
for (k = 0; k < n; k++, ilow++) { for (k = 0; k < n; k++, ilow++) {
PyObject *w = b->ob_item[k]; PyObject *w = PySequence_Fast_GET_ITEM(v_as_SF, k);
Py_XINCREF(w); Py_XINCREF(w);
item[ilow] = w; item[ilow] = w;
} }
...@@ -525,6 +527,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -525,6 +527,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
PyMem_FREE(a->ob_item); PyMem_FREE(a->ob_item);
a->ob_item = NULL; a->ob_item = NULL;
} }
Py_XDECREF(v_as_SF);
return 0; return 0;
#undef b #undef b
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment