static
int
list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
{
PyObject *recycle_on_stack[8];
PyObject **recycle = recycle_on_stack;
PyObject **item;
PyObject **vitem = NULL;
PyObject *v_as_SF = NULL;
Py_ssize_t n;
Py_ssize_t norig;
Py_ssize_t d;
Py_ssize_t k;
size_t s;
int result = -1;
#define b ((PyListObject *)v)
if
(v == NULL)
n = 0;
else
{
if
(a == b) {
v = list_slice(b, 0, Py_SIZE(b));
if
(v == NULL)
return
result;
result = list_ass_slice(a, ilow, ihigh, v);
Py_DECREF(v);
return
result;
}
v_as_SF = PySequence_Fast(v,
"can only assign an iterable"
);
if
(v_as_SF == NULL)
goto
Error;
n = PySequence_Fast_GET_SIZE(v_as_SF);
vitem = PySequence_Fast_ITEMS(v_as_SF);
}
if
(ilow < 0)
ilow = 0;
else
if
(ilow > Py_SIZE(a))
ilow = Py_SIZE(a);
if
(ihigh < ilow)
ihigh = ilow;
else
if
(ihigh > Py_SIZE(a))
ihigh = Py_SIZE(a);
norig = ihigh - ilow;
assert(norig >= 0);
d = n - norig;
if
(Py_SIZE(a) + d == 0) {
Py_XDECREF(v_as_SF);
return
list_clear(a);
}
item = a->ob_item;
s = norig * sizeof(PyObject *);
if
(s > sizeof(recycle_on_stack)) {
recycle = (PyObject **)PyMem_MALLOC(s);
if
(recycle == NULL) {
PyErr_NoMemory();
goto
Error;
}
}
memcpy(recycle, &item[ilow], s);
if
(d < 0) {
memmove(&item[ihigh+d], &item[ihigh],
(Py_SIZE(a) - ihigh)*sizeof(PyObject *));
list_resize(a, Py_SIZE(a) + d);
item = a->ob_item;
}
else
if
(d > 0) {
k = Py_SIZE(a);
if
(list_resize(a, k+d) < 0)
goto
Error;
item = a->ob_item;
printf(
"关键点\n"
);
memmove(&item[ihigh+d], &item[ihigh],
(k - ihigh)*sizeof(PyObject *));
}
for
(k = 0; k < n; k++, ilow++) {
PyObject *w = vitem[k];
Py_XINCREF(w);
item[ilow] = w;
}
for
(k = norig - 1; k >= 0; --k)
Py_XDECREF(recycle[k]);
result = 0;
Error:
if
(recycle != recycle_on_stack)
PyMem_FREE(recycle);
Py_XDECREF(v_as_SF);
return
result;
#undef b
}