Apparently the code to forestall Tk eating events was too aggressive (Tk user input...
[python/dscho.git] / Python / pythonrun.c
blobd7b8872b57c2e1c9d8b64258c5e5f16783d1af5b
2 /* Python interpreter top-level routines, including init/exit */
4 #include "Python.h"
6 #include "grammar.h"
7 #include "node.h"
8 #include "token.h"
9 #include "parsetok.h"
10 #include "errcode.h"
11 #include "compile.h"
12 #include "symtable.h"
13 #include "eval.h"
14 #include "marshal.h"
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
20 #ifdef HAVE_SIGNAL_H
21 #include <signal.h>
22 #endif
24 #ifdef MS_WIN32
25 #undef BYTE
26 #include "windows.h"
27 #endif
29 #ifdef macintosh
30 #include "macglue.h"
31 #endif
32 extern char *Py_GetPath(void);
34 extern grammar _PyParser_Grammar; /* From graminit.c */
36 /* Forward */
37 static void initmain(void);
38 static void initsite(void);
39 static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
40 PyCompilerFlags *);
41 static PyObject *run_node(node *, char *, PyObject *, PyObject *,
42 PyCompilerFlags *);
43 static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *,
44 PyCompilerFlags *);
45 static void err_input(perrdetail *);
46 static void initsigs(void);
47 static void call_sys_exitfunc(void);
48 static void call_ll_exitfuncs(void);
50 #ifdef Py_TRACE_REFS
51 int _Py_AskYesNo(char *prompt);
52 #endif
54 extern void _PyUnicode_Init(void);
55 extern void _PyUnicode_Fini(void);
56 extern void _PyCodecRegistry_Init(void);
57 extern void _PyCodecRegistry_Fini(void);
59 int Py_DebugFlag; /* Needed by parser.c */
60 int Py_VerboseFlag; /* Needed by import.c */
61 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
62 int Py_NoSiteFlag; /* Suppress 'import site' */
63 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
64 int Py_FrozenFlag; /* Needed by getpath.c */
65 int Py_UnicodeFlag = 0; /* Needed by compile.c */
67 static int initialized = 0;
69 /* API to access the initialized flag -- useful for esoteric use */
71 int
72 Py_IsInitialized(void)
74 return initialized;
77 /* Global initializations. Can be undone by Py_Finalize(). Don't
78 call this twice without an intervening Py_Finalize() call. When
79 initializations fail, a fatal error is issued and the function does
80 not return. On return, the first thread and interpreter state have
81 been created.
83 Locking: you must hold the interpreter lock while calling this.
84 (If the lock has not yet been initialized, that's equivalent to
85 having the lock, but you cannot use multiple threads.)
89 void
90 Py_Initialize(void)
92 PyInterpreterState *interp;
93 PyThreadState *tstate;
94 PyObject *bimod, *sysmod;
95 char *p;
97 if (initialized)
98 return;
99 initialized = 1;
101 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
102 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
103 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
104 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
105 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
106 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
108 interp = PyInterpreterState_New();
109 if (interp == NULL)
110 Py_FatalError("Py_Initialize: can't make first interpreter");
112 tstate = PyThreadState_New(interp);
113 if (tstate == NULL)
114 Py_FatalError("Py_Initialize: can't make first thread");
115 (void) PyThreadState_Swap(tstate);
117 interp->modules = PyDict_New();
118 if (interp->modules == NULL)
119 Py_FatalError("Py_Initialize: can't make modules dictionary");
121 /* Init codec registry */
122 _PyCodecRegistry_Init();
124 /* Init Unicode implementation; relies on the codec registry */
125 _PyUnicode_Init();
127 bimod = _PyBuiltin_Init();
128 if (bimod == NULL)
129 Py_FatalError("Py_Initialize: can't initialize __builtin__");
130 interp->builtins = PyModule_GetDict(bimod);
131 Py_INCREF(interp->builtins);
133 sysmod = _PySys_Init();
134 if (sysmod == NULL)
135 Py_FatalError("Py_Initialize: can't initialize sys");
136 interp->sysdict = PyModule_GetDict(sysmod);
137 Py_INCREF(interp->sysdict);
138 _PyImport_FixupExtension("sys", "sys");
139 PySys_SetPath(Py_GetPath());
140 PyDict_SetItemString(interp->sysdict, "modules",
141 interp->modules);
143 _PyImport_Init();
145 /* initialize builtin exceptions */
146 init_exceptions();
148 /* phase 2 of builtins */
149 _PyImport_FixupExtension("__builtin__", "__builtin__");
151 initsigs(); /* Signal handling stuff, including initintr() */
153 initmain(); /* Module __main__ */
154 if (!Py_NoSiteFlag)
155 initsite(); /* Module site */
158 #ifdef COUNT_ALLOCS
159 extern void dump_counts(void);
160 #endif
162 /* Undo the effect of Py_Initialize().
164 Beware: if multiple interpreter and/or thread states exist, these
165 are not wiped out; only the current thread and interpreter state
166 are deleted. But since everything else is deleted, those other
167 interpreter and thread states should no longer be used.
169 (XXX We should do better, e.g. wipe out all interpreters and
170 threads.)
172 Locking: as above.
176 void
177 Py_Finalize(void)
179 PyInterpreterState *interp;
180 PyThreadState *tstate;
182 if (!initialized)
183 return;
185 /* The interpreter is still entirely intact at this point, and the
186 * exit funcs may be relying on that. In particular, if some thread
187 * or exit func is still waiting to do an import, the import machinery
188 * expects Py_IsInitialized() to return true. So don't say the
189 * interpreter is uninitialized until after the exit funcs have run.
190 * Note that Threading.py uses an exit func to do a join on all the
191 * threads created thru it, so this also protects pending imports in
192 * the threads created via Threading.
194 call_sys_exitfunc();
195 initialized = 0;
197 /* Get current thread state and interpreter pointer */
198 tstate = PyThreadState_Get();
199 interp = tstate->interp;
201 /* Disable signal handling */
202 PyOS_FiniInterrupts();
204 /* Cleanup Unicode implementation */
205 _PyUnicode_Fini();
207 /* Cleanup Codec registry */
208 _PyCodecRegistry_Fini();
210 /* Destroy all modules */
211 PyImport_Cleanup();
213 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
214 _PyImport_Fini();
216 /* Debugging stuff */
217 #ifdef COUNT_ALLOCS
218 dump_counts();
219 #endif
221 #ifdef Py_REF_DEBUG
222 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
223 #endif
225 #ifdef Py_TRACE_REFS
226 if (
227 #ifdef MS_WINDOWS /* Only ask on Windows if env var set */
228 getenv("PYTHONDUMPREFS") &&
229 #endif /* MS_WINDOWS */
230 _Py_AskYesNo("Print left references?")) {
231 _Py_PrintReferences(stderr);
233 #endif /* Py_TRACE_REFS */
235 /* Now we decref the exception classes. After this point nothing
236 can raise an exception. That's okay, because each Fini() method
237 below has been checked to make sure no exceptions are ever
238 raised.
240 fini_exceptions();
242 /* Delete current thread */
243 PyInterpreterState_Clear(interp);
244 PyThreadState_Swap(NULL);
245 PyInterpreterState_Delete(interp);
247 PyMethod_Fini();
248 PyFrame_Fini();
249 PyCFunction_Fini();
250 PyTuple_Fini();
251 PyString_Fini();
252 PyInt_Fini();
253 PyFloat_Fini();
255 /* XXX Still allocated:
256 - various static ad-hoc pointers to interned strings
257 - int and float free list blocks
258 - whatever various modules and libraries allocate
261 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
263 call_ll_exitfuncs();
265 #ifdef Py_TRACE_REFS
266 _Py_ResetReferences();
267 #endif /* Py_TRACE_REFS */
270 /* Create and initialize a new interpreter and thread, and return the
271 new thread. This requires that Py_Initialize() has been called
272 first.
274 Unsuccessful initialization yields a NULL pointer. Note that *no*
275 exception information is available even in this case -- the
276 exception information is held in the thread, and there is no
277 thread.
279 Locking: as above.
283 PyThreadState *
284 Py_NewInterpreter(void)
286 PyInterpreterState *interp;
287 PyThreadState *tstate, *save_tstate;
288 PyObject *bimod, *sysmod;
290 if (!initialized)
291 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
293 interp = PyInterpreterState_New();
294 if (interp == NULL)
295 return NULL;
297 tstate = PyThreadState_New(interp);
298 if (tstate == NULL) {
299 PyInterpreterState_Delete(interp);
300 return NULL;
303 save_tstate = PyThreadState_Swap(tstate);
305 /* XXX The following is lax in error checking */
307 interp->modules = PyDict_New();
309 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
310 if (bimod != NULL) {
311 interp->builtins = PyModule_GetDict(bimod);
312 Py_INCREF(interp->builtins);
314 sysmod = _PyImport_FindExtension("sys", "sys");
315 if (bimod != NULL && sysmod != NULL) {
316 interp->sysdict = PyModule_GetDict(sysmod);
317 Py_INCREF(interp->sysdict);
318 PySys_SetPath(Py_GetPath());
319 PyDict_SetItemString(interp->sysdict, "modules",
320 interp->modules);
321 initmain();
322 if (!Py_NoSiteFlag)
323 initsite();
326 if (!PyErr_Occurred())
327 return tstate;
329 /* Oops, it didn't work. Undo it all. */
331 PyErr_Print();
332 PyThreadState_Clear(tstate);
333 PyThreadState_Swap(save_tstate);
334 PyThreadState_Delete(tstate);
335 PyInterpreterState_Delete(interp);
337 return NULL;
340 /* Delete an interpreter and its last thread. This requires that the
341 given thread state is current, that the thread has no remaining
342 frames, and that it is its interpreter's only remaining thread.
343 It is a fatal error to violate these constraints.
345 (Py_Finalize() doesn't have these constraints -- it zaps
346 everything, regardless.)
348 Locking: as above.
352 void
353 Py_EndInterpreter(PyThreadState *tstate)
355 PyInterpreterState *interp = tstate->interp;
357 if (tstate != PyThreadState_Get())
358 Py_FatalError("Py_EndInterpreter: thread is not current");
359 if (tstate->frame != NULL)
360 Py_FatalError("Py_EndInterpreter: thread still has a frame");
361 if (tstate != interp->tstate_head || tstate->next != NULL)
362 Py_FatalError("Py_EndInterpreter: not the last thread");
364 PyImport_Cleanup();
365 PyInterpreterState_Clear(interp);
366 PyThreadState_Swap(NULL);
367 PyInterpreterState_Delete(interp);
370 static char *progname = "python";
372 void
373 Py_SetProgramName(char *pn)
375 if (pn && *pn)
376 progname = pn;
379 char *
380 Py_GetProgramName(void)
382 return progname;
385 static char *default_home = NULL;
387 void
388 Py_SetPythonHome(char *home)
390 default_home = home;
393 char *
394 Py_GetPythonHome(void)
396 char *home = default_home;
397 if (home == NULL)
398 home = getenv("PYTHONHOME");
399 return home;
402 /* Create __main__ module */
404 static void
405 initmain(void)
407 PyObject *m, *d;
408 m = PyImport_AddModule("__main__");
409 if (m == NULL)
410 Py_FatalError("can't create __main__ module");
411 d = PyModule_GetDict(m);
412 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
413 PyObject *bimod = PyImport_ImportModule("__builtin__");
414 if (bimod == NULL ||
415 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
416 Py_FatalError("can't add __builtins__ to __main__");
417 Py_DECREF(bimod);
421 /* Import the site module (not into __main__ though) */
423 static void
424 initsite(void)
426 PyObject *m, *f;
427 m = PyImport_ImportModule("site");
428 if (m == NULL) {
429 f = PySys_GetObject("stderr");
430 if (Py_VerboseFlag) {
431 PyFile_WriteString(
432 "'import site' failed; traceback:\n", f);
433 PyErr_Print();
435 else {
436 PyFile_WriteString(
437 "'import site' failed; use -v for traceback\n", f);
438 PyErr_Clear();
441 else {
442 Py_DECREF(m);
446 /* Parse input from a file and execute it */
449 PyRun_AnyFile(FILE *fp, char *filename)
451 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
455 PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
457 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
461 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
463 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
467 PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit,
468 PyCompilerFlags *flags)
470 if (filename == NULL)
471 filename = "???";
472 if (Py_FdIsInteractive(fp, filename)) {
473 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
474 if (closeit)
475 fclose(fp);
476 return err;
478 else
479 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
483 PyRun_InteractiveLoop(FILE *fp, char *filename)
485 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
489 PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
491 PyObject *v;
492 int ret;
493 PyCompilerFlags local_flags;
495 if (flags == NULL) {
496 flags = &local_flags;
497 local_flags.cf_nested_scopes = 0;
499 v = PySys_GetObject("ps1");
500 if (v == NULL) {
501 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
502 Py_XDECREF(v);
504 v = PySys_GetObject("ps2");
505 if (v == NULL) {
506 PySys_SetObject("ps2", v = PyString_FromString("... "));
507 Py_XDECREF(v);
509 for (;;) {
510 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
511 #ifdef Py_REF_DEBUG
512 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
513 #endif
514 if (ret == E_EOF)
515 return 0;
517 if (ret == E_NOMEM)
518 return -1;
524 PyRun_InteractiveOne(FILE *fp, char *filename)
526 return PyRun_InteractiveOneFlags(fp, filename, NULL);
530 PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
532 PyObject *m, *d, *v, *w;
533 node *n;
534 perrdetail err;
535 char *ps1 = "", *ps2 = "";
536 v = PySys_GetObject("ps1");
537 if (v != NULL) {
538 v = PyObject_Str(v);
539 if (v == NULL)
540 PyErr_Clear();
541 else if (PyString_Check(v))
542 ps1 = PyString_AsString(v);
544 w = PySys_GetObject("ps2");
545 if (w != NULL) {
546 w = PyObject_Str(w);
547 if (w == NULL)
548 PyErr_Clear();
549 else if (PyString_Check(w))
550 ps2 = PyString_AsString(w);
552 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
553 Py_single_input, ps1, ps2, &err);
554 Py_XDECREF(v);
555 Py_XDECREF(w);
556 if (n == NULL) {
557 if (err.error == E_EOF) {
558 if (err.text)
559 PyMem_DEL(err.text);
560 return E_EOF;
562 err_input(&err);
563 PyErr_Print();
564 return err.error;
566 m = PyImport_AddModule("__main__");
567 if (m == NULL)
568 return -1;
569 d = PyModule_GetDict(m);
570 v = run_node(n, filename, d, d, flags);
571 if (v == NULL) {
572 PyErr_Print();
573 return -1;
575 Py_DECREF(v);
576 if (Py_FlushLine())
577 PyErr_Clear();
578 return 0;
582 PyRun_SimpleFile(FILE *fp, char *filename)
584 return PyRun_SimpleFileEx(fp, filename, 0);
587 /* Check whether a file maybe a pyc file: Look at the extension,
588 the file type, and, if we may close it, at the first few bytes. */
590 static int
591 maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
593 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
594 return 1;
596 #ifdef macintosh
597 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
598 if (PyMac_getfiletype(filename) == 'PYC '
599 || PyMac_getfiletype(filename) == 'APPL')
600 return 1;
601 #endif /* macintosh */
603 /* Only look into the file if we are allowed to close it, since
604 it then should also be seekable. */
605 if (closeit) {
606 /* Read only two bytes of the magic. If the file was opened in
607 text mode, the bytes 3 and 4 of the magic (\r\n) might not
608 be read as they are on disk. */
609 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
610 unsigned char buf[2];
611 /* Mess: In case of -x, the stream is NOT at its start now,
612 and ungetc() was used to push back the first newline,
613 which makes the current stream position formally undefined,
614 and a x-platform nightmare.
615 Unfortunately, we have no direct way to know whether -x
616 was specified. So we use a terrible hack: if the current
617 stream position is not 0, we assume -x was specified, and
618 give up. Bug 132850 on SourceForge spells out the
619 hopelessness of trying anything else (fseek and ftell
620 don't work predictably x-platform for text-mode files).
622 int ispyc = 0;
623 if (ftell(fp) == 0) {
624 if (fread(buf, 1, 2, fp) == 2 &&
625 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
626 ispyc = 1;
627 rewind(fp);
629 return ispyc;
631 return 0;
635 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
637 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
641 PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
642 PyCompilerFlags *flags)
644 PyObject *m, *d, *v;
645 char *ext;
647 m = PyImport_AddModule("__main__");
648 if (m == NULL)
649 return -1;
650 d = PyModule_GetDict(m);
651 ext = filename + strlen(filename) - 4;
652 if (maybe_pyc_file(fp, filename, ext, closeit)) {
653 /* Try to run a pyc file. First, re-open in binary */
654 if (closeit)
655 fclose(fp);
656 if( (fp = fopen(filename, "rb")) == NULL ) {
657 fprintf(stderr, "python: Can't reopen .pyc file\n");
658 return -1;
660 /* Turn on optimization if a .pyo file is given */
661 if (strcmp(ext, ".pyo") == 0)
662 Py_OptimizeFlag = 1;
663 v = run_pyc_file(fp, filename, d, d, flags);
664 } else {
665 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
666 closeit, flags);
668 if (v == NULL) {
669 PyErr_Print();
670 return -1;
672 Py_DECREF(v);
673 if (Py_FlushLine())
674 PyErr_Clear();
675 return 0;
679 PyRun_SimpleString(char *command)
681 PyObject *m, *d, *v;
682 m = PyImport_AddModule("__main__");
683 if (m == NULL)
684 return -1;
685 d = PyModule_GetDict(m);
686 v = PyRun_String(command, Py_file_input, d, d);
687 if (v == NULL) {
688 PyErr_Print();
689 return -1;
691 Py_DECREF(v);
692 if (Py_FlushLine())
693 PyErr_Clear();
694 return 0;
697 static int
698 parse_syntax_error(PyObject *err, PyObject **message, char **filename,
699 int *lineno, int *offset, char **text)
701 long hold;
702 PyObject *v;
704 /* old style errors */
705 if (PyTuple_Check(err))
706 return PyArg_Parse(err, "(O(ziiz))", message, filename,
707 lineno, offset, text);
709 /* new style errors. `err' is an instance */
711 if (! (v = PyObject_GetAttrString(err, "msg")))
712 goto finally;
713 *message = v;
715 if (!(v = PyObject_GetAttrString(err, "filename")))
716 goto finally;
717 if (v == Py_None)
718 *filename = NULL;
719 else if (! (*filename = PyString_AsString(v)))
720 goto finally;
722 Py_DECREF(v);
723 if (!(v = PyObject_GetAttrString(err, "lineno")))
724 goto finally;
725 hold = PyInt_AsLong(v);
726 Py_DECREF(v);
727 v = NULL;
728 if (hold < 0 && PyErr_Occurred())
729 goto finally;
730 *lineno = (int)hold;
732 if (!(v = PyObject_GetAttrString(err, "offset")))
733 goto finally;
734 if (v == Py_None) {
735 *offset = -1;
736 Py_DECREF(v);
737 v = NULL;
738 } else {
739 hold = PyInt_AsLong(v);
740 Py_DECREF(v);
741 v = NULL;
742 if (hold < 0 && PyErr_Occurred())
743 goto finally;
744 *offset = (int)hold;
747 if (!(v = PyObject_GetAttrString(err, "text")))
748 goto finally;
749 if (v == Py_None)
750 *text = NULL;
751 else if (! (*text = PyString_AsString(v)))
752 goto finally;
753 Py_DECREF(v);
754 return 1;
756 finally:
757 Py_XDECREF(v);
758 return 0;
761 void
762 PyErr_Print(void)
764 PyErr_PrintEx(1);
767 static void
768 print_error_text(PyObject *f, int offset, char *text)
770 char *nl;
771 if (offset >= 0) {
772 if (offset > 0 && offset == (int)strlen(text))
773 offset--;
774 for (;;) {
775 nl = strchr(text, '\n');
776 if (nl == NULL || nl-text >= offset)
777 break;
778 offset -= (nl+1-text);
779 text = nl+1;
781 while (*text == ' ' || *text == '\t') {
782 text++;
783 offset--;
786 PyFile_WriteString(" ", f);
787 PyFile_WriteString(text, f);
788 if (*text == '\0' || text[strlen(text)-1] != '\n')
789 PyFile_WriteString("\n", f);
790 if (offset == -1)
791 return;
792 PyFile_WriteString(" ", f);
793 offset--;
794 while (offset > 0) {
795 PyFile_WriteString(" ", f);
796 offset--;
798 PyFile_WriteString("^\n", f);
801 static void
802 handle_system_exit(void)
804 PyObject *exception, *value, *tb;
805 PyErr_Fetch(&exception, &value, &tb);
806 if (Py_FlushLine())
807 PyErr_Clear();
808 fflush(stdout);
809 if (value == NULL || value == Py_None)
810 Py_Exit(0);
811 if (PyInstance_Check(value)) {
812 /* The error code should be in the `code' attribute. */
813 PyObject *code = PyObject_GetAttrString(value, "code");
814 if (code) {
815 Py_DECREF(value);
816 value = code;
817 if (value == Py_None)
818 Py_Exit(0);
820 /* If we failed to dig out the 'code' attribute,
821 just let the else clause below print the error. */
823 if (PyInt_Check(value))
824 Py_Exit((int)PyInt_AsLong(value));
825 else {
826 PyObject_Print(value, stderr, Py_PRINT_RAW);
827 PySys_WriteStderr("\n");
828 Py_Exit(1);
832 void
833 PyErr_PrintEx(int set_sys_last_vars)
835 PyObject *exception, *v, *tb, *hook;
837 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
838 handle_system_exit();
840 PyErr_Fetch(&exception, &v, &tb);
841 PyErr_NormalizeException(&exception, &v, &tb);
842 if (exception == NULL)
843 return;
844 if (set_sys_last_vars) {
845 PySys_SetObject("last_type", exception);
846 PySys_SetObject("last_value", v);
847 PySys_SetObject("last_traceback", tb);
849 hook = PySys_GetObject("excepthook");
850 if (hook) {
851 PyObject *args = Py_BuildValue("(OOO)",
852 exception, v ? v : Py_None, tb ? tb : Py_None);
853 PyObject *result = PyEval_CallObject(hook, args);
854 if (result == NULL) {
855 PyObject *exception2, *v2, *tb2;
856 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
857 handle_system_exit();
859 PyErr_Fetch(&exception2, &v2, &tb2);
860 PyErr_NormalizeException(&exception2, &v2, &tb2);
861 if (Py_FlushLine())
862 PyErr_Clear();
863 fflush(stdout);
864 PySys_WriteStderr("Error in sys.excepthook:\n");
865 PyErr_Display(exception2, v2, tb2);
866 PySys_WriteStderr("\nOriginal exception was:\n");
867 PyErr_Display(exception, v, tb);
869 Py_XDECREF(result);
870 Py_XDECREF(args);
871 } else {
872 PySys_WriteStderr("sys.excepthook is missing\n");
873 PyErr_Display(exception, v, tb);
875 Py_XDECREF(exception);
876 Py_XDECREF(v);
877 Py_XDECREF(tb);
880 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
882 int err = 0;
883 PyObject *v = value;
884 PyObject *f = PySys_GetObject("stderr");
885 if (f == NULL)
886 fprintf(stderr, "lost sys.stderr\n");
887 else {
888 if (Py_FlushLine())
889 PyErr_Clear();
890 fflush(stdout);
891 if (tb && tb != Py_None)
892 err = PyTraceBack_Print(tb, f);
893 if (err == 0 &&
894 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
896 PyObject *message;
897 char *filename, *text;
898 int lineno, offset;
899 if (!parse_syntax_error(v, &message, &filename,
900 &lineno, &offset, &text))
901 PyErr_Clear();
902 else {
903 char buf[10];
904 PyFile_WriteString(" File \"", f);
905 if (filename == NULL)
906 PyFile_WriteString("<string>", f);
907 else
908 PyFile_WriteString(filename, f);
909 PyFile_WriteString("\", line ", f);
910 sprintf(buf, "%d", lineno);
911 PyFile_WriteString(buf, f);
912 PyFile_WriteString("\n", f);
913 if (text != NULL)
914 print_error_text(f, offset, text);
915 v = message;
916 /* Can't be bothered to check all those
917 PyFile_WriteString() calls */
918 if (PyErr_Occurred())
919 err = -1;
922 if (err) {
923 /* Don't do anything else */
925 else if (PyClass_Check(exception)) {
926 PyClassObject* exc = (PyClassObject*)exception;
927 PyObject* className = exc->cl_name;
928 PyObject* moduleName =
929 PyDict_GetItemString(exc->cl_dict, "__module__");
931 if (moduleName == NULL)
932 err = PyFile_WriteString("<unknown>", f);
933 else {
934 char* modstr = PyString_AsString(moduleName);
935 if (modstr && strcmp(modstr, "exceptions"))
937 err = PyFile_WriteString(modstr, f);
938 err += PyFile_WriteString(".", f);
941 if (err == 0) {
942 if (className == NULL)
943 err = PyFile_WriteString("<unknown>", f);
944 else
945 err = PyFile_WriteObject(className, f,
946 Py_PRINT_RAW);
949 else
950 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
951 if (err == 0) {
952 if (v != NULL && v != Py_None) {
953 PyObject *s = PyObject_Str(v);
954 /* only print colon if the str() of the
955 object is not the empty string
957 if (s == NULL)
958 err = -1;
959 else if (!PyString_Check(s) ||
960 PyString_GET_SIZE(s) != 0)
961 err = PyFile_WriteString(": ", f);
962 if (err == 0)
963 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
964 Py_XDECREF(s);
967 if (err == 0)
968 err = PyFile_WriteString("\n", f);
970 /* If an error happened here, don't show it.
971 XXX This is wrong, but too many callers rely on this behavior. */
972 if (err != 0)
973 PyErr_Clear();
976 PyObject *
977 PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
979 return run_err_node(PyParser_SimpleParseString(str, start),
980 "<string>", globals, locals, NULL);
983 PyObject *
984 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
985 PyObject *locals)
987 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
990 PyObject *
991 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
992 PyObject *locals, int closeit)
994 node *n = PyParser_SimpleParseFile(fp, filename, start);
995 if (closeit)
996 fclose(fp);
997 return run_err_node(n, filename, globals, locals, NULL);
1000 PyObject *
1001 PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
1002 PyCompilerFlags *flags)
1004 return run_err_node(PyParser_SimpleParseString(str, start),
1005 "<string>", globals, locals, flags);
1008 PyObject *
1009 PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
1010 PyObject *locals, PyCompilerFlags *flags)
1012 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1013 flags);
1016 PyObject *
1017 PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
1018 PyObject *locals, int closeit, PyCompilerFlags *flags)
1020 node *n = PyParser_SimpleParseFile(fp, filename, start);
1021 if (closeit)
1022 fclose(fp);
1023 return run_err_node(n, filename, globals, locals, flags);
1026 static PyObject *
1027 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1028 PyCompilerFlags *flags)
1030 if (n == NULL)
1031 return NULL;
1032 return run_node(n, filename, globals, locals, flags);
1035 static PyObject *
1036 run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
1037 PyCompilerFlags *flags)
1039 PyCodeObject *co;
1040 PyObject *v;
1041 co = PyNode_CompileFlags(n, filename, flags);
1042 PyNode_Free(n);
1043 if (co == NULL)
1044 return NULL;
1045 v = PyEval_EvalCode(co, globals, locals);
1046 Py_DECREF(co);
1047 return v;
1050 static PyObject *
1051 run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
1052 PyCompilerFlags *flags)
1054 PyCodeObject *co;
1055 PyObject *v;
1056 long magic;
1057 long PyImport_GetMagicNumber(void);
1059 magic = PyMarshal_ReadLongFromFile(fp);
1060 if (magic != PyImport_GetMagicNumber()) {
1061 PyErr_SetString(PyExc_RuntimeError,
1062 "Bad magic number in .pyc file");
1063 return NULL;
1065 (void) PyMarshal_ReadLongFromFile(fp);
1066 v = PyMarshal_ReadLastObjectFromFile(fp);
1067 fclose(fp);
1068 if (v == NULL || !PyCode_Check(v)) {
1069 Py_XDECREF(v);
1070 PyErr_SetString(PyExc_RuntimeError,
1071 "Bad code object in .pyc file");
1072 return NULL;
1074 co = (PyCodeObject *)v;
1075 v = PyEval_EvalCode(co, globals, locals);
1076 if (v && flags) {
1077 if (co->co_flags & CO_NESTED)
1078 flags->cf_nested_scopes = 1;
1079 fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
1080 flags->cf_nested_scopes);
1082 Py_DECREF(co);
1083 return v;
1086 PyObject *
1087 Py_CompileString(char *str, char *filename, int start)
1089 return Py_CompileStringFlags(str, filename, start, NULL);
1092 PyObject *
1093 Py_CompileStringFlags(char *str, char *filename, int start,
1094 PyCompilerFlags *flags)
1096 node *n;
1097 PyCodeObject *co;
1098 n = PyParser_SimpleParseString(str, start);
1099 if (n == NULL)
1100 return NULL;
1101 co = PyNode_CompileFlags(n, filename, flags);
1102 PyNode_Free(n);
1103 return (PyObject *)co;
1106 struct symtable *
1107 Py_SymtableString(char *str, char *filename, int start)
1109 node *n;
1110 struct symtable *st;
1111 n = PyParser_SimpleParseString(str, start);
1112 if (n == NULL)
1113 return NULL;
1114 st = PyNode_CompileSymtable(n, filename);
1115 PyNode_Free(n);
1116 return st;
1119 /* Simplified interface to parsefile -- return node or set exception */
1121 node *
1122 PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
1124 node *n;
1125 perrdetail err;
1126 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
1127 (char *)0, (char *)0, &err);
1128 if (n == NULL)
1129 err_input(&err);
1130 return n;
1133 /* Simplified interface to parsestring -- return node or set exception */
1135 node *
1136 PyParser_SimpleParseString(char *str, int start)
1138 node *n;
1139 perrdetail err;
1140 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
1141 if (n == NULL)
1142 err_input(&err);
1143 return n;
1146 /* Set the error appropriate to the given input error code (see errcode.h) */
1148 static void
1149 err_input(perrdetail *err)
1151 PyObject *v, *w, *errtype;
1152 char *msg = NULL;
1153 errtype = PyExc_SyntaxError;
1154 v = Py_BuildValue("(ziiz)", err->filename,
1155 err->lineno, err->offset, err->text);
1156 if (err->text != NULL) {
1157 PyMem_DEL(err->text);
1158 err->text = NULL;
1160 switch (err->error) {
1161 case E_SYNTAX:
1162 errtype = PyExc_IndentationError;
1163 if (err->expected == INDENT)
1164 msg = "expected an indented block";
1165 else if (err->token == INDENT)
1166 msg = "unexpected indent";
1167 else if (err->token == DEDENT)
1168 msg = "unexpected unindent";
1169 else {
1170 errtype = PyExc_SyntaxError;
1171 msg = "invalid syntax";
1173 break;
1174 case E_TOKEN:
1175 msg = "invalid token";
1176 break;
1177 case E_INTR:
1178 PyErr_SetNone(PyExc_KeyboardInterrupt);
1179 Py_XDECREF(v);
1180 return;
1181 case E_NOMEM:
1182 PyErr_NoMemory();
1183 Py_XDECREF(v);
1184 return;
1185 case E_EOF:
1186 msg = "unexpected EOF while parsing";
1187 break;
1188 case E_TABSPACE:
1189 errtype = PyExc_TabError;
1190 msg = "inconsistent use of tabs and spaces in indentation";
1191 break;
1192 case E_OVERFLOW:
1193 msg = "expression too long";
1194 break;
1195 case E_DEDENT:
1196 errtype = PyExc_IndentationError;
1197 msg = "unindent does not match any outer indentation level";
1198 break;
1199 case E_TOODEEP:
1200 errtype = PyExc_IndentationError;
1201 msg = "too many levels of indentation";
1202 break;
1203 default:
1204 fprintf(stderr, "error=%d\n", err->error);
1205 msg = "unknown parsing error";
1206 break;
1208 w = Py_BuildValue("(sO)", msg, v);
1209 Py_XDECREF(v);
1210 PyErr_SetObject(errtype, w);
1211 Py_XDECREF(w);
1214 /* Print fatal error message and abort */
1216 void
1217 Py_FatalError(char *msg)
1219 fprintf(stderr, "Fatal Python error: %s\n", msg);
1220 #ifdef macintosh
1221 for (;;);
1222 #endif
1223 #ifdef MS_WIN32
1224 OutputDebugString("Fatal Python error: ");
1225 OutputDebugString(msg);
1226 OutputDebugString("\n");
1227 #ifdef _DEBUG
1228 DebugBreak();
1229 #endif
1230 #endif /* MS_WIN32 */
1231 abort();
1234 /* Clean up and exit */
1236 #ifdef WITH_THREAD
1237 #include "pythread.h"
1238 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
1239 #endif
1241 #define NEXITFUNCS 32
1242 static void (*exitfuncs[NEXITFUNCS])(void);
1243 static int nexitfuncs = 0;
1245 int Py_AtExit(void (*func)(void))
1247 if (nexitfuncs >= NEXITFUNCS)
1248 return -1;
1249 exitfuncs[nexitfuncs++] = func;
1250 return 0;
1253 static void
1254 call_sys_exitfunc(void)
1256 PyObject *exitfunc = PySys_GetObject("exitfunc");
1258 if (exitfunc) {
1259 PyObject *res;
1260 Py_INCREF(exitfunc);
1261 PySys_SetObject("exitfunc", (PyObject *)NULL);
1262 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1263 if (res == NULL) {
1264 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1265 PySys_WriteStderr("Error in sys.exitfunc:\n");
1267 PyErr_Print();
1269 Py_DECREF(exitfunc);
1272 if (Py_FlushLine())
1273 PyErr_Clear();
1276 static void
1277 call_ll_exitfuncs(void)
1279 while (nexitfuncs > 0)
1280 (*exitfuncs[--nexitfuncs])();
1282 fflush(stdout);
1283 fflush(stderr);
1286 void
1287 Py_Exit(int sts)
1289 Py_Finalize();
1291 #ifdef macintosh
1292 PyMac_Exit(sts);
1293 #else
1294 exit(sts);
1295 #endif
1298 static void
1299 initsigs(void)
1301 #ifdef HAVE_SIGNAL_H
1302 #ifdef SIGPIPE
1303 signal(SIGPIPE, SIG_IGN);
1304 #endif
1305 #endif /* HAVE_SIGNAL_H */
1306 PyOS_InitInterrupts(); /* May imply initsignal() */
1309 #ifdef Py_TRACE_REFS
1310 /* Ask a yes/no question */
1313 _Py_AskYesNo(char *prompt)
1315 char buf[256];
1317 printf("%s [ny] ", prompt);
1318 if (fgets(buf, sizeof buf, stdin) == NULL)
1319 return 0;
1320 return buf[0] == 'y' || buf[0] == 'Y';
1322 #endif
1324 #ifdef MPW
1326 /* Check for file descriptor connected to interactive device.
1327 Pretend that stdin is always interactive, other files never. */
1330 isatty(int fd)
1332 return fd == fileno(stdin);
1335 #endif
1338 * The file descriptor fd is considered ``interactive'' if either
1339 * a) isatty(fd) is TRUE, or
1340 * b) the -i flag was given, and the filename associated with
1341 * the descriptor is NULL or "<stdin>" or "???".
1344 Py_FdIsInteractive(FILE *fp, char *filename)
1346 if (isatty((int)fileno(fp)))
1347 return 1;
1348 if (!Py_InteractiveFlag)
1349 return 0;
1350 return (filename == NULL) ||
1351 (strcmp(filename, "<stdin>") == 0) ||
1352 (strcmp(filename, "???") == 0);
1356 #if defined(USE_STACKCHECK)
1357 #if defined(WIN32) && defined(_MSC_VER)
1359 /* Stack checking for Microsoft C */
1361 #include <malloc.h>
1362 #include <excpt.h>
1365 * Return non-zero when we run out of memory on the stack; zero otherwise.
1368 PyOS_CheckStack(void)
1370 __try {
1371 /* _alloca throws a stack overflow exception if there's
1372 not enough space left on the stack */
1373 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1374 return 0;
1375 } __except (EXCEPTION_EXECUTE_HANDLER) {
1376 /* just ignore all errors */
1378 return 1;
1381 #endif /* WIN32 && _MSC_VER */
1383 /* Alternate implementations can be added here... */
1385 #endif /* USE_STACKCHECK */
1388 /* Wrappers around sigaction() or signal(). */
1390 PyOS_sighandler_t
1391 PyOS_getsig(int sig)
1393 #ifdef HAVE_SIGACTION
1394 struct sigaction context;
1395 sigaction(sig, NULL, &context);
1396 return context.sa_handler;
1397 #else
1398 PyOS_sighandler_t handler;
1399 handler = signal(sig, SIG_IGN);
1400 signal(sig, handler);
1401 return handler;
1402 #endif
1405 PyOS_sighandler_t
1406 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1408 #ifdef HAVE_SIGACTION
1409 struct sigaction context;
1410 PyOS_sighandler_t oldhandler;
1411 sigaction(sig, NULL, &context);
1412 oldhandler = context.sa_handler;
1413 context.sa_handler = handler;
1414 sigaction(sig, &context, NULL);
1415 return oldhandler;
1416 #else
1417 return signal(sig, handler);
1418 #endif