bpo-36796: Clean the error handling in _testcapimodule.c (GH-13085)

This commit is contained in:
Zackery Spytz 2023-12-14 11:06:53 -08:00 committed by GitHub
parent e24eccbc1c
commit a723a13bf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

View File

@ -85,17 +85,25 @@ make_timezones_capi(PyObject *self, PyObject *args)
{ {
PyObject *offset = PyDelta_FromDSU(0, -18000, 0); PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
PyObject *name = PyUnicode_FromString("EST"); PyObject *name = PyUnicode_FromString("EST");
if (offset == NULL || name == NULL) {
Py_XDECREF(offset);
Py_XDECREF(name);
return NULL;
}
PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name); PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name); PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset); PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);
Py_DECREF(offset);
Py_DecRef(offset); Py_DECREF(name);
Py_DecRef(name); if (est_zone_capi == NULL || est_zone_macro == NULL ||
est_zone_macro_noname == NULL)
{
goto error;
}
PyObject *rv = PyTuple_New(3); PyObject *rv = PyTuple_New(3);
if (rv == NULL) { if (rv == NULL) {
return NULL; goto error;
} }
PyTuple_SET_ITEM(rv, 0, est_zone_capi); PyTuple_SET_ITEM(rv, 0, est_zone_capi);
@ -103,6 +111,11 @@ make_timezones_capi(PyObject *self, PyObject *args)
PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname); PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);
return rv; return rv;
error:
Py_XDECREF(est_zone_capi);
Py_XDECREF(est_zone_macro);
Py_XDECREF(est_zone_macro_noname);
return NULL;
} }
static PyObject * static PyObject *
@ -110,6 +123,11 @@ get_timezones_offset_zero(PyObject *self, PyObject *args)
{ {
PyObject *offset = PyDelta_FromDSU(0, 0, 0); PyObject *offset = PyDelta_FromDSU(0, 0, 0);
PyObject *name = PyUnicode_FromString(""); PyObject *name = PyUnicode_FromString("");
if (offset == NULL || name == NULL) {
Py_XDECREF(offset);
Py_XDECREF(name);
return NULL;
}
// These two should return the UTC singleton // These two should return the UTC singleton
PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset); PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
@ -117,16 +135,28 @@ get_timezones_offset_zero(PyObject *self, PyObject *args)
// This one will return +00:00 zone, but not the UTC singleton // This one will return +00:00 zone, but not the UTC singleton
PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name); PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);
Py_DECREF(offset);
Py_DecRef(offset); Py_DECREF(name);
Py_DecRef(name); if (utc_singleton_0 == NULL || utc_singleton_1 == NULL ||
non_utc_zone == NULL)
{
goto error;
}
PyObject *rv = PyTuple_New(3); PyObject *rv = PyTuple_New(3);
if (rv == NULL) {
goto error;
}
PyTuple_SET_ITEM(rv, 0, utc_singleton_0); PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
PyTuple_SET_ITEM(rv, 1, utc_singleton_1); PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
PyTuple_SET_ITEM(rv, 2, non_utc_zone); PyTuple_SET_ITEM(rv, 2, non_utc_zone);
return rv; return rv;
error:
Py_XDECREF(utc_singleton_0);
Py_XDECREF(utc_singleton_1);
Py_XDECREF(non_utc_zone);
return NULL;
} }
static PyObject * static PyObject *

View File

@ -196,11 +196,11 @@ test_dict_inner(int count)
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
v = PyLong_FromLong(i); v = PyLong_FromLong(i);
if (v == NULL) { if (v == NULL) {
return -1; goto error;
} }
if (PyDict_SetItem(dict, v, v) < 0) { if (PyDict_SetItem(dict, v, v) < 0) {
Py_DECREF(v); Py_DECREF(v);
return -1; goto error;
} }
Py_DECREF(v); Py_DECREF(v);
} }
@ -214,11 +214,12 @@ test_dict_inner(int count)
assert(v != UNINITIALIZED_PTR); assert(v != UNINITIALIZED_PTR);
i = PyLong_AS_LONG(v) + 1; i = PyLong_AS_LONG(v) + 1;
o = PyLong_FromLong(i); o = PyLong_FromLong(i);
if (o == NULL) if (o == NULL) {
return -1; goto error;
}
if (PyDict_SetItem(dict, k, o) < 0) { if (PyDict_SetItem(dict, k, o) < 0) {
Py_DECREF(o); Py_DECREF(o);
return -1; goto error;
} }
Py_DECREF(o); Py_DECREF(o);
k = v = UNINITIALIZED_PTR; k = v = UNINITIALIZED_PTR;
@ -236,6 +237,9 @@ test_dict_inner(int count)
} else { } else {
return 0; return 0;
} }
error:
Py_DECREF(dict);
return -1;
} }
@ -1556,7 +1560,9 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
descr.n_in_sequence = 1; descr.n_in_sequence = 1;
PyTypeObject* structseq_type = PyStructSequence_NewType(&descr); PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
assert(structseq_type != NULL); if (structseq_type == NULL) {
return NULL;
}
assert(PyType_Check(structseq_type)); assert(PyType_Check(structseq_type));
assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS)); assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
Py_DECREF(structseq_type); Py_DECREF(structseq_type);