Skip to content

Commit 05c73e1

Browse files
[3.11] gh-105375: Improve PyErr_WarnExplicit() error handling (GH-105610) (#105660)
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 87e493b commit 05c73e1

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
@@ -1231,25 +1231,29 @@ PyErr_WarnExplicit(PyObject *category, const char *text,
12311231
const char *module_str, PyObject *registry)
12321232
{
12331233
PyObject *message = PyUnicode_FromString(text);
1234+
if (message == NULL) {
1235+
return -1;
1236+
}
12341237
PyObject *filename = PyUnicode_DecodeFSDefault(filename_str);
1238+
if (filename == NULL) {
1239+
Py_DECREF(message);
1240+
return -1;
1241+
}
12351242
PyObject *module = NULL;
1236-
int ret = -1;
1237-
1238-
if (message == NULL || filename == NULL)
1239-
goto exit;
12401243
if (module_str != NULL) {
12411244
module = PyUnicode_FromString(module_str);
1242-
if (module == NULL)
1243-
goto exit;
1245+
if (module == NULL) {
1246+
Py_DECREF(filename);
1247+
Py_DECREF(message);
1248+
return -1;
1249+
}
12441250
}
12451251

1246-
ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1247-
module, registry);
1248-
1249-
exit:
1250-
Py_XDECREF(message);
1252+
int ret = PyErr_WarnExplicitObject(category, message, filename, lineno,
1253+
module, registry);
12511254
Py_XDECREF(module);
1252-
Py_XDECREF(filename);
1255+
Py_DECREF(filename);
1256+
Py_DECREF(message);
12531257
return ret;
12541258
}
12551259

0 commit comments

Comments
 (0)