Skip to content

Commit db5022c

Browse files
[3.12] gh-105375: Improve PyErr_WarnExplicit() error handling (GH-105610) (#105659)
Bail on first error to prevent exceptions from possibly being overwritten. (cherry picked from commit 567d6ae) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent c14f6ea commit db5022c

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug in :c:func:`PyErr_WarnExplicit` where an exception could end up
2+
being overwritten if the API failed internally.

Python/_warnings.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,25 +1301,29 @@ PyErr_WarnExplicit(PyObject *category, const char *text,
13011301
const char *module_str, PyObject *registry)
13021302
{
13031303
PyObject *message = PyUnicode_FromString(text);
1304+
if (message == NULL) {
1305+
return -1;
1306+
}
13041307
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1308+
if (filename == NULL) {
1309+
Py_DECREF(message);
1310+
return -1;
1311+
}
13051312
PyObject *module = NULL;
1306-
int ret = -1;
1307-
1308-
if (message == NULL || filename == NULL)
1309-
goto exit;
13101313
if (module_str != NULL) {
13111314
module = PyUnicode_FromString(module_str);
1312-
if (module == NULL)
1313-
goto exit;
1315+
if (module == NULL) {
1316+
Py_DECREF(filename);
1317+
Py_DECREF(message);
1318+
return -1;
1319+
}
13141320
}
13151321

1316-
ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1317-
module, registry);
1318-
1319-
exit:
1320-
Py_XDECREF(message);
1322+
int ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1323+
module, registry);
13211324
Py_XDECREF(module);
1322-
Py_XDECREF(filename);
1325+
Py_DECREF(filename);
1326+
Py_DECREF(message);
13231327
return ret;
13241328
}
13251329

0 commit comments

Comments
 (0)