Patch by Jeremy Katz (SF #1609407)
[python.git] / Python / pythonrun.c
blob634572e22016a02ae3ef66c85035352203a71169
2 /* Python interpreter top-level routines, including init/exit */
4 #include "Python.h"
6 #include "Python-ast.h"
7 #include "grammar.h"
8 #include "node.h"
9 #include "token.h"
10 #include "parsetok.h"
11 #include "errcode.h"
12 #include "code.h"
13 #include "compile.h"
14 #include "symtable.h"
15 #include "pyarena.h"
16 #include "ast.h"
17 #include "eval.h"
18 #include "marshal.h"
20 #ifdef HAVE_SIGNAL_H
21 #include <signal.h>
22 #endif
24 #ifdef HAVE_LANGINFO_H
25 #include <locale.h>
26 #include <langinfo.h>
27 #endif
29 #ifdef MS_WINDOWS
30 #undef BYTE
31 #include "windows.h"
32 #endif
34 #ifndef Py_REF_DEBUG
35 #define PRINT_TOTAL_REFS()
36 #else /* Py_REF_DEBUG */
37 #define PRINT_TOTAL_REFS() fprintf(stderr, \
38 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
39 _Py_GetRefTotal())
40 #endif
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 extern char *Py_GetPath(void);
48 extern grammar _PyParser_Grammar; /* From graminit.c */
50 /* Forward */
51 static void initmain(void);
52 static void initsite(void);
53 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
54 PyCompilerFlags *, PyArena *);
55 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
56 PyCompilerFlags *);
57 static void err_input(perrdetail *);
58 static void initsigs(void);
59 static void call_sys_exitfunc(void);
60 static void call_ll_exitfuncs(void);
61 extern void _PyUnicode_Init(void);
62 extern void _PyUnicode_Fini(void);
64 #ifdef WITH_THREAD
65 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
66 extern void _PyGILState_Fini(void);
67 #endif /* WITH_THREAD */
69 int Py_DebugFlag; /* Needed by parser.c */
70 int Py_VerboseFlag; /* Needed by import.c */
71 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
72 int Py_NoSiteFlag; /* Suppress 'import site' */
73 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
74 int Py_FrozenFlag; /* Needed by getpath.c */
75 int Py_UnicodeFlag = 0; /* Needed by compile.c */
76 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
77 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
78 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
79 true divisions (which they will be in 2.3). */
80 int _Py_QnewFlag = 0;
82 /* Reference to 'warnings' module, to avoid importing it
83 on the fly when the import lock may be held. See 683658/771097
85 static PyObject *warnings_module = NULL;
87 /* Returns a borrowed reference to the 'warnings' module, or NULL.
88 If the module is returned, it is guaranteed to have been obtained
89 without acquiring the import lock
91 PyObject *PyModule_GetWarningsModule(void)
93 PyObject *typ, *val, *tb;
94 PyObject *all_modules;
95 /* If we managed to get the module at init time, just use it */
96 if (warnings_module)
97 return warnings_module;
98 /* If it wasn't available at init time, it may be available
99 now in sys.modules (common scenario is frozen apps: import
100 at init time fails, but the frozen init code sets up sys.path
101 correctly, then does an implicit import of warnings for us
103 /* Save and restore any exceptions */
104 PyErr_Fetch(&typ, &val, &tb);
106 all_modules = PySys_GetObject("modules");
107 if (all_modules) {
108 warnings_module = PyDict_GetItemString(all_modules, "warnings");
109 /* We keep a ref in the global */
110 Py_XINCREF(warnings_module);
112 PyErr_Restore(typ, val, tb);
113 return warnings_module;
116 static int initialized = 0;
118 /* API to access the initialized flag -- useful for esoteric use */
121 Py_IsInitialized(void)
123 return initialized;
126 /* Global initializations. Can be undone by Py_Finalize(). Don't
127 call this twice without an intervening Py_Finalize() call. When
128 initializations fail, a fatal error is issued and the function does
129 not return. On return, the first thread and interpreter state have
130 been created.
132 Locking: you must hold the interpreter lock while calling this.
133 (If the lock has not yet been initialized, that's equivalent to
134 having the lock, but you cannot use multiple threads.)
138 static int
139 add_flag(int flag, const char *envs)
141 int env = atoi(envs);
142 if (flag < env)
143 flag = env;
144 if (flag < 1)
145 flag = 1;
146 return flag;
149 void
150 Py_InitializeEx(int install_sigs)
152 PyInterpreterState *interp;
153 PyThreadState *tstate;
154 PyObject *bimod, *sysmod;
155 char *p;
156 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
157 char *codeset;
158 char *saved_locale;
159 PyObject *sys_stream, *sys_isatty;
160 #endif
161 extern void _Py_ReadyTypes(void);
163 if (initialized)
164 return;
165 initialized = 1;
167 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
168 Py_DebugFlag = add_flag(Py_DebugFlag, p);
169 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
170 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
171 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
172 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
174 interp = PyInterpreterState_New();
175 if (interp == NULL)
176 Py_FatalError("Py_Initialize: can't make first interpreter");
178 tstate = PyThreadState_New(interp);
179 if (tstate == NULL)
180 Py_FatalError("Py_Initialize: can't make first thread");
181 (void) PyThreadState_Swap(tstate);
183 _Py_ReadyTypes();
185 if (!_PyFrame_Init())
186 Py_FatalError("Py_Initialize: can't init frames");
188 if (!_PyInt_Init())
189 Py_FatalError("Py_Initialize: can't init ints");
191 _PyFloat_Init();
193 interp->modules = PyDict_New();
194 if (interp->modules == NULL)
195 Py_FatalError("Py_Initialize: can't make modules dictionary");
197 #ifdef Py_USING_UNICODE
198 /* Init Unicode implementation; relies on the codec registry */
199 _PyUnicode_Init();
200 #endif
202 bimod = _PyBuiltin_Init();
203 if (bimod == NULL)
204 Py_FatalError("Py_Initialize: can't initialize __builtin__");
205 interp->builtins = PyModule_GetDict(bimod);
206 if (interp->builtins == NULL)
207 Py_FatalError("Py_Initialize: can't initialize builtins dict");
208 Py_INCREF(interp->builtins);
210 sysmod = _PySys_Init();
211 if (sysmod == NULL)
212 Py_FatalError("Py_Initialize: can't initialize sys");
213 interp->sysdict = PyModule_GetDict(sysmod);
214 if (interp->sysdict == NULL)
215 Py_FatalError("Py_Initialize: can't initialize sys dict");
216 Py_INCREF(interp->sysdict);
217 _PyImport_FixupExtension("sys", "sys");
218 PySys_SetPath(Py_GetPath());
219 PyDict_SetItemString(interp->sysdict, "modules",
220 interp->modules);
222 _PyImport_Init();
224 /* initialize builtin exceptions */
225 _PyExc_Init();
226 _PyImport_FixupExtension("exceptions", "exceptions");
228 /* phase 2 of builtins */
229 _PyImport_FixupExtension("__builtin__", "__builtin__");
231 _PyImportHooks_Init();
233 if (install_sigs)
234 initsigs(); /* Signal handling stuff, including initintr() */
236 initmain(); /* Module __main__ */
237 if (!Py_NoSiteFlag)
238 initsite(); /* Module site */
240 /* auto-thread-state API, if available */
241 #ifdef WITH_THREAD
242 _PyGILState_Init(interp, tstate);
243 #endif /* WITH_THREAD */
245 warnings_module = PyImport_ImportModule("warnings");
246 if (!warnings_module)
247 PyErr_Clear();
249 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
250 /* On Unix, set the file system encoding according to the
251 user's preference, if the CODESET names a well-known
252 Python codec, and Py_FileSystemDefaultEncoding isn't
253 initialized by other means. Also set the encoding of
254 stdin and stdout if these are terminals. */
256 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
257 setlocale(LC_CTYPE, "");
258 codeset = nl_langinfo(CODESET);
259 if (codeset && *codeset) {
260 PyObject *enc = PyCodec_Encoder(codeset);
261 if (enc) {
262 codeset = strdup(codeset);
263 Py_DECREF(enc);
264 } else {
265 codeset = NULL;
266 PyErr_Clear();
268 } else
269 codeset = NULL;
270 setlocale(LC_CTYPE, saved_locale);
271 free(saved_locale);
273 if (codeset) {
274 sys_stream = PySys_GetObject("stdin");
275 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
276 if (!sys_isatty)
277 PyErr_Clear();
278 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
279 if (!PyFile_SetEncoding(sys_stream, codeset))
280 Py_FatalError("Cannot set codeset of stdin");
282 Py_XDECREF(sys_isatty);
284 sys_stream = PySys_GetObject("stdout");
285 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
286 if (!sys_isatty)
287 PyErr_Clear();
288 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
289 if (!PyFile_SetEncoding(sys_stream, codeset))
290 Py_FatalError("Cannot set codeset of stdout");
292 Py_XDECREF(sys_isatty);
294 sys_stream = PySys_GetObject("stderr");
295 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
296 if (!sys_isatty)
297 PyErr_Clear();
298 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
299 if (!PyFile_SetEncoding(sys_stream, codeset))
300 Py_FatalError("Cannot set codeset of stderr");
302 Py_XDECREF(sys_isatty);
304 if (!Py_FileSystemDefaultEncoding)
305 Py_FileSystemDefaultEncoding = codeset;
306 else
307 free(codeset);
309 #endif
312 void
313 Py_Initialize(void)
315 Py_InitializeEx(1);
319 #ifdef COUNT_ALLOCS
320 extern void dump_counts(FILE*);
321 #endif
323 /* Undo the effect of Py_Initialize().
325 Beware: if multiple interpreter and/or thread states exist, these
326 are not wiped out; only the current thread and interpreter state
327 are deleted. But since everything else is deleted, those other
328 interpreter and thread states should no longer be used.
330 (XXX We should do better, e.g. wipe out all interpreters and
331 threads.)
333 Locking: as above.
337 void
338 Py_Finalize(void)
340 PyInterpreterState *interp;
341 PyThreadState *tstate;
343 if (!initialized)
344 return;
346 /* The interpreter is still entirely intact at this point, and the
347 * exit funcs may be relying on that. In particular, if some thread
348 * or exit func is still waiting to do an import, the import machinery
349 * expects Py_IsInitialized() to return true. So don't say the
350 * interpreter is uninitialized until after the exit funcs have run.
351 * Note that Threading.py uses an exit func to do a join on all the
352 * threads created thru it, so this also protects pending imports in
353 * the threads created via Threading.
355 call_sys_exitfunc();
356 initialized = 0;
358 /* Get current thread state and interpreter pointer */
359 tstate = PyThreadState_GET();
360 interp = tstate->interp;
362 /* Disable signal handling */
363 PyOS_FiniInterrupts();
365 /* drop module references we saved */
366 Py_XDECREF(warnings_module);
367 warnings_module = NULL;
369 /* Collect garbage. This may call finalizers; it's nice to call these
370 * before all modules are destroyed.
371 * XXX If a __del__ or weakref callback is triggered here, and tries to
372 * XXX import a module, bad things can happen, because Python no
373 * XXX longer believes it's initialized.
374 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
375 * XXX is easy to provoke that way. I've also seen, e.g.,
376 * XXX Exception exceptions.ImportError: 'No module named sha'
377 * XXX in <function callback at 0x008F5718> ignored
378 * XXX but I'm unclear on exactly how that one happens. In any case,
379 * XXX I haven't seen a real-life report of either of these.
381 PyGC_Collect();
382 #ifdef COUNT_ALLOCS
383 /* With COUNT_ALLOCS, it helps to run GC multiple times:
384 each collection might release some types from the type
385 list, so they become garbage. */
386 while (PyGC_Collect() > 0)
387 /* nothing */;
388 #endif
390 /* Destroy all modules */
391 PyImport_Cleanup();
393 /* Collect final garbage. This disposes of cycles created by
394 * new-style class definitions, for example.
395 * XXX This is disabled because it caused too many problems. If
396 * XXX a __del__ or weakref callback triggers here, Python code has
397 * XXX a hard time running, because even the sys module has been
398 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
399 * XXX One symptom is a sequence of information-free messages
400 * XXX coming from threads (if a __del__ or callback is invoked,
401 * XXX other threads can execute too, and any exception they encounter
402 * XXX triggers a comedy of errors as subsystem after subsystem
403 * XXX fails to find what it *expects* to find in sys to help report
404 * XXX the exception and consequent unexpected failures). I've also
405 * XXX seen segfaults then, after adding print statements to the
406 * XXX Python code getting called.
408 #if 0
409 PyGC_Collect();
410 #endif
412 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
413 _PyImport_Fini();
415 /* Debugging stuff */
416 #ifdef COUNT_ALLOCS
417 dump_counts(stdout);
418 #endif
420 PRINT_TOTAL_REFS();
422 #ifdef Py_TRACE_REFS
423 /* Display all objects still alive -- this can invoke arbitrary
424 * __repr__ overrides, so requires a mostly-intact interpreter.
425 * Alas, a lot of stuff may still be alive now that will be cleaned
426 * up later.
428 if (Py_GETENV("PYTHONDUMPREFS"))
429 _Py_PrintReferences(stderr);
430 #endif /* Py_TRACE_REFS */
432 /* Cleanup auto-thread-state */
433 #ifdef WITH_THREAD
434 _PyGILState_Fini();
435 #endif /* WITH_THREAD */
437 /* Clear interpreter state */
438 PyInterpreterState_Clear(interp);
440 /* Now we decref the exception classes. After this point nothing
441 can raise an exception. That's okay, because each Fini() method
442 below has been checked to make sure no exceptions are ever
443 raised.
446 _PyExc_Fini();
448 /* Delete current thread */
449 PyThreadState_Swap(NULL);
450 PyInterpreterState_Delete(interp);
452 /* Sundry finalizers */
453 PyMethod_Fini();
454 PyFrame_Fini();
455 PyCFunction_Fini();
456 PyTuple_Fini();
457 PyList_Fini();
458 PySet_Fini();
459 PyString_Fini();
460 PyInt_Fini();
461 PyFloat_Fini();
463 #ifdef Py_USING_UNICODE
464 /* Cleanup Unicode implementation */
465 _PyUnicode_Fini();
466 #endif
468 /* XXX Still allocated:
469 - various static ad-hoc pointers to interned strings
470 - int and float free list blocks
471 - whatever various modules and libraries allocate
474 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
476 #ifdef Py_TRACE_REFS
477 /* Display addresses (& refcnts) of all objects still alive.
478 * An address can be used to find the repr of the object, printed
479 * above by _Py_PrintReferences.
481 if (Py_GETENV("PYTHONDUMPREFS"))
482 _Py_PrintReferenceAddresses(stderr);
483 #endif /* Py_TRACE_REFS */
484 #ifdef PYMALLOC_DEBUG
485 if (Py_GETENV("PYTHONMALLOCSTATS"))
486 _PyObject_DebugMallocStats();
487 #endif
489 call_ll_exitfuncs();
492 /* Create and initialize a new interpreter and thread, and return the
493 new thread. This requires that Py_Initialize() has been called
494 first.
496 Unsuccessful initialization yields a NULL pointer. Note that *no*
497 exception information is available even in this case -- the
498 exception information is held in the thread, and there is no
499 thread.
501 Locking: as above.
505 PyThreadState *
506 Py_NewInterpreter(void)
508 PyInterpreterState *interp;
509 PyThreadState *tstate, *save_tstate;
510 PyObject *bimod, *sysmod;
512 if (!initialized)
513 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
515 interp = PyInterpreterState_New();
516 if (interp == NULL)
517 return NULL;
519 tstate = PyThreadState_New(interp);
520 if (tstate == NULL) {
521 PyInterpreterState_Delete(interp);
522 return NULL;
525 save_tstate = PyThreadState_Swap(tstate);
527 /* XXX The following is lax in error checking */
529 interp->modules = PyDict_New();
531 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
532 if (bimod != NULL) {
533 interp->builtins = PyModule_GetDict(bimod);
534 if (interp->builtins == NULL)
535 goto handle_error;
536 Py_INCREF(interp->builtins);
538 sysmod = _PyImport_FindExtension("sys", "sys");
539 if (bimod != NULL && sysmod != NULL) {
540 interp->sysdict = PyModule_GetDict(sysmod);
541 if (interp->sysdict == NULL)
542 goto handle_error;
543 Py_INCREF(interp->sysdict);
544 PySys_SetPath(Py_GetPath());
545 PyDict_SetItemString(interp->sysdict, "modules",
546 interp->modules);
547 _PyImportHooks_Init();
548 initmain();
549 if (!Py_NoSiteFlag)
550 initsite();
553 if (!PyErr_Occurred())
554 return tstate;
556 handle_error:
557 /* Oops, it didn't work. Undo it all. */
559 PyErr_Print();
560 PyThreadState_Clear(tstate);
561 PyThreadState_Swap(save_tstate);
562 PyThreadState_Delete(tstate);
563 PyInterpreterState_Delete(interp);
565 return NULL;
568 /* Delete an interpreter and its last thread. This requires that the
569 given thread state is current, that the thread has no remaining
570 frames, and that it is its interpreter's only remaining thread.
571 It is a fatal error to violate these constraints.
573 (Py_Finalize() doesn't have these constraints -- it zaps
574 everything, regardless.)
576 Locking: as above.
580 void
581 Py_EndInterpreter(PyThreadState *tstate)
583 PyInterpreterState *interp = tstate->interp;
585 if (tstate != PyThreadState_GET())
586 Py_FatalError("Py_EndInterpreter: thread is not current");
587 if (tstate->frame != NULL)
588 Py_FatalError("Py_EndInterpreter: thread still has a frame");
589 if (tstate != interp->tstate_head || tstate->next != NULL)
590 Py_FatalError("Py_EndInterpreter: not the last thread");
592 PyImport_Cleanup();
593 PyInterpreterState_Clear(interp);
594 PyThreadState_Swap(NULL);
595 PyInterpreterState_Delete(interp);
598 static char *progname = "python";
600 void
601 Py_SetProgramName(char *pn)
603 if (pn && *pn)
604 progname = pn;
607 char *
608 Py_GetProgramName(void)
610 return progname;
613 static char *default_home = NULL;
615 void
616 Py_SetPythonHome(char *home)
618 default_home = home;
621 char *
622 Py_GetPythonHome(void)
624 char *home = default_home;
625 if (home == NULL && !Py_IgnoreEnvironmentFlag)
626 home = Py_GETENV("PYTHONHOME");
627 return home;
630 /* Create __main__ module */
632 static void
633 initmain(void)
635 PyObject *m, *d;
636 m = PyImport_AddModule("__main__");
637 if (m == NULL)
638 Py_FatalError("can't create __main__ module");
639 d = PyModule_GetDict(m);
640 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
641 PyObject *bimod = PyImport_ImportModule("__builtin__");
642 if (bimod == NULL ||
643 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
644 Py_FatalError("can't add __builtins__ to __main__");
645 Py_DECREF(bimod);
649 /* Import the site module (not into __main__ though) */
651 static void
652 initsite(void)
654 PyObject *m, *f;
655 m = PyImport_ImportModule("site");
656 if (m == NULL) {
657 f = PySys_GetObject("stderr");
658 if (Py_VerboseFlag) {
659 PyFile_WriteString(
660 "'import site' failed; traceback:\n", f);
661 PyErr_Print();
663 else {
664 PyFile_WriteString(
665 "'import site' failed; use -v for traceback\n", f);
666 PyErr_Clear();
669 else {
670 Py_DECREF(m);
674 /* Parse input from a file and execute it */
677 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
678 PyCompilerFlags *flags)
680 if (filename == NULL)
681 filename = "???";
682 if (Py_FdIsInteractive(fp, filename)) {
683 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
684 if (closeit)
685 fclose(fp);
686 return err;
688 else
689 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
693 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
695 PyObject *v;
696 int ret;
697 PyCompilerFlags local_flags;
699 if (flags == NULL) {
700 flags = &local_flags;
701 local_flags.cf_flags = 0;
703 v = PySys_GetObject("ps1");
704 if (v == NULL) {
705 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
706 Py_XDECREF(v);
708 v = PySys_GetObject("ps2");
709 if (v == NULL) {
710 PySys_SetObject("ps2", v = PyString_FromString("... "));
711 Py_XDECREF(v);
713 for (;;) {
714 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
715 PRINT_TOTAL_REFS();
716 if (ret == E_EOF)
717 return 0;
719 if (ret == E_NOMEM)
720 return -1;
725 /* compute parser flags based on compiler flags */
726 #define PARSER_FLAGS(flags) \
727 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
728 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
730 #if 0
731 /* Keep an example of flags with future keyword support. */
732 #define PARSER_FLAGS(flags) \
733 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
734 PyPARSE_DONT_IMPLY_DEDENT : 0) \
735 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
736 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
737 #endif
740 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
742 PyObject *m, *d, *v, *w;
743 mod_ty mod;
744 PyArena *arena;
745 char *ps1 = "", *ps2 = "";
746 int errcode = 0;
748 v = PySys_GetObject("ps1");
749 if (v != NULL) {
750 v = PyObject_Str(v);
751 if (v == NULL)
752 PyErr_Clear();
753 else if (PyString_Check(v))
754 ps1 = PyString_AsString(v);
756 w = PySys_GetObject("ps2");
757 if (w != NULL) {
758 w = PyObject_Str(w);
759 if (w == NULL)
760 PyErr_Clear();
761 else if (PyString_Check(w))
762 ps2 = PyString_AsString(w);
764 arena = PyArena_New();
765 if (arena == NULL) {
766 Py_XDECREF(v);
767 Py_XDECREF(w);
768 return -1;
770 mod = PyParser_ASTFromFile(fp, filename,
771 Py_single_input, ps1, ps2,
772 flags, &errcode, arena);
773 Py_XDECREF(v);
774 Py_XDECREF(w);
775 if (mod == NULL) {
776 PyArena_Free(arena);
777 if (errcode == E_EOF) {
778 PyErr_Clear();
779 return E_EOF;
781 PyErr_Print();
782 return -1;
784 m = PyImport_AddModule("__main__");
785 if (m == NULL) {
786 PyArena_Free(arena);
787 return -1;
789 d = PyModule_GetDict(m);
790 v = run_mod(mod, filename, d, d, flags, arena);
791 PyArena_Free(arena);
792 if (v == NULL) {
793 PyErr_Print();
794 return -1;
796 Py_DECREF(v);
797 if (Py_FlushLine())
798 PyErr_Clear();
799 return 0;
802 /* Check whether a file maybe a pyc file: Look at the extension,
803 the file type, and, if we may close it, at the first few bytes. */
805 static int
806 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
808 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
809 return 1;
811 /* Only look into the file if we are allowed to close it, since
812 it then should also be seekable. */
813 if (closeit) {
814 /* Read only two bytes of the magic. If the file was opened in
815 text mode, the bytes 3 and 4 of the magic (\r\n) might not
816 be read as they are on disk. */
817 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
818 unsigned char buf[2];
819 /* Mess: In case of -x, the stream is NOT at its start now,
820 and ungetc() was used to push back the first newline,
821 which makes the current stream position formally undefined,
822 and a x-platform nightmare.
823 Unfortunately, we have no direct way to know whether -x
824 was specified. So we use a terrible hack: if the current
825 stream position is not 0, we assume -x was specified, and
826 give up. Bug 132850 on SourceForge spells out the
827 hopelessness of trying anything else (fseek and ftell
828 don't work predictably x-platform for text-mode files).
830 int ispyc = 0;
831 if (ftell(fp) == 0) {
832 if (fread(buf, 1, 2, fp) == 2 &&
833 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
834 ispyc = 1;
835 rewind(fp);
837 return ispyc;
839 return 0;
843 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
844 PyCompilerFlags *flags)
846 PyObject *m, *d, *v;
847 const char *ext;
849 m = PyImport_AddModule("__main__");
850 if (m == NULL)
851 return -1;
852 d = PyModule_GetDict(m);
853 if (PyDict_GetItemString(d, "__file__") == NULL) {
854 PyObject *f = PyString_FromString(filename);
855 if (f == NULL)
856 return -1;
857 if (PyDict_SetItemString(d, "__file__", f) < 0) {
858 Py_DECREF(f);
859 return -1;
861 Py_DECREF(f);
863 ext = filename + strlen(filename) - 4;
864 if (maybe_pyc_file(fp, filename, ext, closeit)) {
865 /* Try to run a pyc file. First, re-open in binary */
866 if (closeit)
867 fclose(fp);
868 if ((fp = fopen(filename, "rb")) == NULL) {
869 fprintf(stderr, "python: Can't reopen .pyc file\n");
870 return -1;
872 /* Turn on optimization if a .pyo file is given */
873 if (strcmp(ext, ".pyo") == 0)
874 Py_OptimizeFlag = 1;
875 v = run_pyc_file(fp, filename, d, d, flags);
876 } else {
877 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
878 closeit, flags);
880 if (v == NULL) {
881 PyErr_Print();
882 return -1;
884 Py_DECREF(v);
885 if (Py_FlushLine())
886 PyErr_Clear();
887 return 0;
891 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
893 PyObject *m, *d, *v;
894 m = PyImport_AddModule("__main__");
895 if (m == NULL)
896 return -1;
897 d = PyModule_GetDict(m);
898 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
899 if (v == NULL) {
900 PyErr_Print();
901 return -1;
903 Py_DECREF(v);
904 if (Py_FlushLine())
905 PyErr_Clear();
906 return 0;
909 static int
910 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
911 int *lineno, int *offset, const char **text)
913 long hold;
914 PyObject *v;
916 /* old style errors */
917 if (PyTuple_Check(err))
918 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
919 lineno, offset, text);
921 /* new style errors. `err' is an instance */
923 if (! (v = PyObject_GetAttrString(err, "msg")))
924 goto finally;
925 *message = v;
927 if (!(v = PyObject_GetAttrString(err, "filename")))
928 goto finally;
929 if (v == Py_None)
930 *filename = NULL;
931 else if (! (*filename = PyString_AsString(v)))
932 goto finally;
934 Py_DECREF(v);
935 if (!(v = PyObject_GetAttrString(err, "lineno")))
936 goto finally;
937 hold = PyInt_AsLong(v);
938 Py_DECREF(v);
939 v = NULL;
940 if (hold < 0 && PyErr_Occurred())
941 goto finally;
942 *lineno = (int)hold;
944 if (!(v = PyObject_GetAttrString(err, "offset")))
945 goto finally;
946 if (v == Py_None) {
947 *offset = -1;
948 Py_DECREF(v);
949 v = NULL;
950 } else {
951 hold = PyInt_AsLong(v);
952 Py_DECREF(v);
953 v = NULL;
954 if (hold < 0 && PyErr_Occurred())
955 goto finally;
956 *offset = (int)hold;
959 if (!(v = PyObject_GetAttrString(err, "text")))
960 goto finally;
961 if (v == Py_None)
962 *text = NULL;
963 else if (! (*text = PyString_AsString(v)))
964 goto finally;
965 Py_DECREF(v);
966 return 1;
968 finally:
969 Py_XDECREF(v);
970 return 0;
973 void
974 PyErr_Print(void)
976 PyErr_PrintEx(1);
979 static void
980 print_error_text(PyObject *f, int offset, const char *text)
982 char *nl;
983 if (offset >= 0) {
984 if (offset > 0 && offset == (int)strlen(text))
985 offset--;
986 for (;;) {
987 nl = strchr(text, '\n');
988 if (nl == NULL || nl-text >= offset)
989 break;
990 offset -= (int)(nl+1-text);
991 text = nl+1;
993 while (*text == ' ' || *text == '\t') {
994 text++;
995 offset--;
998 PyFile_WriteString(" ", f);
999 PyFile_WriteString(text, f);
1000 if (*text == '\0' || text[strlen(text)-1] != '\n')
1001 PyFile_WriteString("\n", f);
1002 if (offset == -1)
1003 return;
1004 PyFile_WriteString(" ", f);
1005 offset--;
1006 while (offset > 0) {
1007 PyFile_WriteString(" ", f);
1008 offset--;
1010 PyFile_WriteString("^\n", f);
1013 static void
1014 handle_system_exit(void)
1016 PyObject *exception, *value, *tb;
1017 int exitcode = 0;
1019 PyErr_Fetch(&exception, &value, &tb);
1020 if (Py_FlushLine())
1021 PyErr_Clear();
1022 fflush(stdout);
1023 if (value == NULL || value == Py_None)
1024 goto done;
1025 if (PyExceptionInstance_Check(value)) {
1026 /* The error code should be in the `code' attribute. */
1027 PyObject *code = PyObject_GetAttrString(value, "code");
1028 if (code) {
1029 Py_DECREF(value);
1030 value = code;
1031 if (value == Py_None)
1032 goto done;
1034 /* If we failed to dig out the 'code' attribute,
1035 just let the else clause below print the error. */
1037 if (PyInt_Check(value))
1038 exitcode = (int)PyInt_AsLong(value);
1039 else {
1040 PyObject_Print(value, stderr, Py_PRINT_RAW);
1041 PySys_WriteStderr("\n");
1042 exitcode = 1;
1044 done:
1045 /* Restore and clear the exception info, in order to properly decref
1046 * the exception, value, and traceback. If we just exit instead,
1047 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1048 * some finalizers from running.
1050 PyErr_Restore(exception, value, tb);
1051 PyErr_Clear();
1052 Py_Exit(exitcode);
1053 /* NOTREACHED */
1056 void
1057 PyErr_PrintEx(int set_sys_last_vars)
1059 PyObject *exception, *v, *tb, *hook;
1061 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1062 handle_system_exit();
1064 PyErr_Fetch(&exception, &v, &tb);
1065 if (exception == NULL)
1066 return;
1067 PyErr_NormalizeException(&exception, &v, &tb);
1068 if (exception == NULL)
1069 return;
1070 /* Now we know v != NULL too */
1071 if (set_sys_last_vars) {
1072 PySys_SetObject("last_type", exception);
1073 PySys_SetObject("last_value", v);
1074 PySys_SetObject("last_traceback", tb);
1076 hook = PySys_GetObject("excepthook");
1077 if (hook) {
1078 PyObject *args = PyTuple_Pack(3,
1079 exception, v, tb ? tb : Py_None);
1080 PyObject *result = PyEval_CallObject(hook, args);
1081 if (result == NULL) {
1082 PyObject *exception2, *v2, *tb2;
1083 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1084 handle_system_exit();
1086 PyErr_Fetch(&exception2, &v2, &tb2);
1087 PyErr_NormalizeException(&exception2, &v2, &tb2);
1088 /* It should not be possible for exception2 or v2
1089 to be NULL. However PyErr_Display() can't
1090 tolerate NULLs, so just be safe. */
1091 if (exception2 == NULL) {
1092 exception2 = Py_None;
1093 Py_INCREF(exception2);
1095 if (v2 == NULL) {
1096 v2 = Py_None;
1097 Py_INCREF(v2);
1099 if (Py_FlushLine())
1100 PyErr_Clear();
1101 fflush(stdout);
1102 PySys_WriteStderr("Error in sys.excepthook:\n");
1103 PyErr_Display(exception2, v2, tb2);
1104 PySys_WriteStderr("\nOriginal exception was:\n");
1105 PyErr_Display(exception, v, tb);
1106 Py_DECREF(exception2);
1107 Py_DECREF(v2);
1108 Py_XDECREF(tb2);
1110 Py_XDECREF(result);
1111 Py_XDECREF(args);
1112 } else {
1113 PySys_WriteStderr("sys.excepthook is missing\n");
1114 PyErr_Display(exception, v, tb);
1116 Py_XDECREF(exception);
1117 Py_XDECREF(v);
1118 Py_XDECREF(tb);
1121 void
1122 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1124 int err = 0;
1125 PyObject *f = PySys_GetObject("stderr");
1126 Py_INCREF(value);
1127 if (f == NULL)
1128 fprintf(stderr, "lost sys.stderr\n");
1129 else {
1130 if (Py_FlushLine())
1131 PyErr_Clear();
1132 fflush(stdout);
1133 if (tb && tb != Py_None)
1134 err = PyTraceBack_Print(tb, f);
1135 if (err == 0 &&
1136 PyObject_HasAttrString(value, "print_file_and_line"))
1138 PyObject *message;
1139 const char *filename, *text;
1140 int lineno, offset;
1141 if (!parse_syntax_error(value, &message, &filename,
1142 &lineno, &offset, &text))
1143 PyErr_Clear();
1144 else {
1145 char buf[10];
1146 PyFile_WriteString(" File \"", f);
1147 if (filename == NULL)
1148 PyFile_WriteString("<string>", f);
1149 else
1150 PyFile_WriteString(filename, f);
1151 PyFile_WriteString("\", line ", f);
1152 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1153 PyFile_WriteString(buf, f);
1154 PyFile_WriteString("\n", f);
1155 if (text != NULL)
1156 print_error_text(f, offset, text);
1157 Py_DECREF(value);
1158 value = message;
1159 /* Can't be bothered to check all those
1160 PyFile_WriteString() calls */
1161 if (PyErr_Occurred())
1162 err = -1;
1165 if (err) {
1166 /* Don't do anything else */
1168 else if (PyExceptionClass_Check(exception)) {
1169 PyObject* moduleName;
1170 char* className = PyExceptionClass_Name(exception);
1171 if (className != NULL) {
1172 char *dot = strrchr(className, '.');
1173 if (dot != NULL)
1174 className = dot+1;
1177 moduleName = PyObject_GetAttrString(exception, "__module__");
1178 if (moduleName == NULL)
1179 err = PyFile_WriteString("<unknown>", f);
1180 else {
1181 char* modstr = PyString_AsString(moduleName);
1182 if (modstr && strcmp(modstr, "exceptions"))
1184 err = PyFile_WriteString(modstr, f);
1185 err += PyFile_WriteString(".", f);
1187 Py_DECREF(moduleName);
1189 if (err == 0) {
1190 if (className == NULL)
1191 err = PyFile_WriteString("<unknown>", f);
1192 else
1193 err = PyFile_WriteString(className, f);
1196 else
1197 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1198 if (err == 0 && (value != Py_None)) {
1199 PyObject *s = PyObject_Str(value);
1200 /* only print colon if the str() of the
1201 object is not the empty string
1203 if (s == NULL)
1204 err = -1;
1205 else if (!PyString_Check(s) ||
1206 PyString_GET_SIZE(s) != 0)
1207 err = PyFile_WriteString(": ", f);
1208 if (err == 0)
1209 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1210 Py_XDECREF(s);
1212 if (err == 0)
1213 err = PyFile_WriteString("\n", f);
1215 Py_DECREF(value);
1216 /* If an error happened here, don't show it.
1217 XXX This is wrong, but too many callers rely on this behavior. */
1218 if (err != 0)
1219 PyErr_Clear();
1222 PyObject *
1223 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1224 PyObject *locals, PyCompilerFlags *flags)
1226 PyObject *ret = NULL;
1227 mod_ty mod;
1228 PyArena *arena = PyArena_New();
1229 if (arena == NULL)
1230 return NULL;
1232 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1233 if (mod != NULL)
1234 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1235 PyArena_Free(arena);
1236 return ret;
1239 PyObject *
1240 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1241 PyObject *locals, int closeit, PyCompilerFlags *flags)
1243 PyObject *ret;
1244 mod_ty mod;
1245 PyArena *arena = PyArena_New();
1246 if (arena == NULL)
1247 return NULL;
1249 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1250 flags, NULL, arena);
1251 if (mod == NULL) {
1252 PyArena_Free(arena);
1253 return NULL;
1255 if (closeit)
1256 fclose(fp);
1257 ret = run_mod(mod, filename, globals, locals, flags, arena);
1258 PyArena_Free(arena);
1259 return ret;
1262 static PyObject *
1263 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1264 PyCompilerFlags *flags, PyArena *arena)
1266 PyCodeObject *co;
1267 PyObject *v;
1268 co = PyAST_Compile(mod, filename, flags, arena);
1269 if (co == NULL)
1270 return NULL;
1271 v = PyEval_EvalCode(co, globals, locals);
1272 Py_DECREF(co);
1273 return v;
1276 static PyObject *
1277 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1278 PyObject *locals, PyCompilerFlags *flags)
1280 PyCodeObject *co;
1281 PyObject *v;
1282 long magic;
1283 long PyImport_GetMagicNumber(void);
1285 magic = PyMarshal_ReadLongFromFile(fp);
1286 if (magic != PyImport_GetMagicNumber()) {
1287 PyErr_SetString(PyExc_RuntimeError,
1288 "Bad magic number in .pyc file");
1289 return NULL;
1291 (void) PyMarshal_ReadLongFromFile(fp);
1292 v = PyMarshal_ReadLastObjectFromFile(fp);
1293 fclose(fp);
1294 if (v == NULL || !PyCode_Check(v)) {
1295 Py_XDECREF(v);
1296 PyErr_SetString(PyExc_RuntimeError,
1297 "Bad code object in .pyc file");
1298 return NULL;
1300 co = (PyCodeObject *)v;
1301 v = PyEval_EvalCode(co, globals, locals);
1302 if (v && flags)
1303 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1304 Py_DECREF(co);
1305 return v;
1308 PyObject *
1309 Py_CompileStringFlags(const char *str, const char *filename, int start,
1310 PyCompilerFlags *flags)
1312 PyCodeObject *co;
1313 mod_ty mod;
1314 PyArena *arena = PyArena_New();
1315 if (arena == NULL)
1316 return NULL;
1318 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1319 if (mod == NULL) {
1320 PyArena_Free(arena);
1321 return NULL;
1323 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1324 PyObject *result = PyAST_mod2obj(mod);
1325 PyArena_Free(arena);
1326 return result;
1328 co = PyAST_Compile(mod, filename, flags, arena);
1329 PyArena_Free(arena);
1330 return (PyObject *)co;
1333 struct symtable *
1334 Py_SymtableString(const char *str, const char *filename, int start)
1336 struct symtable *st;
1337 mod_ty mod;
1338 PyArena *arena = PyArena_New();
1339 if (arena == NULL)
1340 return NULL;
1342 mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
1343 if (mod == NULL) {
1344 PyArena_Free(arena);
1345 return NULL;
1347 st = PySymtable_Build(mod, filename, 0);
1348 PyArena_Free(arena);
1349 return st;
1352 /* Preferred access to parser is through AST. */
1353 mod_ty
1354 PyParser_ASTFromString(const char *s, const char *filename, int start,
1355 PyCompilerFlags *flags, PyArena *arena)
1357 mod_ty mod;
1358 perrdetail err;
1359 node *n = PyParser_ParseStringFlagsFilename(s, filename,
1360 &_PyParser_Grammar, start, &err,
1361 PARSER_FLAGS(flags));
1362 if (n) {
1363 mod = PyAST_FromNode(n, flags, filename, arena);
1364 PyNode_Free(n);
1365 return mod;
1367 else {
1368 err_input(&err);
1369 return NULL;
1373 mod_ty
1374 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1375 char *ps2, PyCompilerFlags *flags, int *errcode,
1376 PyArena *arena)
1378 mod_ty mod;
1379 perrdetail err;
1380 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1381 start, ps1, ps2, &err, PARSER_FLAGS(flags));
1382 if (n) {
1383 mod = PyAST_FromNode(n, flags, filename, arena);
1384 PyNode_Free(n);
1385 return mod;
1387 else {
1388 err_input(&err);
1389 if (errcode)
1390 *errcode = err.error;
1391 return NULL;
1395 /* Simplified interface to parsefile -- return node or set exception */
1397 node *
1398 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1400 perrdetail err;
1401 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1402 start, NULL, NULL, &err, flags);
1403 if (n == NULL)
1404 err_input(&err);
1406 return n;
1409 /* Simplified interface to parsestring -- return node or set exception */
1411 node *
1412 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1414 perrdetail err;
1415 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1416 start, &err, flags);
1417 if (n == NULL)
1418 err_input(&err);
1419 return n;
1422 node *
1423 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1424 int start, int flags)
1426 perrdetail err;
1427 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1428 &_PyParser_Grammar, start, &err, flags);
1429 if (n == NULL)
1430 err_input(&err);
1431 return n;
1434 node *
1435 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1437 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1440 /* May want to move a more generalized form of this to parsetok.c or
1441 even parser modules. */
1443 void
1444 PyParser_SetError(perrdetail *err)
1446 err_input(err);
1449 /* Set the error appropriate to the given input error code (see errcode.h) */
1451 static void
1452 err_input(perrdetail *err)
1454 PyObject *v, *w, *errtype;
1455 PyObject* u = NULL;
1456 char *msg = NULL;
1457 errtype = PyExc_SyntaxError;
1458 switch (err->error) {
1459 case E_SYNTAX:
1460 errtype = PyExc_IndentationError;
1461 if (err->expected == INDENT)
1462 msg = "expected an indented block";
1463 else if (err->token == INDENT)
1464 msg = "unexpected indent";
1465 else if (err->token == DEDENT)
1466 msg = "unexpected unindent";
1467 else {
1468 errtype = PyExc_SyntaxError;
1469 msg = "invalid syntax";
1471 break;
1472 case E_TOKEN:
1473 msg = "invalid token";
1474 break;
1475 case E_EOFS:
1476 msg = "EOF while scanning triple-quoted string";
1477 break;
1478 case E_EOLS:
1479 msg = "EOL while scanning single-quoted string";
1480 break;
1481 case E_INTR:
1482 if (!PyErr_Occurred())
1483 PyErr_SetNone(PyExc_KeyboardInterrupt);
1484 return;
1485 case E_NOMEM:
1486 PyErr_NoMemory();
1487 return;
1488 case E_EOF:
1489 msg = "unexpected EOF while parsing";
1490 break;
1491 case E_TABSPACE:
1492 errtype = PyExc_TabError;
1493 msg = "inconsistent use of tabs and spaces in indentation";
1494 break;
1495 case E_OVERFLOW:
1496 msg = "expression too long";
1497 break;
1498 case E_DEDENT:
1499 errtype = PyExc_IndentationError;
1500 msg = "unindent does not match any outer indentation level";
1501 break;
1502 case E_TOODEEP:
1503 errtype = PyExc_IndentationError;
1504 msg = "too many levels of indentation";
1505 break;
1506 case E_DECODE: {
1507 PyObject *type, *value, *tb;
1508 PyErr_Fetch(&type, &value, &tb);
1509 if (value != NULL) {
1510 u = PyObject_Str(value);
1511 if (u != NULL) {
1512 msg = PyString_AsString(u);
1515 if (msg == NULL)
1516 msg = "unknown decode error";
1517 Py_XDECREF(type);
1518 Py_XDECREF(value);
1519 Py_XDECREF(tb);
1520 break;
1522 case E_LINECONT:
1523 msg = "unexpected character after line continuation character";
1524 break;
1525 default:
1526 fprintf(stderr, "error=%d\n", err->error);
1527 msg = "unknown parsing error";
1528 break;
1530 v = Py_BuildValue("(ziiz)", err->filename,
1531 err->lineno, err->offset, err->text);
1532 if (err->text != NULL) {
1533 PyObject_FREE(err->text);
1534 err->text = NULL;
1536 w = NULL;
1537 if (v != NULL)
1538 w = Py_BuildValue("(sO)", msg, v);
1539 Py_XDECREF(u);
1540 Py_XDECREF(v);
1541 PyErr_SetObject(errtype, w);
1542 Py_XDECREF(w);
1545 /* Print fatal error message and abort */
1547 void
1548 Py_FatalError(const char *msg)
1550 fprintf(stderr, "Fatal Python error: %s\n", msg);
1551 #ifdef MS_WINDOWS
1552 OutputDebugString("Fatal Python error: ");
1553 OutputDebugString(msg);
1554 OutputDebugString("\n");
1555 #ifdef _DEBUG
1556 DebugBreak();
1557 #endif
1558 #endif /* MS_WINDOWS */
1559 abort();
1562 /* Clean up and exit */
1564 #ifdef WITH_THREAD
1565 #include "pythread.h"
1566 #endif
1568 #define NEXITFUNCS 32
1569 static void (*exitfuncs[NEXITFUNCS])(void);
1570 static int nexitfuncs = 0;
1572 int Py_AtExit(void (*func)(void))
1574 if (nexitfuncs >= NEXITFUNCS)
1575 return -1;
1576 exitfuncs[nexitfuncs++] = func;
1577 return 0;
1580 static void
1581 call_sys_exitfunc(void)
1583 PyObject *exitfunc = PySys_GetObject("exitfunc");
1585 if (exitfunc) {
1586 PyObject *res;
1587 Py_INCREF(exitfunc);
1588 PySys_SetObject("exitfunc", (PyObject *)NULL);
1589 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1590 if (res == NULL) {
1591 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1592 PySys_WriteStderr("Error in sys.exitfunc:\n");
1594 PyErr_Print();
1596 Py_DECREF(exitfunc);
1599 if (Py_FlushLine())
1600 PyErr_Clear();
1603 static void
1604 call_ll_exitfuncs(void)
1606 while (nexitfuncs > 0)
1607 (*exitfuncs[--nexitfuncs])();
1609 fflush(stdout);
1610 fflush(stderr);
1613 void
1614 Py_Exit(int sts)
1616 Py_Finalize();
1618 exit(sts);
1621 static void
1622 initsigs(void)
1624 #ifdef SIGPIPE
1625 PyOS_setsig(SIGPIPE, SIG_IGN);
1626 #endif
1627 #ifdef SIGXFZ
1628 PyOS_setsig(SIGXFZ, SIG_IGN);
1629 #endif
1630 #ifdef SIGXFSZ
1631 PyOS_setsig(SIGXFSZ, SIG_IGN);
1632 #endif
1633 PyOS_InitInterrupts(); /* May imply initsignal() */
1638 * The file descriptor fd is considered ``interactive'' if either
1639 * a) isatty(fd) is TRUE, or
1640 * b) the -i flag was given, and the filename associated with
1641 * the descriptor is NULL or "<stdin>" or "???".
1644 Py_FdIsInteractive(FILE *fp, const char *filename)
1646 if (isatty((int)fileno(fp)))
1647 return 1;
1648 if (!Py_InteractiveFlag)
1649 return 0;
1650 return (filename == NULL) ||
1651 (strcmp(filename, "<stdin>") == 0) ||
1652 (strcmp(filename, "???") == 0);
1656 #if defined(USE_STACKCHECK)
1657 #if defined(WIN32) && defined(_MSC_VER)
1659 /* Stack checking for Microsoft C */
1661 #include <malloc.h>
1662 #include <excpt.h>
1665 * Return non-zero when we run out of memory on the stack; zero otherwise.
1668 PyOS_CheckStack(void)
1670 __try {
1671 /* alloca throws a stack overflow exception if there's
1672 not enough space left on the stack */
1673 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1674 return 0;
1675 } __except (EXCEPTION_EXECUTE_HANDLER) {
1676 /* just ignore all errors */
1678 return 1;
1681 #endif /* WIN32 && _MSC_VER */
1683 /* Alternate implementations can be added here... */
1685 #endif /* USE_STACKCHECK */
1688 /* Wrappers around sigaction() or signal(). */
1690 PyOS_sighandler_t
1691 PyOS_getsig(int sig)
1693 #ifdef HAVE_SIGACTION
1694 struct sigaction context;
1695 if (sigaction(sig, NULL, &context) == -1)
1696 return SIG_ERR;
1697 return context.sa_handler;
1698 #else
1699 PyOS_sighandler_t handler;
1700 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1701 #if defined(_MSC_VER) && _MSC_VER >= 1400
1702 switch (sig) {
1703 /* Only these signals are valid */
1704 case SIGINT:
1705 case SIGILL:
1706 case SIGFPE:
1707 case SIGSEGV:
1708 case SIGTERM:
1709 case SIGBREAK:
1710 case SIGABRT:
1711 break;
1712 /* Don't call signal() with other values or it will assert */
1713 default:
1714 return SIG_ERR;
1716 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1717 handler = signal(sig, SIG_IGN);
1718 if (handler != SIG_ERR)
1719 signal(sig, handler);
1720 return handler;
1721 #endif
1724 PyOS_sighandler_t
1725 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1727 #ifdef HAVE_SIGACTION
1728 struct sigaction context, ocontext;
1729 context.sa_handler = handler;
1730 sigemptyset(&context.sa_mask);
1731 context.sa_flags = 0;
1732 if (sigaction(sig, &context, &ocontext) == -1)
1733 return SIG_ERR;
1734 return ocontext.sa_handler;
1735 #else
1736 PyOS_sighandler_t oldhandler;
1737 oldhandler = signal(sig, handler);
1738 #ifdef HAVE_SIGINTERRUPT
1739 siginterrupt(sig, 1);
1740 #endif
1741 return oldhandler;
1742 #endif
1745 /* Deprecated C API functions still provided for binary compatiblity */
1747 #undef PyParser_SimpleParseFile
1748 PyAPI_FUNC(node *)
1749 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1751 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1754 #undef PyParser_SimpleParseString
1755 PyAPI_FUNC(node *)
1756 PyParser_SimpleParseString(const char *str, int start)
1758 return PyParser_SimpleParseStringFlags(str, start, 0);
1761 #undef PyRun_AnyFile
1762 PyAPI_FUNC(int)
1763 PyRun_AnyFile(FILE *fp, const char *name)
1765 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1768 #undef PyRun_AnyFileEx
1769 PyAPI_FUNC(int)
1770 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1772 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1775 #undef PyRun_AnyFileFlags
1776 PyAPI_FUNC(int)
1777 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1779 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1782 #undef PyRun_File
1783 PyAPI_FUNC(PyObject *)
1784 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1786 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1789 #undef PyRun_FileEx
1790 PyAPI_FUNC(PyObject *)
1791 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1793 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1796 #undef PyRun_FileFlags
1797 PyAPI_FUNC(PyObject *)
1798 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1799 PyCompilerFlags *flags)
1801 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1804 #undef PyRun_SimpleFile
1805 PyAPI_FUNC(int)
1806 PyRun_SimpleFile(FILE *f, const char *p)
1808 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1811 #undef PyRun_SimpleFileEx
1812 PyAPI_FUNC(int)
1813 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1815 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1819 #undef PyRun_String
1820 PyAPI_FUNC(PyObject *)
1821 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1823 return PyRun_StringFlags(str, s, g, l, NULL);
1826 #undef PyRun_SimpleString
1827 PyAPI_FUNC(int)
1828 PyRun_SimpleString(const char *s)
1830 return PyRun_SimpleStringFlags(s, NULL);
1833 #undef Py_CompileString
1834 PyAPI_FUNC(PyObject *)
1835 Py_CompileString(const char *str, const char *p, int s)
1837 return Py_CompileStringFlags(str, p, s, NULL);
1840 #undef PyRun_InteractiveOne
1841 PyAPI_FUNC(int)
1842 PyRun_InteractiveOne(FILE *f, const char *p)
1844 return PyRun_InteractiveOneFlags(f, p, NULL);
1847 #undef PyRun_InteractiveLoop
1848 PyAPI_FUNC(int)
1849 PyRun_InteractiveLoop(FILE *f, const char *p)
1851 return PyRun_InteractiveLoopFlags(f, p, NULL);
1854 #ifdef __cplusplus
1856 #endif