gh-105375: Improve PyErr_WarnExplicit() error handling (#105610)

Bail on first error to prevent exceptions from possibly being
overwritten.
This commit is contained in:
Erlend E. Aasland 2023-06-11 21:23:28 +02:00 committed by GitHub
parent 3f7c0810f6
commit 567d6ae8e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 12 deletions

View file

@ -1298,25 +1298,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;
}