bpo-26491 Defer DECREFs until enumobject is in a consistent state (#3… · python/cpython@8110dbd (original) (raw)

`@@ -103,6 +103,8 @@ enum_next_long(enumobject en, PyObject next_item)

`

103

103

`PyObject *result = en->en_result;

`

104

104

`PyObject *next_index;

`

105

105

`PyObject *stepped_up;

`

``

106

`+

PyObject *old_index;

`

``

107

`+

PyObject *old_item;

`

106

108

``

107

109

`if (en->en_longindex == NULL) {

`

108

110

`en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);

`

`@@ -118,15 +120,19 @@ enum_next_long(enumobject en, PyObject next_item)

`

118

120

``

119

121

`if (result->ob_refcnt == 1) {

`

120

122

`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;

`

130

136

` }

`

131

137

`PyTuple_SET_ITEM(result, 0, next_index);

`

132

138

`PyTuple_SET_ITEM(result, 1, next_item);

`

`@@ -140,6 +146,8 @@ enum_next(enumobject *en)

`

140

146

`PyObject *next_item;

`

141

147

`PyObject *result = en->en_result;

`

142

148

`PyObject *it = en->en_sit;

`

``

149

`+

PyObject *old_index;

`

``

150

`+

PyObject *old_item;

`

143

151

``

144

152

`next_item = (*Py_TYPE(it)->tp_iternext)(it);

`

145

153

`if (next_item == NULL)

`

`@@ -157,15 +165,19 @@ enum_next(enumobject *en)

`

157

165

``

158

166

`if (result->ob_refcnt == 1) {

`

159

167

`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;

`

169

181

` }

`

170

182

`PyTuple_SET_ITEM(result, 0, next_index);

`

171

183

`PyTuple_SET_ITEM(result, 1, next_item);

`