Skip to content

Commit

Permalink
[3.12] gh-105375: Improve PyErr_WarnExplicit() error handling (GH-105610
Browse files Browse the repository at this point in the history
) (#105659)

Bail on first error to prevent exceptions from possibly being
overwritten.
(cherry picked from commit 567d6ae)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
  • Loading branch information
miss-islington and erlend-aasland authored Jun 11, 2023
1 parent c14f6ea commit db5022c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug in :c:func:`PyErr_WarnExplicit` where an exception could end up
being overwritten if the API failed internally.
28 changes: 16 additions & 12 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1301,25 +1301,29 @@ PyErr_WarnExplicit(PyObject *category, const char *text,
const char *module_str, PyObject *registry)
{
PyObject *message = PyUnicode_FromString(text);
if (message == NULL) {
return -1;
}
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
if (filename == NULL) {
Py_DECREF(message);
return -1;
}
PyObject *module = NULL;
int ret = -1;

if (message == NULL || filename == NULL)
goto exit;
if (module_str != NULL) {
module = PyUnicode_FromString(module_str);
if (module == NULL)
goto exit;
if (module == NULL) {
Py_DECREF(filename);
Py_DECREF(message);
return -1;
}
}

ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
module, registry);

exit:
Py_XDECREF(message);
int ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
module, registry);
Py_XDECREF(module);
Py_XDECREF(filename);
Py_DECREF(filename);
Py_DECREF(message);
return ret;
}

Expand Down

0 comments on commit db5022c

Please sign in to comment.