mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
* Catch PyFile_WriteString() and PyFile_WriteObject() errors * Clear the current exception on _PyObject_GetAttrId() failure * Use PyUnicode_CompareWithASCIIString() and PyFile_WriteObject() instead of _PyUnicode_AsString() and strcmp() to avoid Unicode encoding error. stderr has a more tolerant error handler than utf-8/strict.
This commit is contained in:
parent
e51321020c
commit
c82bfd871f
1 changed files with 65 additions and 43 deletions
108
Python/errors.c
108
Python/errors.c
|
@ -825,54 +825,76 @@ PyErr_WriteUnraisable(PyObject *obj)
|
||||||
{
|
{
|
||||||
_Py_IDENTIFIER(__module__);
|
_Py_IDENTIFIER(__module__);
|
||||||
PyObject *f, *t, *v, *tb;
|
PyObject *f, *t, *v, *tb;
|
||||||
PyErr_Fetch(&t, &v, &tb);
|
PyObject *moduleName = NULL;
|
||||||
f = PySys_GetObject("stderr");
|
char* className;
|
||||||
if (f != NULL && f != Py_None) {
|
|
||||||
if (obj) {
|
|
||||||
PyFile_WriteString("Exception ignored in: ", f);
|
|
||||||
PyFile_WriteObject(obj, f, 0);
|
|
||||||
PyFile_WriteString("\n", f);
|
|
||||||
}
|
|
||||||
PyTraceBack_Print(tb, f);
|
|
||||||
if (t) {
|
|
||||||
PyObject* moduleName;
|
|
||||||
char* className;
|
|
||||||
assert(PyExceptionClass_Check(t));
|
|
||||||
className = PyExceptionClass_Name(t);
|
|
||||||
if (className != NULL) {
|
|
||||||
char *dot = strrchr(className, '.');
|
|
||||||
if (dot != NULL)
|
|
||||||
className = dot+1;
|
|
||||||
}
|
|
||||||
|
|
||||||
moduleName = _PyObject_GetAttrId(t, &PyId___module__);
|
PyErr_Fetch(&t, &v, &tb);
|
||||||
if (moduleName == NULL)
|
|
||||||
PyFile_WriteString("<unknown>", f);
|
f = PySys_GetObject("stderr");
|
||||||
else {
|
if (f == NULL || f == Py_None)
|
||||||
char* modstr = _PyUnicode_AsString(moduleName);
|
goto done;
|
||||||
if (modstr &&
|
|
||||||
strcmp(modstr, "builtins") != 0)
|
if (obj) {
|
||||||
{
|
if (PyFile_WriteString("Exception ignored in: ", f) < 0)
|
||||||
PyFile_WriteString(modstr, f);
|
goto done;
|
||||||
PyFile_WriteString(".", f);
|
if (PyFile_WriteObject(obj, f, 0) < 0)
|
||||||
}
|
goto done;
|
||||||
}
|
if (PyFile_WriteString("\n", f) < 0)
|
||||||
if (className == NULL)
|
goto done;
|
||||||
PyFile_WriteString("<unknown>", f);
|
|
||||||
else
|
|
||||||
PyFile_WriteString(className, f);
|
|
||||||
if (v && v != Py_None) {
|
|
||||||
PyFile_WriteString(": ", f);
|
|
||||||
PyFile_WriteObject(v, f, Py_PRINT_RAW);
|
|
||||||
}
|
|
||||||
PyFile_WriteString("\n", f);
|
|
||||||
Py_XDECREF(moduleName);
|
|
||||||
}
|
|
||||||
PyErr_Clear(); /* Just in case */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (PyTraceBack_Print(tb, f) < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
if (!t)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
assert(PyExceptionClass_Check(t));
|
||||||
|
className = PyExceptionClass_Name(t);
|
||||||
|
if (className != NULL) {
|
||||||
|
char *dot = strrchr(className, '.');
|
||||||
|
if (dot != NULL)
|
||||||
|
className = dot+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
moduleName = _PyObject_GetAttrId(t, &PyId___module__);
|
||||||
|
if (moduleName == NULL) {
|
||||||
|
PyErr_Clear();
|
||||||
|
if (PyFile_WriteString("<unknown>", f) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) {
|
||||||
|
if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
|
||||||
|
goto done;
|
||||||
|
if (PyFile_WriteString(".", f) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (className == NULL) {
|
||||||
|
if (PyFile_WriteString("<unknown>", f) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (PyFile_WriteString(className, f) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v && v != Py_None) {
|
||||||
|
if (PyFile_WriteString(": ", f) < 0)
|
||||||
|
goto done;
|
||||||
|
if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (PyFile_WriteString("\n", f) < 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
done:
|
||||||
|
Py_XDECREF(moduleName);
|
||||||
Py_XDECREF(t);
|
Py_XDECREF(t);
|
||||||
Py_XDECREF(v);
|
Py_XDECREF(v);
|
||||||
Py_XDECREF(tb);
|
Py_XDECREF(tb);
|
||||||
|
PyErr_Clear(); /* Just in case */
|
||||||
}
|
}
|
||||||
|
|
||||||
extern PyObject *PyModule_GetWarningsModule(void);
|
extern PyObject *PyModule_GetWarningsModule(void);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue