Fixup error exits in nlargest() and nsmallest().

This commit is contained in:
Raymond Hettinger 2004-06-13 15:36:56 +00:00
parent bb6b7346ce
commit de72eddf69
1 changed files with 16 additions and 8 deletions

View File

@ -230,13 +230,17 @@ nlargest(PyObject *self, PyObject *args)
return NULL; return NULL;
heap = PyList_New(0); heap = PyList_New(0);
if (it == NULL) if (heap == NULL)
goto fail; goto fail;
for (i=0 ; i<n ; i++ ){ for (i=0 ; i<n ; i++ ){
elem = PyIter_Next(it); elem = PyIter_Next(it);
if (elem == NULL) if (elem == NULL) {
goto sortit; if (PyErr_Occurred())
goto fail;
else
goto sortit;
}
if (PyList_Append(heap, elem) == -1) { if (PyList_Append(heap, elem) == -1) {
Py_DECREF(elem); Py_DECREF(elem);
goto fail; goto fail;
@ -271,11 +275,11 @@ nlargest(PyObject *self, PyObject *args)
sol = PyList_GET_ITEM(heap, 0); sol = PyList_GET_ITEM(heap, 0);
} }
sortit: sortit:
Py_DECREF(it);
if (PyList_Sort(heap) == -1) if (PyList_Sort(heap) == -1)
goto fail; goto fail;
if (PyList_Reverse(heap) == -1) if (PyList_Reverse(heap) == -1)
goto fail; goto fail;
Py_DECREF(it);
return heap; return heap;
fail: fail:
@ -385,13 +389,17 @@ nsmallest(PyObject *self, PyObject *args)
return NULL; return NULL;
heap = PyList_New(0); heap = PyList_New(0);
if (it == NULL) if (heap == NULL)
goto fail; goto fail;
for (i=0 ; i<n ; i++ ){ for (i=0 ; i<n ; i++ ){
elem = PyIter_Next(it); elem = PyIter_Next(it);
if (elem == NULL) if (elem == NULL) {
goto sortit; if (PyErr_Occurred())
goto fail;
else
goto sortit;
}
if (PyList_Append(heap, elem) == -1) { if (PyList_Append(heap, elem) == -1) {
Py_DECREF(elem); Py_DECREF(elem);
goto fail; goto fail;
@ -429,9 +437,9 @@ nsmallest(PyObject *self, PyObject *args)
} }
sortit: sortit:
Py_DECREF(it);
if (PyList_Sort(heap) == -1) if (PyList_Sort(heap) == -1)
goto fail; goto fail;
Py_DECREF(it);
return heap; return heap;
fail: fail: