comparison Python/pythonrun.c @ 5607:cbb4e3cf086e legacy-trunk

Py_FlushLine and PyFile_WriteString now return error indicators instead of calling PyErr_Clear(). Add checking of those errors.
author Guido van Rossum <guido@python.org>
date Thu, 22 May 1997 22:35:04 +0000
parents 451c7ccf36b6
children 5ed3a21cc122
comparison
equal deleted inserted replaced
5606:495fbde64a73 5607:cbb4e3cf086e
52 52
53 #ifdef MS_WIN32 53 #ifdef MS_WIN32
54 #undef BYTE 54 #undef BYTE
55 #undef arglist 55 #undef arglist
56 #include "windows.h" 56 #include "windows.h"
57 #endif
58
59 #ifdef HAVE_GETPID
60 #ifndef MS_WINDOWS
61 #define HAVE_KILL
62 #endif
63 #endif 57 #endif
64 58
65 extern char *Py_GetPath(); 59 extern char *Py_GetPath();
66 60
67 extern grammar _PyParser_Grammar; /* From graminit.c */ 61 extern grammar _PyParser_Grammar; /* From graminit.c */
258 if (v == NULL) { 252 if (v == NULL) {
259 PyErr_Print(); 253 PyErr_Print();
260 return -1; 254 return -1;
261 } 255 }
262 Py_DECREF(v); 256 Py_DECREF(v);
263 Py_FlushLine(); 257 if (Py_FlushLine())
258 PyErr_Clear();
264 return 0; 259 return 0;
265 } 260 }
266 261
267 int 262 int
268 PyRun_SimpleFile(fp, filename) 263 PyRun_SimpleFile(fp, filename)
300 if (v == NULL) { 295 if (v == NULL) {
301 PyErr_Print(); 296 PyErr_Print();
302 return -1; 297 return -1;
303 } 298 }
304 Py_DECREF(v); 299 Py_DECREF(v);
305 Py_FlushLine(); 300 if (Py_FlushLine())
301 PyErr_Clear();
306 return 0; 302 return 0;
307 } 303 }
308 304
309 int 305 int
310 PyRun_SimpleString(command) 306 PyRun_SimpleString(command)
319 if (v == NULL) { 315 if (v == NULL) {
320 PyErr_Print(); 316 PyErr_Print();
321 return -1; 317 return -1;
322 } 318 }
323 Py_DECREF(v); 319 Py_DECREF(v);
324 Py_FlushLine(); 320 if (Py_FlushLine())
321 PyErr_Clear();
325 return 0; 322 return 0;
326 } 323 }
327 324
328 void 325 void
329 PyErr_Print() 326 PyErr_Print()
330 { 327 {
328 int err = 0;
331 PyObject *exception, *v, *tb, *f; 329 PyObject *exception, *v, *tb, *f;
332 PyErr_Fetch(&exception, &v, &tb); 330 PyErr_Fetch(&exception, &v, &tb);
333 Py_FlushLine();
334 fflush(stdout);
335 if (exception == NULL) 331 if (exception == NULL)
336 Py_FatalError("PyErr_Print called but no exception"); 332 return 0;
337 if (exception == PyExc_SystemExit) { 333 if (exception == PyExc_SystemExit) {
334 err = Py_FlushLine();
335 fflush(stdout);
338 if (v == NULL || v == Py_None) 336 if (v == NULL || v == Py_None)
339 Py_Exit(0); 337 Py_Exit(0);
340 if (PyInt_Check(v)) 338 if (PyInt_Check(v))
341 Py_Exit((int)PyInt_AsLong(v)); 339 Py_Exit((int)PyInt_AsLong(v));
342 else { 340 else {
351 PySys_SetObject("last_traceback", tb); 349 PySys_SetObject("last_traceback", tb);
352 f = PySys_GetObject("stderr"); 350 f = PySys_GetObject("stderr");
353 if (f == NULL) 351 if (f == NULL)
354 fprintf(stderr, "lost sys.stderr\n"); 352 fprintf(stderr, "lost sys.stderr\n");
355 else { 353 else {
356 PyTraceBack_Print(tb, f); 354 err = Py_FlushLine();
357 if (exception == PyExc_SyntaxError) { 355 fflush(stdout);
356 if (err == 0)
357 err = PyTraceBack_Print(tb, f);
358 if (err == 0 && exception == PyExc_SyntaxError) {
358 PyObject *message; 359 PyObject *message;
359 char *filename, *text; 360 char *filename, *text;
360 int lineno, offset; 361 int lineno, offset;
361 if (!PyArg_Parse(v, "(O(ziiz))", &message, 362 if (!PyArg_Parse(v, "(O(ziiz))", &message,
362 &filename, &lineno, &offset, &text)) 363 &filename, &lineno, &offset, &text))
403 PyFile_WriteString("^\n", f); 404 PyFile_WriteString("^\n", f);
404 } 405 }
405 Py_INCREF(message); 406 Py_INCREF(message);
406 Py_DECREF(v); 407 Py_DECREF(v);
407 v = message; 408 v = message;
409 /* Can't be bothered to check all those
410 PyFile_WriteString() calls */
411 if (PyErr_Occurred())
412 err = -1;
408 } 413 }
409 } 414 }
410 if (PyClass_Check(exception)) { 415 if (err) {
416 /* Don't do anything else */
417 }
418 else if (PyClass_Check(exception)) {
411 PyObject* className = 419 PyObject* className =
412 ((PyClassObject*)exception)->cl_name; 420 ((PyClassObject*)exception)->cl_name;
413 if (className == NULL) 421 if (className == NULL)
414 PyFile_WriteString("<unknown>", f); 422 err = PyFile_WriteString("<unknown>", f);
415 else { 423 else
416 if (PyFile_WriteObject(className, f, 424 err = PyFile_WriteObject(className, f,
417 Py_PRINT_RAW) != 0) 425 Py_PRINT_RAW);
418 PyErr_Clear(); 426 }
427 else
428 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
429 if (err == 0) {
430 if (v != NULL && v != Py_None) {
431 err = PyFile_WriteString(": ", f);
432 if (err == 0)
433 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
419 } 434 }
420 } else {
421 if (PyFile_WriteObject(exception, f,
422 Py_PRINT_RAW) != 0)
423 PyErr_Clear();
424 } 435 }
425 if (v != NULL && v != Py_None) { 436 if (err == 0)
426 PyFile_WriteString(": ", f); 437 err = PyFile_WriteString("\n", f);
427 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
428 PyErr_Clear();
429 }
430 PyFile_WriteString("\n", f);
431 } 438 }
432 Py_XDECREF(exception); 439 Py_XDECREF(exception);
433 Py_XDECREF(v); 440 Py_XDECREF(v);
434 Py_XDECREF(tb); 441 Py_XDECREF(tb);
442 /* If an error happened here, don't show it.
443 XXX This is wrong, but too many callers rely on this behavior. */
444 if (err != 0)
445 PyErr_Clear();
435 } 446 }
436 447
437 PyObject * 448 PyObject *
438 PyRun_String(str, start, globals, locals) 449 PyRun_String(str, start, globals, locals)
439 char *str; 450 char *str;