Commit 78a1ed3d authored by Guido van Rossum's avatar Guido van Rossum

Py_FlushLine and PyFile_WriteString now return error indicators

instead of calling PyErr_Clear().  Add checking of those errors.
parent be27026c
...@@ -56,12 +56,6 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -56,12 +56,6 @@ PERFORMANCE OF THIS SOFTWARE.
#include "windows.h" #include "windows.h"
#endif #endif
#ifdef HAVE_GETPID
#ifndef MS_WINDOWS
#define HAVE_KILL
#endif
#endif
extern char *Py_GetPath(); extern char *Py_GetPath();
extern grammar _PyParser_Grammar; /* From graminit.c */ extern grammar _PyParser_Grammar; /* From graminit.c */
...@@ -260,7 +254,8 @@ PyRun_InteractiveOne(fp, filename) ...@@ -260,7 +254,8 @@ PyRun_InteractiveOne(fp, filename)
return -1; return -1;
} }
Py_DECREF(v); Py_DECREF(v);
Py_FlushLine(); if (Py_FlushLine())
PyErr_Clear();
return 0; return 0;
} }
...@@ -302,7 +297,8 @@ PyRun_SimpleFile(fp, filename) ...@@ -302,7 +297,8 @@ PyRun_SimpleFile(fp, filename)
return -1; return -1;
} }
Py_DECREF(v); Py_DECREF(v);
Py_FlushLine(); if (Py_FlushLine())
PyErr_Clear();
return 0; return 0;
} }
...@@ -321,20 +317,22 @@ PyRun_SimpleString(command) ...@@ -321,20 +317,22 @@ PyRun_SimpleString(command)
return -1; return -1;
} }
Py_DECREF(v); Py_DECREF(v);
Py_FlushLine(); if (Py_FlushLine())
PyErr_Clear();
return 0; return 0;
} }
void void
PyErr_Print() PyErr_Print()
{ {
int err = 0;
PyObject *exception, *v, *tb, *f; PyObject *exception, *v, *tb, *f;
PyErr_Fetch(&exception, &v, &tb); PyErr_Fetch(&exception, &v, &tb);
Py_FlushLine();
fflush(stdout);
if (exception == NULL) if (exception == NULL)
Py_FatalError("PyErr_Print called but no exception"); return 0;
if (exception == PyExc_SystemExit) { if (exception == PyExc_SystemExit) {
err = Py_FlushLine();
fflush(stdout);
if (v == NULL || v == Py_None) if (v == NULL || v == Py_None)
Py_Exit(0); Py_Exit(0);
if (PyInt_Check(v)) if (PyInt_Check(v))
...@@ -353,8 +351,11 @@ PyErr_Print() ...@@ -353,8 +351,11 @@ PyErr_Print()
if (f == NULL) if (f == NULL)
fprintf(stderr, "lost sys.stderr\n"); fprintf(stderr, "lost sys.stderr\n");
else { else {
PyTraceBack_Print(tb, f); err = Py_FlushLine();
if (exception == PyExc_SyntaxError) { fflush(stdout);
if (err == 0)
err = PyTraceBack_Print(tb, f);
if (err == 0 && exception == PyExc_SyntaxError) {
PyObject *message; PyObject *message;
char *filename, *text; char *filename, *text;
int lineno, offset; int lineno, offset;
...@@ -405,33 +406,43 @@ PyErr_Print() ...@@ -405,33 +406,43 @@ PyErr_Print()
Py_INCREF(message); Py_INCREF(message);
Py_DECREF(v); Py_DECREF(v);
v = message; v = message;
/* Can't be bothered to check all those
PyFile_WriteString() calls */
if (PyErr_Occurred())
err = -1;
} }
} }
if (PyClass_Check(exception)) { if (err) {
/* Don't do anything else */
}
else if (PyClass_Check(exception)) {
PyObject* className = PyObject* className =
((PyClassObject*)exception)->cl_name; ((PyClassObject*)exception)->cl_name;
if (className == NULL) if (className == NULL)
PyFile_WriteString("<unknown>", f); err = PyFile_WriteString("<unknown>", f);
else { else
if (PyFile_WriteObject(className, f, err = PyFile_WriteObject(className, f,
Py_PRINT_RAW) != 0) Py_PRINT_RAW);
PyErr_Clear();
}
} else {
if (PyFile_WriteObject(exception, f,
Py_PRINT_RAW) != 0)
PyErr_Clear();
} }
if (v != NULL && v != Py_None) { else
PyFile_WriteString(": ", f); err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0) if (err == 0) {
PyErr_Clear(); if (v != NULL && v != Py_None) {
err = PyFile_WriteString(": ", f);
if (err == 0)
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
}
} }
PyFile_WriteString("\n", f); if (err == 0)
err = PyFile_WriteString("\n", f);
} }
Py_XDECREF(exception); Py_XDECREF(exception);
Py_XDECREF(v); Py_XDECREF(v);
Py_XDECREF(tb); Py_XDECREF(tb);
/* If an error happened here, don't show it.
XXX This is wrong, but too many callers rely on this behavior. */
if (err != 0)
PyErr_Clear();
} }
PyObject * PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment