bpo-26491 Defer DECREFs until enumobject is in a consistent state (#3747)

This commit is contained in:
Raymond Hettinger 2017-09-25 02:15:53 -07:00 committed by GitHub
parent e6d9fcbb8d
commit 8110dbd470
1 changed files with 30 additions and 18 deletions

View File

@ -103,6 +103,8 @@ enum_next_long(enumobject *en, PyObject* next_item)
PyObject *result = en->en_result; PyObject *result = en->en_result;
PyObject *next_index; PyObject *next_index;
PyObject *stepped_up; PyObject *stepped_up;
PyObject *old_index;
PyObject *old_item;
if (en->en_longindex == NULL) { if (en->en_longindex == NULL) {
en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX); en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
@ -118,16 +120,20 @@ enum_next_long(enumobject *en, PyObject* next_item)
if (result->ob_refcnt == 1) { if (result->ob_refcnt == 1) {
Py_INCREF(result); Py_INCREF(result);
Py_DECREF(PyTuple_GET_ITEM(result, 0)); old_index = PyTuple_GET_ITEM(result, 0);
Py_DECREF(PyTuple_GET_ITEM(result, 1)); old_item = PyTuple_GET_ITEM(result, 1);
} else { PyTuple_SET_ITEM(result, 0, next_index);
PyTuple_SET_ITEM(result, 1, next_item);
Py_DECREF(old_index);
Py_DECREF(old_item);
return result;
}
result = PyTuple_New(2); result = PyTuple_New(2);
if (result == NULL) { if (result == NULL) {
Py_DECREF(next_index); Py_DECREF(next_index);
Py_DECREF(next_item); Py_DECREF(next_item);
return NULL; return NULL;
} }
}
PyTuple_SET_ITEM(result, 0, next_index); PyTuple_SET_ITEM(result, 0, next_index);
PyTuple_SET_ITEM(result, 1, next_item); PyTuple_SET_ITEM(result, 1, next_item);
return result; return result;
@ -140,6 +146,8 @@ enum_next(enumobject *en)
PyObject *next_item; PyObject *next_item;
PyObject *result = en->en_result; PyObject *result = en->en_result;
PyObject *it = en->en_sit; PyObject *it = en->en_sit;
PyObject *old_index;
PyObject *old_item;
next_item = (*Py_TYPE(it)->tp_iternext)(it); next_item = (*Py_TYPE(it)->tp_iternext)(it);
if (next_item == NULL) if (next_item == NULL)
@ -157,16 +165,20 @@ enum_next(enumobject *en)
if (result->ob_refcnt == 1) { if (result->ob_refcnt == 1) {
Py_INCREF(result); Py_INCREF(result);
Py_DECREF(PyTuple_GET_ITEM(result, 0)); old_index = PyTuple_GET_ITEM(result, 0);
Py_DECREF(PyTuple_GET_ITEM(result, 1)); old_item = PyTuple_GET_ITEM(result, 1);
} else { PyTuple_SET_ITEM(result, 0, next_index);
PyTuple_SET_ITEM(result, 1, next_item);
Py_DECREF(old_index);
Py_DECREF(old_item);
return result;
}
result = PyTuple_New(2); result = PyTuple_New(2);
if (result == NULL) { if (result == NULL) {
Py_DECREF(next_index); Py_DECREF(next_index);
Py_DECREF(next_item); Py_DECREF(next_item);
return NULL; return NULL;
} }
}
PyTuple_SET_ITEM(result, 0, next_index); PyTuple_SET_ITEM(result, 0, next_index);
PyTuple_SET_ITEM(result, 1, next_item); PyTuple_SET_ITEM(result, 1, next_item);
return result; return result;