10000 bpo-26491 Defer DECREFs until enumobject is in a consistent state (#3… · python/cpython@8110dbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 8110dbd

Browse files
authored
bpo-26491 Defer DECREFs until enumobject is in a consistent state (#3747)
1 parent e6d9fcb commit 8110dbd

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

Objects/enumobject.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ enum_next_long(enumobject *en, PyObject* next_item)
103103
PyObject *result = en->en_result;
104104
PyObject *next_index;
105105
PyObject *stepped_up;
106+
PyObject *old_index;
107+
PyObject *old_item;
106108

107109
if (en->en_longindex == NULL) {
108110
en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
@@ -118,15 +120,19 @@ enum_next_long(enumobject *en, PyObject* next_item)
118120

119121
if (result->ob_refcnt == 1) {
120122
Py_INCREF(result);
121-
Py_DECREF(PyTuple_GET_ITEM(result, 0));
122-
Py_DECREF(PyTuple_GET_ITEM(result, 1));
123-
} else {
124-
result = PyTuple_New(2);
125-
if (result == NULL) {
126-
Py_DECREF(next_index);
127-
Py_DECREF(next_item);
128-
return NULL;
129-
}
123+
old_index = PyTuple_GET_ITEM(result, 0);
124+
old_item = PyTuple_GET_ITEM(result, 1);
125+
PyTuple_SET_ITEM(result, 0, next_index);
126+
PyTuple_SET_ITEM(result, 1, next_item);
127+
Py_DECREF(old_index);
128+
Py_DECREF(old_item);
129+
return result;
130+
}
131+
result = PyTuple_New(2);
132+
if (result == NULL) {
133+
Py_DECREF(next_index);
134+
Py_DECREF(next_item);
135+
return NULL;
130136
}
131137
PyTuple_SET_ITEM(result, 0, next_index);
132138
PyTuple_SET_ITEM(result, 1, next_item);
@@ -140,6 +146,8 @@ enum_next(enumobject *en)
140146
PyObject *next_item;
141147
PyObject *result = en->en_result;
142148
PyObject *it = en->en_sit;
149+
PyObject *old_index;
150+
PyObject *old_item;
143151

144152
next_item = (*Py_TYPE(it)->tp_iternext)(it);
145153
if (next_item == NULL)
@@ -157,15 +165,19 @@ enum_next(enumobject *en)
157165

158166
if (result->ob_refcnt == 1) {
159167
Py_INCREF(result);
160-
Py_DECREF(PyTuple_GET_ITEM(result, 0));
161-
Py_DECREF(PyTuple_GET_ITEM(result, 1));
162-
} else {
163-
result = PyTuple_New(2);
164-
if (result == NULL) {
165-
Py_DECREF(next_index);
166-
Py_DECREF(next_item);
167-
return NULL;
168-
}
168+
old_index = PyTuple_GET_ITEM(result, 0);
169+
old_item = PyTuple_GET_ITEM(result, 1);
170+
PyTuple_SET_ITEM(result, 0, next_index);
171+
PyTuple_SET_ITEM(result, 1, next_item);
172+
Py_DECREF(old_index);
173+
Py_DECREF(old_item);
174+
return result;
175+
}
176+
result = PyTuple_New(2);
177+
if (result == NULL) {
178+
Py_DECREF(next_index);
179+
Py_DECREF(next_item);
180+
return NULL;
169181
}
170182
PyTuple_SET_ITEM(result, 0, next_index);
171183
PyTuple_SET_ITEM(result, 1, next_item);

0 commit comments

Comments
 (0)
0