python/dist/src/Objects listobject.c,2.214,2.215

Update of /cvsroot/python/python/dist/src/Objects In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20768 Modified Files: listobject.c Log Message: This is a reorganization of list_ass_slice(). It should probably be reviewed, though I tried to be very careful. This is a slight simplification, and it adds a new feature: a small stack-allocated "recycled" array for the cases when we don't remove too many items. It allows PyList_SetSlice() to never fail if: * you are sure that the object is a list; and * you either do not remove more than 8 items, or clear the list. This makes a number of other places in the source code correct again -- there are some places that delete a single item without checking for MemoryErrors raised by PyList_SetSlice(), or that clear the whole list, and sometimes the context doesn't allow an error to be propagated. Index: listobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/listobject.c,v retrieving revision 2.214 retrieving revision 2.215 diff -C2 -d -r2.214 -r2.215 *** listobject.c 30 Jul 2004 11:20:18 -0000 2.214 --- listobject.c 30 Jul 2004 11:38:22 -0000 2.215 *************** *** 518,521 **** --- 518,522 ---- list. :-( */ PyObject **recycle, **p; + PyObject *recycled[8]; PyObject **item; PyObject **vitem = NULL; *************** *** 560,565 **** } item = a->ob_item; ! if (ihigh > ilow) { ! p = recycle = PyMem_NEW(PyObject *, (ihigh-ilow)); if (recycle == NULL) { PyErr_NoMemory(); --- 561,568 ---- } item = a->ob_item; ! /* recycle the ihigh-ilow items that we are about to remove */ ! s = (ihigh - ilow)*sizeof(PyObject *); ! if (s > sizeof(recycled)) { ! recycle = (PyObject **)PyMem_MALLOC(s); if (recycle == NULL) { PyErr_NoMemory(); *************** *** 569,588 **** } else ! p = recycle = NULL; ! if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ ! memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *)); ! p += ihigh - ilow; ! if (d < 0) { ! memmove(&item[ihigh+d], &item[ihigh], ! (a->ob_size - ihigh)*sizeof(PyObject *)); ! list_resize(a, a->ob_size + d); ! item = a->ob_item; ! } } ! else { /* Insert d items; recycle ihigh-ilow items */ s = a->ob_size; if (list_resize(a, s+d) == -1) { ! if (recycle != NULL) ! PyMem_DEL(recycle); Py_XDECREF(v_as_SF); return -1; --- 572,589 ---- } else ! recycle = recycled; ! p = recycle + (ihigh - ilow); ! memcpy(recycle, &item[ilow], s); ! if (d < 0) { /* Delete -d items */ ! memmove(&item[ihigh+d], &item[ihigh], ! (a->ob_size - ihigh)*sizeof(PyObject *)); ! list_resize(a, a->ob_size + d); ! item = a->ob_item; } ! else if (d > 0) { /* Insert d items */ s = a->ob_size; if (list_resize(a, s+d) == -1) { ! if (recycle != recycled) ! PyMem_FREE(recycle); Py_XDECREF(v_as_SF); return -1; *************** *** 591,596 **** memmove(&item[ihigh+d], &item[ihigh], (s - ihigh)*sizeof(PyObject *)); - memcpy(p, &item[ilow], (ihigh - ilow)*sizeof(PyObject *)); - p += ihigh - ilow; } for (k = 0; k < n; k++, ilow++) { --- 592,595 ---- *************** *** 599,607 **** item[ilow] = w; } ! if (recycle) { ! while (--p >= recycle) ! Py_XDECREF(*p); ! PyMem_DEL(recycle); ! } Py_XDECREF(v_as_SF); return 0; --- 598,605 ---- item[ilow] = w; } ! while (--p >= recycle) ! Py_XDECREF(*p); ! if (recycle != recycled) ! PyMem_FREE(recycle); Py_XDECREF(v_as_SF); return 0;
participants (1)
-
arigoļ¼ users.sourceforge.net