comparison Python/pythonrun.c @ 6197:7da7ba5c6525 legacy-trunk

Added Py_UseClassExceptionsFlag, the variable containing the state of the -X command line option. Py_Initialize(): Handle the two phase initialization of the built-in module. Py_Finalize(): Handle the two phase finalization of the built-in module. parse_syntax_error(): New function which parses syntax errors that PyErr_Print() will catch. This correctly parses such errors regardless of whether PyExc_SyntaxError is an old-style string exception or new-fangled class exception. PyErr_Print(): Many changes: 1. Normalize the exception. 2. Handle SystemExit exceptions which might be class based. Digs the exit code out of the "code" attribute. String based SystemExit is handled the same as before. 3. Handle SyntaxError exceptions which might be class based. Digs the various information bits out of the instance's attributes (see parse_syntax_error() for details). String based SyntaxError still works too. 4. Don't write the `:' after the exception if the exception is class based and has an empty string str() value.
author Barry Warsaw <barry@python.org>
date Fri, 29 Aug 1997 22:07:17 +0000
parents 29ddf1049842
children dc974b3a6cf4
comparison
equal deleted inserted replaced
6196:ab045ed11bc9 6197:7da7ba5c6525
73 static void call_ll_exitfuncs Py_PROTO((void)); 73 static void call_ll_exitfuncs Py_PROTO((void));
74 74
75 int Py_DebugFlag; /* Needed by parser.c */ 75 int Py_DebugFlag; /* Needed by parser.c */
76 int Py_VerboseFlag; /* Needed by import.c */ 76 int Py_VerboseFlag; /* Needed by import.c */
77 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 77 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
78 int Py_UseClassExceptionsFlag; /* Needed by bltinmodule.c */
78 79
79 static int initialized = 0; 80 static int initialized = 0;
80 81
81 /* API to access the initialized flag -- useful for eroteric use */ 82 /* API to access the initialized flag -- useful for eroteric use */
82 83
125 126
126 interp->modules = PyDict_New(); 127 interp->modules = PyDict_New();
127 if (interp->modules == NULL) 128 if (interp->modules == NULL)
128 Py_FatalError("Py_Initialize: can't make modules dictionary"); 129 Py_FatalError("Py_Initialize: can't make modules dictionary");
129 130
130 bimod = _PyBuiltin_Init(); 131 bimod = _PyBuiltin_Init_1();
131 if (bimod == NULL) 132 if (bimod == NULL)
132 Py_FatalError("Py_Initialize: can't initialize __builtin__"); 133 Py_FatalError("Py_Initialize: can't initialize __builtin__");
133 interp->builtins = PyModule_GetDict(bimod); 134 interp->builtins = PyModule_GetDict(bimod);
134 Py_INCREF(interp->builtins); 135 Py_INCREF(interp->builtins);
135 _PyImport_FixupExtension("__builtin__", "__builtin__"); 136 _PyImport_FixupExtension("__builtin__", "__builtin__");
142 _PyImport_FixupExtension("sys", "sys"); 143 _PyImport_FixupExtension("sys", "sys");
143 PySys_SetPath(Py_GetPath()); 144 PySys_SetPath(Py_GetPath());
144 PyDict_SetItemString(interp->sysdict, "modules", 145 PyDict_SetItemString(interp->sysdict, "modules",
145 interp->modules); 146 interp->modules);
146 147
148 /* phase 2 of builtins */
149 _PyBuiltin_Init_2(interp->builtins);
150
147 _PyImport_Init(); 151 _PyImport_Init();
148 152
149 initsigs(); /* Signal handling stuff, including initintr() */ 153 initsigs(); /* Signal handling stuff, including initintr() */
150 154
151 initmain(); /* Module __main__ */ 155 initmain(); /* Module __main__ */
176 if (--initialized > 0) 180 if (--initialized > 0)
177 return; 181 return;
178 if (initialized < 0) 182 if (initialized < 0)
179 Py_FatalError("Py_Finalize: not initialized"); 183 Py_FatalError("Py_Finalize: not initialized");
180 184
185 /* We must call this before the current thread gets removed because
186 it decrefs class instances, which in turn save and restore the
187 current error state, which is a per thread data structure.
188 */
189 _PyBuiltin_Fini_1();
190
181 tstate = PyThreadState_Get(); 191 tstate = PyThreadState_Get();
182 interp = tstate->interp; 192 interp = tstate->interp;
183 193
184 PyImport_Cleanup(); 194 PyImport_Cleanup();
185 PyInterpreterState_Clear(interp); 195 PyInterpreterState_Clear(interp);
186 PyThreadState_Swap(NULL); 196 PyThreadState_Swap(NULL);
187 PyInterpreterState_Delete(interp); 197 PyInterpreterState_Delete(interp);
188 198
189 finisigs(); 199 finisigs();
190 _PyImport_Fini(); 200 _PyImport_Fini();
191 _PyBuiltin_Fini(); 201
202 /* Now we decref the exception classes. After this point nothing
203 can raise an exception. That's okay, because each Fini() method
204 below has been checked to make sure no exceptions are ever
205 raised.
206 */
207 _PyBuiltin_Fini_2();
192 PyMethod_Fini(); 208 PyMethod_Fini();
193 PyFrame_Fini(); 209 PyFrame_Fini();
194 PyCFunction_Fini(); 210 PyCFunction_Fini();
195 PyTuple_Fini(); 211 PyTuple_Fini();
196 PyString_Fini(); 212 PyString_Fini();
520 if (Py_FlushLine()) 536 if (Py_FlushLine())
521 PyErr_Clear(); 537 PyErr_Clear();
522 return 0; 538 return 0;
523 } 539 }
524 540
541 static int
542 parse_syntax_error(err, message, filename, lineno, offset, text)
543 PyObject* err;
544 PyObject** message;
545 char** filename;
546 int* lineno;
547 int* offset;
548 char** text;
549 {
550 long hold;
551 PyObject *v;
552
553 /* old style errors */
554 if (PyTuple_Check(err))
555 return PyArg_Parse(err, "(O(ziiz))", message, filename,
556 lineno, offset, text);
557
558 /* new style errors. `err' is an instance */
559
560 if (! (v = PyObject_GetAttrString(err, "msg")))
561 goto finally;
562 *message = v;
563
564 if (!(v = PyObject_GetAttrString(err, "filename")))
565 goto finally;
566 if (v == Py_None)
567 *filename = NULL;
568 else if (! (*filename = PyString_AsString(v)))
569 goto finally;
570
571 Py_DECREF(v);
572 if (!(v = PyObject_GetAttrString(err, "lineno")))
573 goto finally;
574 hold = PyInt_AsLong(v);
575 Py_DECREF(v);
576 v = NULL;
577 if (hold < 0 && PyErr_Occurred())
578 goto finally;
579 *lineno = (int)hold;
580
581 if (!(v = PyObject_GetAttrString(err, "offset")))
582 goto finally;
583 hold = PyInt_AsLong(v);
584 Py_DECREF(v);
585 v = NULL;
586 if (hold < 0 && PyErr_Occurred())
587 goto finally;
588 *offset = (int)hold;
589
590 if (!(v = PyObject_GetAttrString(err, "text")))
591 goto finally;
592 if (v == Py_None)
593 *text = NULL;
594 else if (! (*text = PyString_AsString(v)))
595 goto finally;
596 Py_DECREF(v);
597 return 1;
598
599 finally:
600 Py_XDECREF(v);
601 return 0;
602 }
603
525 void 604 void
526 PyErr_Print() 605 PyErr_Print()
527 { 606 {
528 int err = 0; 607 int err = 0;
529 PyObject *exception, *v, *tb, *f; 608 PyObject *exception, *v, *tb, *f;
530 PyErr_Fetch(&exception, &v, &tb); 609 PyErr_Fetch(&exception, &v, &tb);
610 PyErr_NormalizeException(&exception, &v, &tb);
611
531 if (exception == NULL) 612 if (exception == NULL)
532 return; 613 return;
614
533 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) { 615 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
534 err = Py_FlushLine(); 616 err = Py_FlushLine();
535 fflush(stdout); 617 fflush(stdout);
536 if (v == NULL || v == Py_None) 618 if (v == NULL || v == Py_None)
537 Py_Exit(0); 619 Py_Exit(0);
620 if (PyInstance_Check(v)) {
621 /* we expect the error code to be store in the
622 `code' attribute
623 */
624 PyObject *code = PyObject_GetAttrString(v, "code");
625 if (code) {
626 Py_DECREF(v);
627 v = code;
628 }
629 /* if we failed to dig out the "code" attribute,
630 then just let the else clause below print the
631 error
632 */
633 }
538 if (PyInt_Check(v)) 634 if (PyInt_Check(v))
539 Py_Exit((int)PyInt_AsLong(v)); 635 Py_Exit((int)PyInt_AsLong(v));
540 else { 636 else {
541 /* OK to use real stderr here */ 637 /* OK to use real stderr here */
542 PyObject_Print(v, stderr, Py_PRINT_RAW); 638 PyObject_Print(v, stderr, Py_PRINT_RAW);
559 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) 655 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
560 { 656 {
561 PyObject *message; 657 PyObject *message;
562 char *filename, *text; 658 char *filename, *text;
563 int lineno, offset; 659 int lineno, offset;
564 if (!PyArg_Parse(v, "(O(ziiz))", &message, 660 if (!parse_syntax_error(v, &message, &filename,
565 &filename, &lineno, &offset, &text)) 661 &lineno, &offset, &text))
566 PyErr_Clear(); 662 PyErr_Clear();
567 else { 663 else {
568 char buf[10]; 664 char buf[10];
569 PyFile_WriteString(" File \"", f); 665 PyFile_WriteString(" File \"", f);
570 if (filename == NULL) 666 if (filename == NULL)
628 } 724 }
629 else 725 else
630 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW); 726 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
631 if (err == 0) { 727 if (err == 0) {
632 if (v != NULL && v != Py_None) { 728 if (v != NULL && v != Py_None) {
633 err = PyFile_WriteString(": ", f); 729 PyObject *s = PyObject_Str(v);
730 /* only print colon if the str() of the
731 object is not the empty string
732 */
733 if (s && strcmp(PyString_AsString(s), ""))
734 err = PyFile_WriteString(": ", f);
634 if (err == 0) 735 if (err == 0)
635 err = PyFile_WriteObject(v, f, Py_PRINT_RAW); 736 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
636 } 737 }
637 } 738 }
638 if (err == 0) 739 if (err == 0)