Issue #19437: Fix show_warning() of _warnings, stop at the first error to not

call a Python function with an exception set
This commit is contained in:
Victor Stinner 2013-10-31 14:51:38 +01:00
parent 3cd04aa1b2
commit ae233eab5c
1 changed files with 16 additions and 10 deletions

View File

@ -263,23 +263,28 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
name = _PyObject_GetAttrId(category, &PyId___name__); name = _PyObject_GetAttrId(category, &PyId___name__);
if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */
return; goto error;
f_stderr = PySys_GetObject("stderr"); f_stderr = PySys_GetObject("stderr");
if (f_stderr == NULL) { if (f_stderr == NULL) {
fprintf(stderr, "lost sys.stderr\n"); fprintf(stderr, "lost sys.stderr\n");
Py_DECREF(name); goto error;
return;
} }
/* Print "filename:lineno: category: text\n" */ /* Print "filename:lineno: category: text\n" */
PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW); if (PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW) < 0)
PyFile_WriteString(lineno_str, f_stderr); goto error;
PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW); if (PyFile_WriteString(lineno_str, f_stderr) < 0)
PyFile_WriteString(": ", f_stderr); goto error;
PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW); if (PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW) < 0)
PyFile_WriteString("\n", f_stderr); goto error;
Py_XDECREF(name); if (PyFile_WriteString(": ", f_stderr) < 0)
goto error;
if (PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW) < 0)
goto error;
if (PyFile_WriteString("\n", f_stderr) < 0)
goto error;
Py_CLEAR(name);
/* Print " source_line\n" */ /* Print " source_line\n" */
if (sourceline) { if (sourceline) {
@ -314,6 +319,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
} }
error: error:
Py_XDECREF(name);
PyErr_Clear(); PyErr_Clear();
} }