Commit 7357222d authored by Tim Peters's avatar Tim Peters

list_ass_slice(): The difference between "recycle" and "recycled" was

impossible to remember, so renamed one to something obvious.  Headed
off potential signed-vs-unsigned compiler complaints I introduced by
changing the type of a vrbl to unsigned.  Removed the need for the
tedious explanation about "backward pointer loops" by looping on an
int instead.
parent 8d9eb10c
...@@ -528,15 +528,15 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -528,15 +528,15 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
we must allocate an additional array, 'recycle', into which we must allocate an additional array, 'recycle', into which
we temporarily copy the items that are deleted from the we temporarily copy the items that are deleted from the
list. :-( */ list. :-( */
PyObject *recycled[8]; PyObject *recycle_on_stack[8];
PyObject **recycle = recycled; /* will allocate more if needed */ PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
PyObject **item; PyObject **item;
PyObject **vitem = NULL; PyObject **vitem = NULL;
PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */ PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
int n; /* # of elements in replacement list */ int n; /* # of elements in replacement list */
int norig; /* # of elements in list getting replaced */ int norig; /* # of elements in list getting replaced */
int d; /* Change in size */ int d; /* Change in size */
int k; /* Loop index */ int k;
size_t s; size_t s;
int result = -1; /* guilty until proved innocent */ int result = -1; /* guilty until proved innocent */
#define b ((PyListObject *)v) #define b ((PyListObject *)v)
...@@ -578,7 +578,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -578,7 +578,7 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
item = a->ob_item; item = a->ob_item;
/* recycle the items that we are about to remove */ /* recycle the items that we are about to remove */
s = norig * sizeof(PyObject *); s = norig * sizeof(PyObject *);
if (s > sizeof(recycled)) { if (s > sizeof(recycle_on_stack)) {
recycle = (PyObject **)PyMem_MALLOC(s); recycle = (PyObject **)PyMem_MALLOC(s);
if (recycle == NULL) { if (recycle == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
...@@ -594,30 +594,23 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v) ...@@ -594,30 +594,23 @@ list_ass_slice(PyListObject *a, int ilow, int ihigh, PyObject *v)
item = a->ob_item; item = a->ob_item;
} }
else if (d > 0) { /* Insert d items */ else if (d > 0) { /* Insert d items */
s = a->ob_size; k = a->ob_size;
if (list_resize(a, s+d) < 0) if (list_resize(a, k+d) < 0)
goto Error; goto Error;
item = a->ob_item; item = a->ob_item;
memmove(&item[ihigh+d], &item[ihigh], memmove(&item[ihigh+d], &item[ihigh],
(s - ihigh)*sizeof(PyObject *)); (k - ihigh)*sizeof(PyObject *));
} }
for (k = 0; k < n; k++, ilow++) { for (k = 0; k < n; k++, ilow++) {
PyObject *w = vitem[k]; PyObject *w = vitem[k];
Py_XINCREF(w); Py_XINCREF(w);
item[ilow] = w; item[ilow] = w;
} }
/* Convoluted: there's some obscure reason for wanting to do for (k = norig - 1; k >= 0; --k)
* the decrefs "backwards", but C doesn't guarantee you can compute Py_XDECREF(recycle[k]);
* a pointer to one slot *before* an allocated vector. So checking
* for item >= recycle is incorrect.
*/
for (item = recycle + norig; item > recycle; ) {
--item;
Py_XDECREF(*item);
}
result = 0; result = 0;
Error: Error:
if (recycle != recycled) if (recycle != recycle_on_stack)
PyMem_FREE(recycle); PyMem_FREE(recycle);
Py_XDECREF(v_as_SF); Py_XDECREF(v_as_SF);
return result; return result;
......
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