From 4ceb5c4924f8a63ddb45aac001565bb36cf94c1e Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:54:00 -0700 Subject: [PATCH] [3.11] gh-105375: Improve error handling in `zoneinfo` module (GH-105586) (#105613) Fix bugs where exceptions could end up being overwritten because of deferred error handling. (cherry picked from commit 33c92c4f15539806c8aff8574ff30a8b307e3e4d) Co-authored-by: Nikita Sobolev Co-authored-by: Erlend E. Aasland --- ...-06-09-21-11-28.gh-issue-105375.4Mxn7t.rst | 1 + Modules/_zoneinfo.c | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-06-09-21-11-28.gh-issue-105375.4Mxn7t.rst diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-11-28.gh-issue-105375.4Mxn7t.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-11-28.gh-issue-105375.4Mxn7t.rst new file mode 100644 index 00000000000..4202b758d1d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-06-09-21-11-28.gh-issue-105375.4Mxn7t.rst @@ -0,0 +1 @@ +Fix bugs in :mod:`zoneinfo` where exceptions could be overwritten. diff --git a/Modules/_zoneinfo.c b/Modules/_zoneinfo.c index 55975f4e269..5f584ff93e9 100644 --- a/Modules/_zoneinfo.c +++ b/Modules/_zoneinfo.c @@ -581,14 +581,19 @@ zoneinfo_fromutc(PyObject *obj_self, PyObject *dt) } else { PyObject *replace = PyObject_GetAttrString(tmp, "replace"); - PyObject *args = PyTuple_New(0); - PyObject *kwargs = PyDict_New(); - Py_DECREF(tmp); - if (args == NULL || kwargs == NULL || replace == NULL) { - Py_XDECREF(args); - Py_XDECREF(kwargs); - Py_XDECREF(replace); + if (replace == NULL) { + return NULL; + } + PyObject *args = PyTuple_New(0); + if (args == NULL) { + Py_DECREF(replace); + return NULL; + } + PyObject *kwargs = PyDict_New(); + if (kwargs == NULL) { + Py_DECREF(replace); + Py_DECREF(args); return NULL; }