Skip to content

Commit

Permalink
bpo-36796: Clean the error handling in _testcapimodule.c (GH-13085)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackerySpytz committed Dec 14, 2023
1 parent e24eccb commit a723a13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
46 changes: 38 additions & 8 deletions Modules/_testcapi/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,48 +85,78 @@ make_timezones_capi(PyObject *self, PyObject *args)
{
PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
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_macro = PyTimeZone_FromOffsetAndName(offset, name);
PyObject *est_zone_macro_noname = PyTimeZone_FromOffset(offset);

Py_DecRef(offset);
Py_DecRef(name);

Py_DECREF(offset);
Py_DECREF(name);
if (est_zone_capi == NULL || est_zone_macro == NULL ||
est_zone_macro_noname == NULL)
{
goto error;
}
PyObject *rv = PyTuple_New(3);
if (rv == NULL) {
return NULL;
goto error;
}

PyTuple_SET_ITEM(rv, 0, est_zone_capi);
PyTuple_SET_ITEM(rv, 1, est_zone_macro);
PyTuple_SET_ITEM(rv, 2, est_zone_macro_noname);

return rv;
error:
Py_XDECREF(est_zone_capi);
Py_XDECREF(est_zone_macro);
Py_XDECREF(est_zone_macro_noname);
return NULL;
}

static PyObject *
get_timezones_offset_zero(PyObject *self, PyObject *args)
{
PyObject *offset = PyDelta_FromDSU(0, 0, 0);
PyObject *name = PyUnicode_FromString("");
if (offset == NULL || name == NULL) {
Py_XDECREF(offset);
Py_XDECREF(name);
return NULL;
}

// These two should return the UTC singleton
PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
PyObject *utc_singleton_1 = PyTimeZone_FromOffsetAndName(offset, NULL);

// This one will return +00:00 zone, but not the UTC singleton
PyObject *non_utc_zone = PyTimeZone_FromOffsetAndName(offset, name);

Py_DecRef(offset);
Py_DecRef(name);
Py_DECREF(offset);
Py_DECREF(name);
if (utc_singleton_0 == NULL || utc_singleton_1 == NULL ||
non_utc_zone == NULL)
{
goto error;
}

PyObject *rv = PyTuple_New(3);
if (rv == NULL) {
goto error;
}
PyTuple_SET_ITEM(rv, 0, utc_singleton_0);
PyTuple_SET_ITEM(rv, 1, utc_singleton_1);
PyTuple_SET_ITEM(rv, 2, non_utc_zone);

return rv;
error:
Py_XDECREF(utc_singleton_0);
Py_XDECREF(utc_singleton_1);
Py_XDECREF(non_utc_zone);
return NULL;
}

static PyObject *
Expand Down
18 changes: 12 additions & 6 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ test_dict_inner(int count)
for (i = 0; i < count; i++) {
v = PyLong_FromLong(i);
if (v == NULL) {
return -1;
goto error;
}
if (PyDict_SetItem(dict, v, v) < 0) {
Py_DECREF(v);
return -1;
goto error;
}
Py_DECREF(v);
}
Expand All @@ -214,11 +214,12 @@ test_dict_inner(int count)
assert(v != UNINITIALIZED_PTR);
i = PyLong_AS_LONG(v) + 1;
o = PyLong_FromLong(i);
if (o == NULL)
return -1;
if (o == NULL) {
goto error;
}
if (PyDict_SetItem(dict, k, o) < 0) {
Py_DECREF(o);
return -1;
goto error;
}
Py_DECREF(o);
k = v = UNINITIALIZED_PTR;
Expand All @@ -236,6 +237,9 @@ test_dict_inner(int count)
} else {
return 0;
}
error:
Py_DECREF(dict);
return -1;
}


Expand Down Expand Up @@ -1556,7 +1560,9 @@ test_structseq_newtype_doesnt_leak(PyObject *Py_UNUSED(self),
descr.n_in_sequence = 1;

PyTypeObject* structseq_type = PyStructSequence_NewType(&descr);
assert(structseq_type != NULL);
if (structseq_type == NULL) {
return NULL;
}
assert(PyType_Check(structseq_type));
assert(PyType_FastSubclass(structseq_type, Py_TPFLAGS_TUPLE_SUBCLASS));
Py_DECREF(structseq_type);
Expand Down

0 comments on commit a723a13

Please sign in to comment.