This commit was manufactured by cvs2svn to create tag 'r201'.
[python/dscho.git] / Python / pythonrun.c
blobe0af0da030fdf2aeea6c6caa28e7b69b46a41e1b
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 "eval.h"
13 #include "marshal.h"
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
19 #ifdef HAVE_SIGNAL_H
20 #include <signal.h>
21 #endif
23 #ifdef MS_WIN32
24 #undef BYTE
25 #include "windows.h"
26 #endif
28 #ifdef macintosh
29 #include "macglue.h"
30 #endif
31 extern char *Py_GetPath(void);
33 extern grammar _PyParser_Grammar; /* From graminit.c */
35 /* Forward */
36 static void initmain(void);
37 static void initsite(void);
38 static PyObject *run_err_node(node *n, char *filename,
39 PyObject *globals, PyObject *locals);
40 static PyObject *run_node(node *n, char *filename,
41 PyObject *globals, PyObject *locals);
42 static PyObject *run_pyc_file(FILE *fp, char *filename,
43 PyObject *globals, PyObject *locals);
44 static void err_input(perrdetail *);
45 static void initsigs(void);
46 static void call_sys_exitfunc(void);
47 static void call_ll_exitfuncs(void);
49 #ifdef Py_TRACE_REFS
50 int _Py_AskYesNo(char *prompt);
51 #endif
53 extern void _PyUnicode_Init(void);
54 extern void _PyUnicode_Fini(void);
55 extern void _PyCodecRegistry_Init(void);
56 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 _PyCompareState_Key = PyString_InternFromString("cmp_state");
129 bimod = _PyBuiltin_Init();
130 if (bimod == NULL)
131 Py_FatalError("Py_Initialize: can't initialize __builtin__");
132 interp->builtins = PyModule_GetDict(bimod);
133 Py_INCREF(interp->builtins);
135 sysmod = _PySys_Init();
136 if (sysmod == NULL)
137 Py_FatalError("Py_Initialize: can't initialize sys");
138 interp->sysdict = PyModule_GetDict(sysmod);
139 Py_INCREF(interp->sysdict);
140 _PyImport_FixupExtension("sys", "sys");
141 PySys_SetPath(Py_GetPath());
142 PyDict_SetItemString(interp->sysdict, "modules",
143 interp->modules);
145 _PyImport_Init();
147 /* initialize builtin exceptions */
148 init_exceptions();
150 /* phase 2 of builtins */
151 _PyImport_FixupExtension("__builtin__", "__builtin__");
153 initsigs(); /* Signal handling stuff, including initintr() */
155 initmain(); /* Module __main__ */
156 if (!Py_NoSiteFlag)
157 initsite(); /* Module site */
160 #ifdef COUNT_ALLOCS
161 extern void dump_counts(void);
162 #endif
164 /* Undo the effect of Py_Initialize().
166 Beware: if multiple interpreter and/or thread states exist, these
167 are not wiped out; only the current thread and interpreter state
168 are deleted. But since everything else is deleted, those other
169 interpreter and thread states should no longer be used.
171 (XXX We should do better, e.g. wipe out all interpreters and
172 threads.)
174 Locking: as above.
178 void
179 Py_Finalize(void)
181 PyInterpreterState *interp;
182 PyThreadState *tstate;
184 if (!initialized)
185 return;
187 /* The interpreter is still entirely intact at this point, and the
188 * exit funcs may be relying on that. In particular, if some thread
189 * or exit func is still waiting to do an import, the import machinery
190 * expects Py_IsInitialized() to return true. So don't say the
191 * interpreter is uninitialized until after the exit funcs have run.
192 * Note that Threading.py uses an exit func to do a join on all the
193 * threads created thru it, so this also protects pending imports in
194 * the threads created via Threading.
196 call_sys_exitfunc();
197 initialized = 0;
199 /* Get current thread state and interpreter pointer */
200 tstate = PyThreadState_Get();
201 interp = tstate->interp;
203 /* Disable signal handling */
204 PyOS_FiniInterrupts();
206 /* Cleanup Unicode implementation */
207 _PyUnicode_Fini();
209 /* Cleanup Codec registry */
210 _PyCodecRegistry_Fini();
212 /* Destroy all modules */
213 PyImport_Cleanup();
215 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
216 _PyImport_Fini();
218 /* Debugging stuff */
219 #ifdef COUNT_ALLOCS
220 dump_counts();
221 #endif
223 #ifdef Py_REF_DEBUG
224 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
225 #endif
227 #ifdef Py_TRACE_REFS
228 if (
229 #ifdef MS_WINDOWS /* Only ask on Windows if env var set */
230 getenv("PYTHONDUMPREFS") &&
231 #endif /* MS_WINDOWS */
232 _Py_AskYesNo("Print left references?")) {
233 _Py_PrintReferences(stderr);
235 #endif /* Py_TRACE_REFS */
237 /* Now we decref the exception classes. After this point nothing
238 can raise an exception. That's okay, because each Fini() method
239 below has been checked to make sure no exceptions are ever
240 raised.
242 fini_exceptions();
244 /* Delete current thread */
245 PyInterpreterState_Clear(interp);
246 PyThreadState_Swap(NULL);
247 PyInterpreterState_Delete(interp);
249 PyMethod_Fini();
250 PyFrame_Fini();
251 PyCFunction_Fini();
252 PyTuple_Fini();
253 PyString_Fini();
254 PyInt_Fini();
255 PyFloat_Fini();
257 /* XXX Still allocated:
258 - various static ad-hoc pointers to interned strings
259 - int and float free list blocks
260 - whatever various modules and libraries allocate
263 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
265 call_ll_exitfuncs();
267 #ifdef Py_TRACE_REFS
268 _Py_ResetReferences();
269 #endif /* Py_TRACE_REFS */
272 /* Create and initialize a new interpreter and thread, and return the
273 new thread. This requires that Py_Initialize() has been called
274 first.
276 Unsuccessful initialization yields a NULL pointer. Note that *no*
277 exception information is available even in this case -- the
278 exception information is held in the thread, and there is no
279 thread.
281 Locking: as above.
285 PyThreadState *
286 Py_NewInterpreter(void)
288 PyInterpreterState *interp;
289 PyThreadState *tstate, *save_tstate;
290 PyObject *bimod, *sysmod;
292 if (!initialized)
293 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
295 interp = PyInterpreterState_New();
296 if (interp == NULL)
297 return NULL;
299 tstate = PyThreadState_New(interp);
300 if (tstate == NULL) {
301 PyInterpreterState_Delete(interp);
302 return NULL;
305 save_tstate = PyThreadState_Swap(tstate);
307 /* XXX The following is lax in error checking */
309 interp->modules = PyDict_New();
311 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
312 if (bimod != NULL) {
313 interp->builtins = PyModule_GetDict(bimod);
314 Py_INCREF(interp->builtins);
316 sysmod = _PyImport_FindExtension("sys", "sys");
317 if (bimod != NULL && sysmod != NULL) {
318 interp->sysdict = PyModule_GetDict(sysmod);
319 Py_INCREF(interp->sysdict);
320 PySys_SetPath(Py_GetPath());
321 PyDict_SetItemString(interp->sysdict, "modules",
322 interp->modules);
323 initmain();
324 if (!Py_NoSiteFlag)
325 initsite();
328 if (!PyErr_Occurred())
329 return tstate;
331 /* Oops, it didn't work. Undo it all. */
333 PyErr_Print();
334 PyThreadState_Clear(tstate);
335 PyThreadState_Swap(save_tstate);
336 PyThreadState_Delete(tstate);
337 PyInterpreterState_Delete(interp);
339 return NULL;
342 /* Delete an interpreter and its last thread. This requires that the
343 given thread state is current, that the thread has no remaining
344 frames, and that it is its interpreter's only remaining thread.
345 It is a fatal error to violate these constraints.
347 (Py_Finalize() doesn't have these constraints -- it zaps
348 everything, regardless.)
350 Locking: as above.
354 void
355 Py_EndInterpreter(PyThreadState *tstate)
357 PyInterpreterState *interp = tstate->interp;
359 if (tstate != PyThreadState_Get())
360 Py_FatalError("Py_EndInterpreter: thread is not current");
361 if (tstate->frame != NULL)
362 Py_FatalError("Py_EndInterpreter: thread still has a frame");
363 if (tstate != interp->tstate_head || tstate->next != NULL)
364 Py_FatalError("Py_EndInterpreter: not the last thread");
366 PyImport_Cleanup();
367 PyInterpreterState_Clear(interp);
368 PyThreadState_Swap(NULL);
369 PyInterpreterState_Delete(interp);
372 static char *progname = "python";
374 void
375 Py_SetProgramName(char *pn)
377 if (pn && *pn)
378 progname = pn;
381 char *
382 Py_GetProgramName(void)
384 return progname;
387 static char *default_home = NULL;
389 void
390 Py_SetPythonHome(char *home)
392 default_home = home;
395 char *
396 Py_GetPythonHome(void)
398 char *home = default_home;
399 if (home == NULL)
400 home = getenv("PYTHONHOME");
401 return home;
404 /* Create __main__ module */
406 static void
407 initmain(void)
409 PyObject *m, *d;
410 m = PyImport_AddModule("__main__");
411 if (m == NULL)
412 Py_FatalError("can't create __main__ module");
413 d = PyModule_GetDict(m);
414 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
415 PyObject *bimod = PyImport_ImportModule("__builtin__");
416 if (bimod == NULL ||
417 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
418 Py_FatalError("can't add __builtins__ to __main__");
419 Py_DECREF(bimod);
423 /* Import the site module (not into __main__ though) */
425 static void
426 initsite(void)
428 PyObject *m, *f;
429 m = PyImport_ImportModule("site");
430 if (m == NULL) {
431 f = PySys_GetObject("stderr");
432 if (Py_VerboseFlag) {
433 PyFile_WriteString(
434 "'import site' failed; traceback:\n", f);
435 PyErr_Print();
437 else {
438 PyFile_WriteString(
439 "'import site' failed; use -v for traceback\n", f);
440 PyErr_Clear();
443 else {
444 Py_DECREF(m);
448 /* Parse input from a file and execute it */
451 PyRun_AnyFile(FILE *fp, char *filename)
453 return PyRun_AnyFileEx(fp, filename, 0);
457 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
459 if (filename == NULL)
460 filename = "???";
461 if (Py_FdIsInteractive(fp, filename)) {
462 int err = PyRun_InteractiveLoop(fp, filename);
463 if (closeit)
464 fclose(fp);
465 return err;
467 else
468 return PyRun_SimpleFileEx(fp, filename, closeit);
472 PyRun_InteractiveLoop(FILE *fp, char *filename)
474 PyObject *v;
475 int ret;
476 v = PySys_GetObject("ps1");
477 if (v == NULL) {
478 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
479 Py_XDECREF(v);
481 v = PySys_GetObject("ps2");
482 if (v == NULL) {
483 PySys_SetObject("ps2", v = PyString_FromString("... "));
484 Py_XDECREF(v);
486 for (;;) {
487 ret = PyRun_InteractiveOne(fp, filename);
488 #ifdef Py_REF_DEBUG
489 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
490 #endif
491 if (ret == E_EOF)
492 return 0;
494 if (ret == E_NOMEM)
495 return -1;
501 PyRun_InteractiveOne(FILE *fp, char *filename)
503 PyObject *m, *d, *v, *w;
504 node *n;
505 perrdetail err;
506 char *ps1 = "", *ps2 = "";
507 v = PySys_GetObject("ps1");
508 if (v != NULL) {
509 v = PyObject_Str(v);
510 if (v == NULL)
511 PyErr_Clear();
512 else if (PyString_Check(v))
513 ps1 = PyString_AsString(v);
515 w = PySys_GetObject("ps2");
516 if (w != NULL) {
517 w = PyObject_Str(w);
518 if (w == NULL)
519 PyErr_Clear();
520 else if (PyString_Check(w))
521 ps2 = PyString_AsString(w);
523 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
524 Py_single_input, ps1, ps2, &err);
525 Py_XDECREF(v);
526 Py_XDECREF(w);
527 if (n == NULL) {
528 if (err.error == E_EOF) {
529 if (err.text)
530 PyMem_DEL(err.text);
531 return E_EOF;
533 err_input(&err);
534 PyErr_Print();
535 return err.error;
537 m = PyImport_AddModule("__main__");
538 if (m == NULL)
539 return -1;
540 d = PyModule_GetDict(m);
541 v = run_node(n, filename, d, d);
542 if (v == NULL) {
543 PyErr_Print();
544 return -1;
546 Py_DECREF(v);
547 if (Py_FlushLine())
548 PyErr_Clear();
549 return 0;
553 PyRun_SimpleFile(FILE *fp, char *filename)
555 return PyRun_SimpleFileEx(fp, filename, 0);
559 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
561 PyObject *m, *d, *v;
562 char *ext;
564 m = PyImport_AddModule("__main__");
565 if (m == NULL)
566 return -1;
567 d = PyModule_GetDict(m);
568 ext = filename + strlen(filename) - 4;
569 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
570 #ifdef macintosh
571 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
572 || PyMac_getfiletype(filename) == 'PYC '
573 || PyMac_getfiletype(filename) == 'APPL'
574 #endif /* macintosh */
576 /* Try to run a pyc file. First, re-open in binary */
577 if (closeit)
578 fclose(fp);
579 if( (fp = fopen(filename, "rb")) == NULL ) {
580 fprintf(stderr, "python: Can't reopen .pyc file\n");
581 return -1;
583 /* Turn on optimization if a .pyo file is given */
584 if (strcmp(ext, ".pyo") == 0)
585 Py_OptimizeFlag = 1;
586 v = run_pyc_file(fp, filename, d, d);
587 } else {
588 v = PyRun_FileEx(fp, filename, Py_file_input, d, d, closeit);
590 if (v == NULL) {
591 PyErr_Print();
592 return -1;
594 Py_DECREF(v);
595 if (Py_FlushLine())
596 PyErr_Clear();
597 return 0;
601 PyRun_SimpleString(char *command)
603 PyObject *m, *d, *v;
604 m = PyImport_AddModule("__main__");
605 if (m == NULL)
606 return -1;
607 d = PyModule_GetDict(m);
608 v = PyRun_String(command, Py_file_input, d, d);
609 if (v == NULL) {
610 PyErr_Print();
611 return -1;
613 Py_DECREF(v);
614 if (Py_FlushLine())
615 PyErr_Clear();
616 return 0;
619 static int
620 parse_syntax_error(PyObject *err, PyObject **message, char **filename,
621 int *lineno, int *offset, char **text)
623 long hold;
624 PyObject *v;
626 /* old style errors */
627 if (PyTuple_Check(err))
628 return PyArg_Parse(err, "(O(ziiz))", message, filename,
629 lineno, offset, text);
631 /* new style errors. `err' is an instance */
633 if (! (v = PyObject_GetAttrString(err, "msg")))
634 goto finally;
635 *message = v;
637 if (!(v = PyObject_GetAttrString(err, "filename")))
638 goto finally;
639 if (v == Py_None)
640 *filename = NULL;
641 else if (! (*filename = PyString_AsString(v)))
642 goto finally;
644 Py_DECREF(v);
645 if (!(v = PyObject_GetAttrString(err, "lineno")))
646 goto finally;
647 hold = PyInt_AsLong(v);
648 Py_DECREF(v);
649 v = NULL;
650 if (hold < 0 && PyErr_Occurred())
651 goto finally;
652 *lineno = (int)hold;
654 if (!(v = PyObject_GetAttrString(err, "offset")))
655 goto finally;
656 hold = PyInt_AsLong(v);
657 Py_DECREF(v);
658 v = NULL;
659 if (hold < 0 && PyErr_Occurred())
660 goto finally;
661 *offset = (int)hold;
663 if (!(v = PyObject_GetAttrString(err, "text")))
664 goto finally;
665 if (v == Py_None)
666 *text = NULL;
667 else if (! (*text = PyString_AsString(v)))
668 goto finally;
669 Py_DECREF(v);
670 return 1;
672 finally:
673 Py_XDECREF(v);
674 return 0;
677 void
678 PyErr_Print(void)
680 PyErr_PrintEx(1);
683 void
684 PyErr_PrintEx(int set_sys_last_vars)
686 int err = 0;
687 PyObject *exception, *v, *tb, *f;
688 PyErr_Fetch(&exception, &v, &tb);
689 PyErr_NormalizeException(&exception, &v, &tb);
691 if (exception == NULL)
692 return;
694 if (PyErr_GivenExceptionMatches(exception, PyExc_SystemExit)) {
695 if (Py_FlushLine())
696 PyErr_Clear();
697 fflush(stdout);
698 if (v == NULL || v == Py_None)
699 Py_Exit(0);
700 if (PyInstance_Check(v)) {
701 /* we expect the error code to be store in the
702 `code' attribute
704 PyObject *code = PyObject_GetAttrString(v, "code");
705 if (code) {
706 Py_DECREF(v);
707 v = code;
708 if (v == Py_None)
709 Py_Exit(0);
711 /* if we failed to dig out the "code" attribute,
712 then just let the else clause below print the
713 error
716 if (PyInt_Check(v))
717 Py_Exit((int)PyInt_AsLong(v));
718 else {
719 /* OK to use real stderr here */
720 PyObject_Print(v, stderr, Py_PRINT_RAW);
721 fprintf(stderr, "\n");
722 Py_Exit(1);
725 if (set_sys_last_vars) {
726 PySys_SetObject("last_type", exception);
727 PySys_SetObject("last_value", v);
728 PySys_SetObject("last_traceback", tb);
730 f = PySys_GetObject("stderr");
731 if (f == NULL)
732 fprintf(stderr, "lost sys.stderr\n");
733 else {
734 if (Py_FlushLine())
735 PyErr_Clear();
736 fflush(stdout);
737 err = PyTraceBack_Print(tb, f);
738 if (err == 0 &&
739 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
741 PyObject *message;
742 char *filename, *text;
743 int lineno, offset;
744 if (!parse_syntax_error(v, &message, &filename,
745 &lineno, &offset, &text))
746 PyErr_Clear();
747 else {
748 char buf[10];
749 PyFile_WriteString(" File \"", f);
750 if (filename == NULL)
751 PyFile_WriteString("<string>", f);
752 else
753 PyFile_WriteString(filename, f);
754 PyFile_WriteString("\", line ", f);
755 sprintf(buf, "%d", lineno);
756 PyFile_WriteString(buf, f);
757 PyFile_WriteString("\n", f);
758 if (text != NULL) {
759 char *nl;
760 if (offset > 0 &&
761 offset == (int)strlen(text))
762 offset--;
763 for (;;) {
764 nl = strchr(text, '\n');
765 if (nl == NULL ||
766 nl-text >= offset)
767 break;
768 offset -= (nl+1-text);
769 text = nl+1;
771 while (*text == ' ' || *text == '\t') {
772 text++;
773 offset--;
775 PyFile_WriteString(" ", f);
776 PyFile_WriteString(text, f);
777 if (*text == '\0' ||
778 text[strlen(text)-1] != '\n')
779 PyFile_WriteString("\n", f);
780 PyFile_WriteString(" ", f);
781 offset--;
782 while (offset > 0) {
783 PyFile_WriteString(" ", f);
784 offset--;
786 PyFile_WriteString("^\n", f);
788 Py_INCREF(message);
789 Py_DECREF(v);
790 v = message;
791 /* Can't be bothered to check all those
792 PyFile_WriteString() calls */
793 if (PyErr_Occurred())
794 err = -1;
797 if (err) {
798 /* Don't do anything else */
800 else if (PyClass_Check(exception)) {
801 PyClassObject* exc = (PyClassObject*)exception;
802 PyObject* className = exc->cl_name;
803 PyObject* moduleName =
804 PyDict_GetItemString(exc->cl_dict, "__module__");
806 if (moduleName == NULL)
807 err = PyFile_WriteString("<unknown>", f);
808 else {
809 char* modstr = PyString_AsString(moduleName);
810 if (modstr && strcmp(modstr, "exceptions"))
812 err = PyFile_WriteString(modstr, f);
813 err += PyFile_WriteString(".", f);
816 if (err == 0) {
817 if (className == NULL)
818 err = PyFile_WriteString("<unknown>", f);
819 else
820 err = PyFile_WriteObject(className, f,
821 Py_PRINT_RAW);
824 else
825 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
826 if (err == 0) {
827 if (v != NULL && v != Py_None) {
828 PyObject *s = PyObject_Str(v);
829 /* only print colon if the str() of the
830 object is not the empty string
832 if (s == NULL)
833 err = -1;
834 else if (!PyString_Check(s) ||
835 PyString_GET_SIZE(s) != 0)
836 err = PyFile_WriteString(": ", f);
837 if (err == 0)
838 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
839 Py_XDECREF(s);
842 if (err == 0)
843 err = PyFile_WriteString("\n", f);
845 Py_XDECREF(exception);
846 Py_XDECREF(v);
847 Py_XDECREF(tb);
848 /* If an error happened here, don't show it.
849 XXX This is wrong, but too many callers rely on this behavior. */
850 if (err != 0)
851 PyErr_Clear();
854 PyObject *
855 PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
857 return run_err_node(PyParser_SimpleParseString(str, start),
858 "<string>", globals, locals);
861 PyObject *
862 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
863 PyObject *locals)
865 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
868 PyObject *
869 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
870 PyObject *locals, int closeit)
872 node *n = PyParser_SimpleParseFile(fp, filename, start);
873 if (closeit)
874 fclose(fp);
875 return run_err_node(n, filename, globals, locals);
878 static PyObject *
879 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals)
881 if (n == NULL)
882 return NULL;
883 return run_node(n, filename, globals, locals);
886 static PyObject *
887 run_node(node *n, char *filename, PyObject *globals, PyObject *locals)
889 PyCodeObject *co;
890 PyObject *v;
891 co = PyNode_Compile(n, filename);
892 PyNode_Free(n);
893 if (co == NULL)
894 return NULL;
895 v = PyEval_EvalCode(co, globals, locals);
896 Py_DECREF(co);
897 return v;
900 static PyObject *
901 run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals)
903 PyCodeObject *co;
904 PyObject *v;
905 long magic;
906 long PyImport_GetMagicNumber(void);
908 magic = PyMarshal_ReadLongFromFile(fp);
909 if (magic != PyImport_GetMagicNumber()) {
910 PyErr_SetString(PyExc_RuntimeError,
911 "Bad magic number in .pyc file");
912 return NULL;
914 (void) PyMarshal_ReadLongFromFile(fp);
915 v = PyMarshal_ReadObjectFromFile(fp);
916 fclose(fp);
917 if (v == NULL || !PyCode_Check(v)) {
918 Py_XDECREF(v);
919 PyErr_SetString(PyExc_RuntimeError,
920 "Bad code object in .pyc file");
921 return NULL;
923 co = (PyCodeObject *)v;
924 v = PyEval_EvalCode(co, globals, locals);
925 Py_DECREF(co);
926 return v;
929 PyObject *
930 Py_CompileString(char *str, char *filename, int start)
932 node *n;
933 PyCodeObject *co;
934 n = PyParser_SimpleParseString(str, start);
935 if (n == NULL)
936 return NULL;
937 co = PyNode_Compile(n, filename);
938 PyNode_Free(n);
939 return (PyObject *)co;
942 /* Simplified interface to parsefile -- return node or set exception */
944 node *
945 PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
947 node *n;
948 perrdetail err;
949 n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
950 (char *)0, (char *)0, &err);
951 if (n == NULL)
952 err_input(&err);
953 return n;
956 /* Simplified interface to parsestring -- return node or set exception */
958 node *
959 PyParser_SimpleParseString(char *str, int start)
961 node *n;
962 perrdetail err;
963 n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
964 if (n == NULL)
965 err_input(&err);
966 return n;
969 /* Set the error appropriate to the given input error code (see errcode.h) */
971 static void
972 err_input(perrdetail *err)
974 PyObject *v, *w, *errtype;
975 char *msg = NULL;
976 errtype = PyExc_SyntaxError;
977 v = Py_BuildValue("(ziiz)", err->filename,
978 err->lineno, err->offset, err->text);
979 if (err->text != NULL) {
980 PyMem_DEL(err->text);
981 err->text = NULL;
983 switch (err->error) {
984 case E_SYNTAX:
985 errtype = PyExc_IndentationError;
986 if (err->expected == INDENT)
987 msg = "expected an indented block";
988 else if (err->token == INDENT)
989 msg = "unexpected indent";
990 else if (err->token == DEDENT)
991 msg = "unexpected unindent";
992 else {
993 errtype = PyExc_SyntaxError;
994 msg = "invalid syntax";
996 break;
997 case E_TOKEN:
998 msg = "invalid token";
999 break;
1000 case E_INTR:
1001 PyErr_SetNone(PyExc_KeyboardInterrupt);
1002 Py_XDECREF(v);
1003 return;
1004 case E_NOMEM:
1005 PyErr_NoMemory();
1006 Py_XDECREF(v);
1007 return;
1008 case E_EOF:
1009 msg = "unexpected EOF while parsing";
1010 break;
1011 case E_TABSPACE:
1012 errtype = PyExc_TabError;
1013 msg = "inconsistent use of tabs and spaces in indentation";
1014 break;
1015 case E_OVERFLOW:
1016 msg = "expression too long";
1017 break;
1018 case E_DEDENT:
1019 errtype = PyExc_IndentationError;
1020 msg = "unindent does not match any outer indentation level";
1021 break;
1022 case E_TOODEEP:
1023 errtype = PyExc_IndentationError;
1024 msg = "too many levels of indentation";
1025 break;
1026 default:
1027 fprintf(stderr, "error=%d\n", err->error);
1028 msg = "unknown parsing error";
1029 break;
1031 w = Py_BuildValue("(sO)", msg, v);
1032 PyErr_SetObject(errtype, w);
1033 Py_XDECREF(w);
1035 if (v != NULL) {
1036 PyObject *exc, *tb;
1038 PyErr_Fetch(&errtype, &exc, &tb);
1039 PyErr_NormalizeException(&errtype, &exc, &tb);
1040 if (PyObject_SetAttrString(exc, "filename",
1041 PyTuple_GET_ITEM(v, 0)))
1042 PyErr_Clear();
1043 if (PyObject_SetAttrString(exc, "lineno",
1044 PyTuple_GET_ITEM(v, 1)))
1045 PyErr_Clear();
1046 if (PyObject_SetAttrString(exc, "offset",
1047 PyTuple_GET_ITEM(v, 2)))
1048 PyErr_Clear();
1049 Py_DECREF(v);
1050 PyErr_Restore(errtype, exc, tb);
1054 /* Print fatal error message and abort */
1056 void
1057 Py_FatalError(char *msg)
1059 fprintf(stderr, "Fatal Python error: %s\n", msg);
1060 #ifdef macintosh
1061 for (;;);
1062 #endif
1063 #ifdef MS_WIN32
1064 OutputDebugString("Fatal Python error: ");
1065 OutputDebugString(msg);
1066 OutputDebugString("\n");
1067 #ifdef _DEBUG
1068 DebugBreak();
1069 #endif
1070 #endif /* MS_WIN32 */
1071 abort();
1074 /* Clean up and exit */
1076 #ifdef WITH_THREAD
1077 #include "pythread.h"
1078 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
1079 #endif
1081 #define NEXITFUNCS 32
1082 static void (*exitfuncs[NEXITFUNCS])(void);
1083 static int nexitfuncs = 0;
1085 int Py_AtExit(void (*func)(void))
1087 if (nexitfuncs >= NEXITFUNCS)
1088 return -1;
1089 exitfuncs[nexitfuncs++] = func;
1090 return 0;
1093 static void
1094 call_sys_exitfunc(void)
1096 PyObject *exitfunc = PySys_GetObject("exitfunc");
1098 if (exitfunc) {
1099 PyObject *res, *f;
1100 Py_INCREF(exitfunc);
1101 PySys_SetObject("exitfunc", (PyObject *)NULL);
1102 f = PySys_GetObject("stderr");
1103 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1104 if (res == NULL) {
1105 if (f)
1106 PyFile_WriteString("Error in sys.exitfunc:\n", f);
1107 PyErr_Print();
1109 Py_DECREF(exitfunc);
1112 if (Py_FlushLine())
1113 PyErr_Clear();
1116 static void
1117 call_ll_exitfuncs(void)
1119 while (nexitfuncs > 0)
1120 (*exitfuncs[--nexitfuncs])();
1122 fflush(stdout);
1123 fflush(stderr);
1126 void
1127 Py_Exit(int sts)
1129 Py_Finalize();
1131 #ifdef macintosh
1132 PyMac_Exit(sts);
1133 #else
1134 exit(sts);
1135 #endif
1138 static void
1139 initsigs(void)
1141 #ifdef HAVE_SIGNAL_H
1142 #ifdef SIGPIPE
1143 signal(SIGPIPE, SIG_IGN);
1144 #endif
1145 #endif /* HAVE_SIGNAL_H */
1146 PyOS_InitInterrupts(); /* May imply initsignal() */
1149 #ifdef Py_TRACE_REFS
1150 /* Ask a yes/no question */
1153 _Py_AskYesNo(char *prompt)
1155 char buf[256];
1157 printf("%s [ny] ", prompt);
1158 if (fgets(buf, sizeof buf, stdin) == NULL)
1159 return 0;
1160 return buf[0] == 'y' || buf[0] == 'Y';
1162 #endif
1164 #ifdef MPW
1166 /* Check for file descriptor connected to interactive device.
1167 Pretend that stdin is always interactive, other files never. */
1170 isatty(int fd)
1172 return fd == fileno(stdin);
1175 #endif
1178 * The file descriptor fd is considered ``interactive'' if either
1179 * a) isatty(fd) is TRUE, or
1180 * b) the -i flag was given, and the filename associated with
1181 * the descriptor is NULL or "<stdin>" or "???".
1184 Py_FdIsInteractive(FILE *fp, char *filename)
1186 if (isatty((int)fileno(fp)))
1187 return 1;
1188 if (!Py_InteractiveFlag)
1189 return 0;
1190 return (filename == NULL) ||
1191 (strcmp(filename, "<stdin>") == 0) ||
1192 (strcmp(filename, "???") == 0);
1196 #if defined(USE_STACKCHECK)
1197 #if defined(WIN32) && defined(_MSC_VER)
1199 /* Stack checking for Microsoft C */
1201 #include <malloc.h>
1202 #include <excpt.h>
1205 * Return non-zero when we run out of memory on the stack; zero otherwise.
1208 PyOS_CheckStack(void)
1210 __try {
1211 /* _alloca throws a stack overflow exception if there's
1212 not enough space left on the stack */
1213 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
1214 return 0;
1215 } __except (EXCEPTION_EXECUTE_HANDLER) {
1216 /* just ignore all errors */
1218 return 1;
1221 #endif /* WIN32 && _MSC_VER */
1223 /* Alternate implementations can be added here... */
1225 #endif /* USE_STACKCHECK */
1228 /* Wrappers around sigaction() or signal(). */
1230 PyOS_sighandler_t
1231 PyOS_getsig(int sig)
1233 #ifdef HAVE_SIGACTION
1234 struct sigaction context;
1235 sigaction(sig, NULL, &context);
1236 return context.sa_handler;
1237 #else
1238 PyOS_sighandler_t handler;
1239 handler = signal(sig, SIG_IGN);
1240 signal(sig, handler);
1241 return handler;
1242 #endif
1245 PyOS_sighandler_t
1246 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1248 #ifdef HAVE_SIGACTION
1249 struct sigaction context;
1250 PyOS_sighandler_t oldhandler;
1251 sigaction(sig, NULL, &context);
1252 oldhandler = context.sa_handler;
1253 context.sa_handler = handler;
1254 sigaction(sig, &context, NULL);
1255 return oldhandler;
1256 #else
1257 return signal(sig, handler);
1258 #endif