Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f74efa

Browse files
committed
Fix error handling
1 parent 5186c35 commit 0f74efa

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Objects/structseq.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,9 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
394394
return NULL;
395395
}
396396

397-
// Create an identical instance.
398-
result = (PyStructSequence *)PyStructSequence_New(Py_TYPE(self));
399-
if (result == NULL) {
400-
return NULL;
401-
}
402-
for (i = 0; i < n_fields; ++i) {
403-
result->ob_item[i] = Py_NewRef(self->ob_item[i]);
397+
result = (PyStructSequence *) PyStructSequence_New(Py_TYPE(self));
398+
if (!result) {
399+
goto error;
404400
}
405401

406402
if (kwargs != NULL) {
@@ -409,14 +405,13 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
409405
for (i = 0; i < n_fields; i++) {
410406
PyObject *key = PyUnicode_FromString(Py_TYPE(self)->tp_members[i].name);
411407
if (!key) {
412-
return NULL;
408+
goto error;
413409
}
414410
PyObject *ob = _PyDict_Pop(kwargs, key, self->ob_item[i]); // borrowed
415411
Py_DECREF(key);
416412
if (!ob) {
417-
return NULL;
413+
goto error;
418414
}
419-
Py_DECREF(result->ob_item[i]);
420415
result->ob_item[i] = ob;
421416
}
422417
// Check if there are any unexpected fields.
@@ -426,11 +421,22 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs)
426421
PyErr_Format(PyExc_TypeError, "Got unexpected field name(s): %R", names);
427422
Py_DECREF(names);
428423
}
429-
return NULL;
424+
goto error;
425+
}
426+
}
427+
else
428+
{
429+
// Just create a copy of the original.
430+
for (i = 0; i < n_fields; ++i) {
431+
result->ob_item[i] = Py_NewRef(self->ob_item[i]);
430432
}
431433
}
432434

433435
return result;
436+
437+
error:
438+
Py_XDECREF(result);
439+
return NULL;
434440
}
435441

436442
static PyMethodDef structseq_methods[] = {

0 commit comments

Comments
 (0)