2 /* Python interpreter top-level routines, including init/exit */
6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */
26 #include "malloc.h" /* for alloca */
29 #ifdef HAVE_LANGINFO_H
40 #define PRINT_TOTAL_REFS()
41 #else /* Py_REF_DEBUG */
42 #define PRINT_TOTAL_REFS() fprintf(stderr, \
43 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
51 extern char *Py_GetPath(void);
53 extern grammar _PyParser_Grammar; /* From graminit.c */
56 static void initmain(void);
57 static void initsite(void);
58 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
59 PyCompilerFlags *, PyArena *);
60 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
62 static void err_input(perrdetail *);
63 static void initsigs(void);
64 static void call_sys_exitfunc(void);
65 static void call_ll_exitfuncs(void);
66 extern void _PyUnicode_Init(void);
67 extern void _PyUnicode_Fini(void);
70 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
71 extern void _PyGILState_Fini(void);
72 #endif /* WITH_THREAD */
74 int Py_DebugFlag; /* Needed by parser.c */
75 int Py_VerboseFlag; /* Needed by import.c */
76 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
77 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
78 int Py_NoSiteFlag; /* Suppress 'import site' */
79 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
80 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
81 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
82 int Py_FrozenFlag; /* Needed by getpath.c */
83 int Py_UnicodeFlag = 0; /* Needed by compile.c */
84 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
85 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
86 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
87 true divisions (which they will be in 2.3). */
89 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
91 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
92 since _warnings is builtin. This API should not be used. */
94 PyModule_GetWarningsModule(void)
96 return PyImport_ImportModule("warnings");
99 static int initialized = 0;
101 /* API to access the initialized flag -- useful for esoteric use */
104 Py_IsInitialized(void)
109 /* Global initializations. Can be undone by Py_Finalize(). Don't
110 call this twice without an intervening Py_Finalize() call. When
111 initializations fail, a fatal error is issued and the function does
112 not return. On return, the first thread and interpreter state have
115 Locking: you must hold the interpreter lock while calling this.
116 (If the lock has not yet been initialized, that's equivalent to
117 having the lock, but you cannot use multiple threads.)
122 add_flag(int flag, const char *envs)
124 int env = atoi(envs);
133 Py_InitializeEx(int install_sigs)
135 PyInterpreterState *interp;
136 PyThreadState *tstate;
137 PyObject *bimod, *sysmod;
139 char *icodeset = NULL; /* On Windows, input codeset may theoretically
140 differ from output codeset. */
141 char *codeset = NULL;
143 int free_codeset = 0;
145 PyObject *sys_stream, *sys_isatty;
146 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
147 char *saved_locale, *loc_codeset;
153 extern void _Py_ReadyTypes(void);
159 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
160 Py_DebugFlag = add_flag(Py_DebugFlag, p);
161 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
162 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
163 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
164 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
165 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
166 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
168 interp = PyInterpreterState_New();
170 Py_FatalError("Py_Initialize: can't make first interpreter");
172 tstate = PyThreadState_New(interp);
174 Py_FatalError("Py_Initialize: can't make first thread");
175 (void) PyThreadState_Swap(tstate);
179 if (!_PyFrame_Init())
180 Py_FatalError("Py_Initialize: can't init frames");
183 Py_FatalError("Py_Initialize: can't init ints");
185 if (!PyByteArray_Init())
186 Py_FatalError("Py_Initialize: can't init bytearray");
190 interp->modules = PyDict_New();
191 if (interp->modules == NULL)
192 Py_FatalError("Py_Initialize: can't make modules dictionary");
193 interp->modules_reloading = PyDict_New();
194 if (interp->modules_reloading == NULL)
195 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
197 #ifdef Py_USING_UNICODE
198 /* Init Unicode implementation; relies on the codec registry */
202 bimod = _PyBuiltin_Init();
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();
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",
224 /* initialize builtin exceptions */
226 _PyImport_FixupExtension("exceptions", "exceptions");
228 /* phase 2 of builtins */
229 _PyImport_FixupExtension("__builtin__", "__builtin__");
231 _PyImportHooks_Init();
234 initsigs(); /* Signal handling stuff, including initintr() */
236 /* Initialize warnings. */
238 if (PySys_HasWarnOptions()) {
239 PyObject *warnings_module = PyImport_ImportModule("warnings");
240 if (!warnings_module)
242 Py_XDECREF(warnings_module);
245 initmain(); /* Module __main__ */
247 initsite(); /* Module site */
249 /* auto-thread-state API, if available */
251 _PyGILState_Init(interp, tstate);
252 #endif /* WITH_THREAD */
254 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
255 p = icodeset = codeset = strdup(p);
257 errors = strchr(p, ':');
265 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
266 /* On Unix, set the file system encoding according to the
267 user's preference, if the CODESET names a well-known
268 Python codec, and Py_FileSystemDefaultEncoding isn't
269 initialized by other means. Also set the encoding of
270 stdin and stdout if these are terminals, unless overridden. */
272 if (!overridden || !Py_FileSystemDefaultEncoding) {
273 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
274 setlocale(LC_CTYPE, "");
275 loc_codeset = nl_langinfo(CODESET);
276 if (loc_codeset && *loc_codeset) {
277 PyObject *enc = PyCodec_Encoder(loc_codeset);
279 loc_codeset = strdup(loc_codeset);
287 setlocale(LC_CTYPE, saved_locale);
291 codeset = icodeset = loc_codeset;
295 /* Initialize Py_FileSystemDefaultEncoding from
296 locale even if PYTHONIOENCODING is set. */
297 if (!Py_FileSystemDefaultEncoding) {
298 Py_FileSystemDefaultEncoding = loc_codeset;
309 sprintf(ibuf, "cp%d", GetConsoleCP());
310 sprintf(buf, "cp%d", GetConsoleOutputCP());
315 sys_stream = PySys_GetObject("stdin");
316 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
320 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
321 PyFile_Check(sys_stream)) {
322 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
323 Py_FatalError("Cannot set codeset of stdin");
325 Py_XDECREF(sys_isatty);
327 sys_stream = PySys_GetObject("stdout");
328 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
332 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
333 PyFile_Check(sys_stream)) {
334 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
335 Py_FatalError("Cannot set codeset of stdout");
337 Py_XDECREF(sys_isatty);
339 sys_stream = PySys_GetObject("stderr");
340 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
344 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
345 PyFile_Check(sys_stream)) {
346 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
347 Py_FatalError("Cannot set codeset of stderr");
349 Py_XDECREF(sys_isatty);
364 extern void dump_counts(FILE*);
367 /* Undo the effect of Py_Initialize().
369 Beware: if multiple interpreter and/or thread states exist, these
370 are not wiped out; only the current thread and interpreter state
371 are deleted. But since everything else is deleted, those other
372 interpreter and thread states should no longer be used.
374 (XXX We should do better, e.g. wipe out all interpreters and
384 PyInterpreterState *interp;
385 PyThreadState *tstate;
390 /* The interpreter is still entirely intact at this point, and the
391 * exit funcs may be relying on that. In particular, if some thread
392 * or exit func is still waiting to do an import, the import machinery
393 * expects Py_IsInitialized() to return true. So don't say the
394 * interpreter is uninitialized until after the exit funcs have run.
395 * Note that Threading.py uses an exit func to do a join on all the
396 * threads created thru it, so this also protects pending imports in
397 * the threads created via Threading.
402 /* Get current thread state and interpreter pointer */
403 tstate = PyThreadState_GET();
404 interp = tstate->interp;
406 /* Disable signal handling */
407 PyOS_FiniInterrupts();
409 /* Clear type lookup cache */
412 /* Collect garbage. This may call finalizers; it's nice to call these
413 * before all modules are destroyed.
414 * XXX If a __del__ or weakref callback is triggered here, and tries to
415 * XXX import a module, bad things can happen, because Python no
416 * XXX longer believes it's initialized.
417 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
418 * XXX is easy to provoke that way. I've also seen, e.g.,
419 * XXX Exception exceptions.ImportError: 'No module named sha'
420 * XXX in <function callback at 0x008F5718> ignored
421 * XXX but I'm unclear on exactly how that one happens. In any case,
422 * XXX I haven't seen a real-life report of either of these.
426 /* With COUNT_ALLOCS, it helps to run GC multiple times:
427 each collection might release some types from the type
428 list, so they become garbage. */
429 while (PyGC_Collect() > 0)
433 /* Destroy all modules */
436 /* Collect final garbage. This disposes of cycles created by
437 * new-style class definitions, for example.
438 * XXX This is disabled because it caused too many problems. If
439 * XXX a __del__ or weakref callback triggers here, Python code has
440 * XXX a hard time running, because even the sys module has been
441 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
442 * XXX One symptom is a sequence of information-free messages
443 * XXX coming from threads (if a __del__ or callback is invoked,
444 * XXX other threads can execute too, and any exception they encounter
445 * XXX triggers a comedy of errors as subsystem after subsystem
446 * XXX fails to find what it *expects* to find in sys to help report
447 * XXX the exception and consequent unexpected failures). I've also
448 * XXX seen segfaults then, after adding print statements to the
449 * XXX Python code getting called.
455 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
458 /* Debugging stuff */
466 /* Display all objects still alive -- this can invoke arbitrary
467 * __repr__ overrides, so requires a mostly-intact interpreter.
468 * Alas, a lot of stuff may still be alive now that will be cleaned
471 if (Py_GETENV("PYTHONDUMPREFS"))
472 _Py_PrintReferences(stderr);
473 #endif /* Py_TRACE_REFS */
475 /* Clear interpreter state */
476 PyInterpreterState_Clear(interp);
478 /* Now we decref the exception classes. After this point nothing
479 can raise an exception. That's okay, because each Fini() method
480 below has been checked to make sure no exceptions are ever
486 /* Cleanup auto-thread-state */
489 #endif /* WITH_THREAD */
491 /* Delete current thread */
492 PyThreadState_Swap(NULL);
493 PyInterpreterState_Delete(interp);
495 /* Sundry finalizers */
508 #ifdef Py_USING_UNICODE
509 /* Cleanup Unicode implementation */
513 /* XXX Still allocated:
514 - various static ad-hoc pointers to interned strings
515 - int and float free list blocks
516 - whatever various modules and libraries allocate
519 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
522 /* Display addresses (& refcnts) of all objects still alive.
523 * An address can be used to find the repr of the object, printed
524 * above by _Py_PrintReferences.
526 if (Py_GETENV("PYTHONDUMPREFS"))
527 _Py_PrintReferenceAddresses(stderr);
528 #endif /* Py_TRACE_REFS */
529 #ifdef PYMALLOC_DEBUG
530 if (Py_GETENV("PYTHONMALLOCSTATS"))
531 _PyObject_DebugMallocStats();
537 /* Create and initialize a new interpreter and thread, and return the
538 new thread. This requires that Py_Initialize() has been called
541 Unsuccessful initialization yields a NULL pointer. Note that *no*
542 exception information is available even in this case -- the
543 exception information is held in the thread, and there is no
551 Py_NewInterpreter(void)
553 PyInterpreterState *interp;
554 PyThreadState *tstate, *save_tstate;
555 PyObject *bimod, *sysmod;
558 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
560 interp = PyInterpreterState_New();
564 tstate = PyThreadState_New(interp);
565 if (tstate == NULL) {
566 PyInterpreterState_Delete(interp);
570 save_tstate = PyThreadState_Swap(tstate);
572 /* XXX The following is lax in error checking */
574 interp->modules = PyDict_New();
575 interp->modules_reloading = PyDict_New();
577 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
579 interp->builtins = PyModule_GetDict(bimod);
580 if (interp->builtins == NULL)
582 Py_INCREF(interp->builtins);
584 sysmod = _PyImport_FindExtension("sys", "sys");
585 if (bimod != NULL && sysmod != NULL) {
586 interp->sysdict = PyModule_GetDict(sysmod);
587 if (interp->sysdict == NULL)
589 Py_INCREF(interp->sysdict);
590 PySys_SetPath(Py_GetPath());
591 PyDict_SetItemString(interp->sysdict, "modules",
593 _PyImportHooks_Init();
599 if (!PyErr_Occurred())
603 /* Oops, it didn't work. Undo it all. */
606 PyThreadState_Clear(tstate);
607 PyThreadState_Swap(save_tstate);
608 PyThreadState_Delete(tstate);
609 PyInterpreterState_Delete(interp);
614 /* Delete an interpreter and its last thread. This requires that the
615 given thread state is current, that the thread has no remaining
616 frames, and that it is its interpreter's only remaining thread.
617 It is a fatal error to violate these constraints.
619 (Py_Finalize() doesn't have these constraints -- it zaps
620 everything, regardless.)
627 Py_EndInterpreter(PyThreadState *tstate)
629 PyInterpreterState *interp = tstate->interp;
631 if (tstate != PyThreadState_GET())
632 Py_FatalError("Py_EndInterpreter: thread is not current");
633 if (tstate->frame != NULL)
634 Py_FatalError("Py_EndInterpreter: thread still has a frame");
635 if (tstate != interp->tstate_head || tstate->next != NULL)
636 Py_FatalError("Py_EndInterpreter: not the last thread");
639 PyInterpreterState_Clear(interp);
640 PyThreadState_Swap(NULL);
641 PyInterpreterState_Delete(interp);
644 static char *progname = "python";
647 Py_SetProgramName(char *pn)
654 Py_GetProgramName(void)
659 static char *default_home = NULL;
662 Py_SetPythonHome(char *home)
668 Py_GetPythonHome(void)
670 char *home = default_home;
671 if (home == NULL && !Py_IgnoreEnvironmentFlag)
672 home = Py_GETENV("PYTHONHOME");
676 /* Create __main__ module */
682 m = PyImport_AddModule("__main__");
684 Py_FatalError("can't create __main__ module");
685 d = PyModule_GetDict(m);
686 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
687 PyObject *bimod = PyImport_ImportModule("__builtin__");
689 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
690 Py_FatalError("can't add __builtins__ to __main__");
695 /* Import the site module (not into __main__ though) */
701 m = PyImport_ImportModule("site");
703 f = PySys_GetObject("stderr");
704 if (Py_VerboseFlag) {
706 "'import site' failed; traceback:\n", f);
711 "'import site' failed; use -v for traceback\n", f);
720 /* Parse input from a file and execute it */
723 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
724 PyCompilerFlags *flags)
726 if (filename == NULL)
728 if (Py_FdIsInteractive(fp, filename)) {
729 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
735 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
739 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
743 PyCompilerFlags local_flags;
746 flags = &local_flags;
747 local_flags.cf_flags = 0;
749 v = PySys_GetObject("ps1");
751 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
754 v = PySys_GetObject("ps2");
756 PySys_SetObject("ps2", v = PyString_FromString("... "));
760 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
772 /* compute parser flags based on compiler flags */
773 #define PARSER_FLAGS(flags) \
774 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
775 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
778 /* Keep an example of flags with future keyword support. */
779 #define PARSER_FLAGS(flags) \
780 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
781 PyPARSE_DONT_IMPLY_DEDENT : 0) \
782 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
783 PyPARSE_PRINT_IS_FUNCTION : 0) \
784 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
785 PyPARSE_UNICODE_LITERALS : 0) \
790 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
792 PyObject *m, *d, *v, *w;
795 char *ps1 = "", *ps2 = "";
798 v = PySys_GetObject("ps1");
803 else if (PyString_Check(v))
804 ps1 = PyString_AsString(v);
806 w = PySys_GetObject("ps2");
811 else if (PyString_Check(w))
812 ps2 = PyString_AsString(w);
814 arena = PyArena_New();
820 mod = PyParser_ASTFromFile(fp, filename,
821 Py_single_input, ps1, ps2,
822 flags, &errcode, arena);
827 if (errcode == E_EOF) {
834 m = PyImport_AddModule("__main__");
839 d = PyModule_GetDict(m);
840 v = run_mod(mod, filename, d, d, flags, arena);
852 /* Check whether a file maybe a pyc file: Look at the extension,
853 the file type, and, if we may close it, at the first few bytes. */
856 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
858 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
861 /* Only look into the file if we are allowed to close it, since
862 it then should also be seekable. */
864 /* Read only two bytes of the magic. If the file was opened in
865 text mode, the bytes 3 and 4 of the magic (\r\n) might not
866 be read as they are on disk. */
867 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
868 unsigned char buf[2];
869 /* Mess: In case of -x, the stream is NOT at its start now,
870 and ungetc() was used to push back the first newline,
871 which makes the current stream position formally undefined,
872 and a x-platform nightmare.
873 Unfortunately, we have no direct way to know whether -x
874 was specified. So we use a terrible hack: if the current
875 stream position is not 0, we assume -x was specified, and
876 give up. Bug 132850 on SourceForge spells out the
877 hopelessness of trying anything else (fseek and ftell
878 don't work predictably x-platform for text-mode files).
881 if (ftell(fp) == 0) {
882 if (fread(buf, 1, 2, fp) == 2 &&
883 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
893 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
894 PyCompilerFlags *flags)
898 int set_file_name = 0, ret, len;
900 m = PyImport_AddModule("__main__");
903 d = PyModule_GetDict(m);
904 if (PyDict_GetItemString(d, "__file__") == NULL) {
905 PyObject *f = PyString_FromString(filename);
908 if (PyDict_SetItemString(d, "__file__", f) < 0) {
915 len = strlen(filename);
916 ext = filename + len - (len > 4 ? 4 : 0);
917 if (maybe_pyc_file(fp, filename, ext, closeit)) {
918 /* Try to run a pyc file. First, re-open in binary */
921 if ((fp = fopen(filename, "rb")) == NULL) {
922 fprintf(stderr, "python: Can't reopen .pyc file\n");
926 /* Turn on optimization if a .pyo file is given */
927 if (strcmp(ext, ".pyo") == 0)
929 v = run_pyc_file(fp, filename, d, d, flags);
931 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
944 if (set_file_name && PyDict_DelItemString(d, "__file__"))
950 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
953 m = PyImport_AddModule("__main__");
956 d = PyModule_GetDict(m);
957 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
969 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
970 int *lineno, int *offset, const char **text)
975 /* old style errors */
976 if (PyTuple_Check(err))
977 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
978 lineno, offset, text);
980 /* new style errors. `err' is an instance */
982 if (! (v = PyObject_GetAttrString(err, "msg")))
986 if (!(v = PyObject_GetAttrString(err, "filename")))
990 else if (! (*filename = PyString_AsString(v)))
994 if (!(v = PyObject_GetAttrString(err, "lineno")))
996 hold = PyInt_AsLong(v);
999 if (hold < 0 && PyErr_Occurred())
1001 *lineno = (int)hold;
1003 if (!(v = PyObject_GetAttrString(err, "offset")))
1010 hold = PyInt_AsLong(v);
1013 if (hold < 0 && PyErr_Occurred())
1015 *offset = (int)hold;
1018 if (!(v = PyObject_GetAttrString(err, "text")))
1022 else if (! (*text = PyString_AsString(v)))
1039 print_error_text(PyObject *f, int offset, const char *text)
1043 if (offset > 0 && offset == (int)strlen(text))
1046 nl = strchr(text, '\n');
1047 if (nl == NULL || nl-text >= offset)
1049 offset -= (int)(nl+1-text);
1052 while (*text == ' ' || *text == '\t') {
1057 PyFile_WriteString(" ", f);
1058 PyFile_WriteString(text, f);
1059 if (*text == '\0' || text[strlen(text)-1] != '\n')
1060 PyFile_WriteString("\n", f);
1063 PyFile_WriteString(" ", f);
1065 while (offset > 0) {
1066 PyFile_WriteString(" ", f);
1069 PyFile_WriteString("^\n", f);
1073 handle_system_exit(void)
1075 PyObject *exception, *value, *tb;
1079 /* Don't exit if -i flag was given. This flag is set to 0
1080 * when entering interactive mode for inspecting. */
1083 PyErr_Fetch(&exception, &value, &tb);
1087 if (value == NULL || value == Py_None)
1089 if (PyExceptionInstance_Check(value)) {
1090 /* The error code should be in the `code' attribute. */
1091 PyObject *code = PyObject_GetAttrString(value, "code");
1095 if (value == Py_None)
1098 /* If we failed to dig out the 'code' attribute,
1099 just let the else clause below print the error. */
1101 if (PyInt_Check(value))
1102 exitcode = (int)PyInt_AsLong(value);
1104 PyObject_Print(value, stderr, Py_PRINT_RAW);
1105 PySys_WriteStderr("\n");
1109 /* Restore and clear the exception info, in order to properly decref
1110 * the exception, value, and traceback. If we just exit instead,
1111 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1112 * some finalizers from running.
1114 PyErr_Restore(exception, value, tb);
1121 PyErr_PrintEx(int set_sys_last_vars)
1123 PyObject *exception, *v, *tb, *hook;
1125 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1126 handle_system_exit();
1128 PyErr_Fetch(&exception, &v, &tb);
1129 if (exception == NULL)
1131 PyErr_NormalizeException(&exception, &v, &tb);
1132 if (exception == NULL)
1134 /* Now we know v != NULL too */
1135 if (set_sys_last_vars) {
1136 PySys_SetObject("last_type", exception);
1137 PySys_SetObject("last_value", v);
1138 PySys_SetObject("last_traceback", tb);
1140 hook = PySys_GetObject("excepthook");
1142 PyObject *args = PyTuple_Pack(3,
1143 exception, v, tb ? tb : Py_None);
1144 PyObject *result = PyEval_CallObject(hook, args);
1145 if (result == NULL) {
1146 PyObject *exception2, *v2, *tb2;
1147 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1148 handle_system_exit();
1150 PyErr_Fetch(&exception2, &v2, &tb2);
1151 PyErr_NormalizeException(&exception2, &v2, &tb2);
1152 /* It should not be possible for exception2 or v2
1153 to be NULL. However PyErr_Display() can't
1154 tolerate NULLs, so just be safe. */
1155 if (exception2 == NULL) {
1156 exception2 = Py_None;
1157 Py_INCREF(exception2);
1166 PySys_WriteStderr("Error in sys.excepthook:\n");
1167 PyErr_Display(exception2, v2, tb2);
1168 PySys_WriteStderr("\nOriginal exception was:\n");
1169 PyErr_Display(exception, v, tb);
1170 Py_DECREF(exception2);
1177 PySys_WriteStderr("sys.excepthook is missing\n");
1178 PyErr_Display(exception, v, tb);
1180 Py_XDECREF(exception);
1186 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1189 PyObject *f = PySys_GetObject("stderr");
1192 fprintf(stderr, "lost sys.stderr\n");
1197 if (tb && tb != Py_None)
1198 err = PyTraceBack_Print(tb, f);
1200 PyObject_HasAttrString(value, "print_file_and_line"))
1203 const char *filename, *text;
1205 if (!parse_syntax_error(value, &message, &filename,
1206 &lineno, &offset, &text))
1210 PyFile_WriteString(" File \"", f);
1211 if (filename == NULL)
1212 PyFile_WriteString("<string>", f);
1214 PyFile_WriteString(filename, f);
1215 PyFile_WriteString("\", line ", f);
1216 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1217 PyFile_WriteString(buf, f);
1218 PyFile_WriteString("\n", f);
1220 print_error_text(f, offset, text);
1223 /* Can't be bothered to check all those
1224 PyFile_WriteString() calls */
1225 if (PyErr_Occurred())
1230 /* Don't do anything else */
1232 else if (PyExceptionClass_Check(exception)) {
1233 PyObject* moduleName;
1234 char* className = PyExceptionClass_Name(exception);
1235 if (className != NULL) {
1236 char *dot = strrchr(className, '.');
1241 moduleName = PyObject_GetAttrString(exception, "__module__");
1242 if (moduleName == NULL)
1243 err = PyFile_WriteString("<unknown>", f);
1245 char* modstr = PyString_AsString(moduleName);
1246 if (modstr && strcmp(modstr, "exceptions"))
1248 err = PyFile_WriteString(modstr, f);
1249 err += PyFile_WriteString(".", f);
1251 Py_DECREF(moduleName);
1254 if (className == NULL)
1255 err = PyFile_WriteString("<unknown>", f);
1257 err = PyFile_WriteString(className, f);
1261 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1262 if (err == 0 && (value != Py_None)) {
1263 PyObject *s = PyObject_Str(value);
1264 /* only print colon if the str() of the
1265 object is not the empty string
1269 else if (!PyString_Check(s) ||
1270 PyString_GET_SIZE(s) != 0)
1271 err = PyFile_WriteString(": ", f);
1273 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1276 /* try to write a newline in any case */
1277 err += PyFile_WriteString("\n", f);
1280 /* If an error happened here, don't show it.
1281 XXX This is wrong, but too many callers rely on this behavior. */
1287 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1288 PyObject *locals, PyCompilerFlags *flags)
1290 PyObject *ret = NULL;
1292 PyArena *arena = PyArena_New();
1296 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1298 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1299 PyArena_Free(arena);
1304 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1305 PyObject *locals, int closeit, PyCompilerFlags *flags)
1309 PyArena *arena = PyArena_New();
1313 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1314 flags, NULL, arena);
1318 PyArena_Free(arena);
1321 ret = run_mod(mod, filename, globals, locals, flags, arena);
1322 PyArena_Free(arena);
1327 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1328 PyCompilerFlags *flags, PyArena *arena)
1332 co = PyAST_Compile(mod, filename, flags, arena);
1335 v = PyEval_EvalCode(co, globals, locals);
1341 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1342 PyObject *locals, PyCompilerFlags *flags)
1347 long PyImport_GetMagicNumber(void);
1349 magic = PyMarshal_ReadLongFromFile(fp);
1350 if (magic != PyImport_GetMagicNumber()) {
1351 PyErr_SetString(PyExc_RuntimeError,
1352 "Bad magic number in .pyc file");
1355 (void) PyMarshal_ReadLongFromFile(fp);
1356 v = PyMarshal_ReadLastObjectFromFile(fp);
1358 if (v == NULL || !PyCode_Check(v)) {
1360 PyErr_SetString(PyExc_RuntimeError,
1361 "Bad code object in .pyc file");
1364 co = (PyCodeObject *)v;
1365 v = PyEval_EvalCode(co, globals, locals);
1367 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1373 Py_CompileStringFlags(const char *str, const char *filename, int start,
1374 PyCompilerFlags *flags)
1378 PyArena *arena = PyArena_New();
1382 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1384 PyArena_Free(arena);
1387 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1388 PyObject *result = PyAST_mod2obj(mod);
1389 PyArena_Free(arena);
1392 co = PyAST_Compile(mod, filename, flags, arena);
1393 PyArena_Free(arena);
1394 return (PyObject *)co;
1398 Py_SymtableString(const char *str, const char *filename, int start)
1400 struct symtable *st;
1402 PyCompilerFlags flags;
1403 PyArena *arena = PyArena_New();
1409 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1411 PyArena_Free(arena);
1414 st = PySymtable_Build(mod, filename, 0);
1415 PyArena_Free(arena);
1419 /* Preferred access to parser is through AST. */
1421 PyParser_ASTFromString(const char *s, const char *filename, int start,
1422 PyCompilerFlags *flags, PyArena *arena)
1425 PyCompilerFlags localflags;
1427 int iflags = PARSER_FLAGS(flags);
1429 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1430 &_PyParser_Grammar, start, &err,
1432 if (flags == NULL) {
1433 localflags.cf_flags = 0;
1434 flags = &localflags;
1437 flags->cf_flags |= iflags & PyCF_MASK;
1438 mod = PyAST_FromNode(n, flags, filename, arena);
1449 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1450 char *ps2, PyCompilerFlags *flags, int *errcode,
1454 PyCompilerFlags localflags;
1456 int iflags = PARSER_FLAGS(flags);
1458 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1459 start, ps1, ps2, &err, &iflags);
1460 if (flags == NULL) {
1461 localflags.cf_flags = 0;
1462 flags = &localflags;
1465 flags->cf_flags |= iflags & PyCF_MASK;
1466 mod = PyAST_FromNode(n, flags, filename, arena);
1473 *errcode = err.error;
1478 /* Simplified interface to parsefile -- return node or set exception */
1481 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1484 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1485 start, NULL, NULL, &err, flags);
1492 /* Simplified interface to parsestring -- return node or set exception */
1495 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1498 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1499 start, &err, flags);
1506 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1507 int start, int flags)
1510 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1511 &_PyParser_Grammar, start, &err, flags);
1518 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1520 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1523 /* May want to move a more generalized form of this to parsetok.c or
1524 even parser modules. */
1527 PyParser_SetError(perrdetail *err)
1532 /* Set the error appropriate to the given input error code (see errcode.h) */
1535 err_input(perrdetail *err)
1537 PyObject *v, *w, *errtype;
1540 errtype = PyExc_SyntaxError;
1541 switch (err->error) {
1543 errtype = PyExc_IndentationError;
1544 if (err->expected == INDENT)
1545 msg = "expected an indented block";
1546 else if (err->token == INDENT)
1547 msg = "unexpected indent";
1548 else if (err->token == DEDENT)
1549 msg = "unexpected unindent";
1551 errtype = PyExc_SyntaxError;
1552 msg = "invalid syntax";
1556 msg = "invalid token";
1559 msg = "EOF while scanning triple-quoted string literal";
1562 msg = "EOL while scanning string literal";
1565 if (!PyErr_Occurred())
1566 PyErr_SetNone(PyExc_KeyboardInterrupt);
1572 msg = "unexpected EOF while parsing";
1575 errtype = PyExc_TabError;
1576 msg = "inconsistent use of tabs and spaces in indentation";
1579 msg = "expression too long";
1582 errtype = PyExc_IndentationError;
1583 msg = "unindent does not match any outer indentation level";
1586 errtype = PyExc_IndentationError;
1587 msg = "too many levels of indentation";
1590 PyObject *type, *value, *tb;
1591 PyErr_Fetch(&type, &value, &tb);
1592 if (value != NULL) {
1593 u = PyObject_Str(value);
1595 msg = PyString_AsString(u);
1599 msg = "unknown decode error";
1606 msg = "unexpected character after line continuation character";
1609 fprintf(stderr, "error=%d\n", err->error);
1610 msg = "unknown parsing error";
1613 v = Py_BuildValue("(ziiz)", err->filename,
1614 err->lineno, err->offset, err->text);
1617 w = Py_BuildValue("(sO)", msg, v);
1620 PyErr_SetObject(errtype, w);
1623 if (err->text != NULL) {
1624 PyObject_FREE(err->text);
1629 /* Print fatal error message and abort */
1632 Py_FatalError(const char *msg)
1634 fprintf(stderr, "Fatal Python error: %s\n", msg);
1635 fflush(stderr); /* it helps in Windows debug build */
1639 size_t len = strlen(msg);
1643 /* Convert the message to wchar_t. This uses a simple one-to-one
1644 conversion, assuming that the this error message actually uses ASCII
1645 only. If this ceases to be true, we will have to convert. */
1646 buffer = alloca( (len+1) * (sizeof *buffer));
1647 for( i=0; i<=len; ++i)
1649 OutputDebugStringW(L"Fatal Python error: ");
1650 OutputDebugStringW(buffer);
1651 OutputDebugStringW(L"\n");
1656 #endif /* MS_WINDOWS */
1660 /* Clean up and exit */
1663 #include "pythread.h"
1666 #define NEXITFUNCS 32
1667 static void (*exitfuncs[NEXITFUNCS])(void);
1668 static int nexitfuncs = 0;
1670 int Py_AtExit(void (*func)(void))
1672 if (nexitfuncs >= NEXITFUNCS)
1674 exitfuncs[nexitfuncs++] = func;
1679 call_sys_exitfunc(void)
1681 PyObject *exitfunc = PySys_GetObject("exitfunc");
1685 Py_INCREF(exitfunc);
1686 PySys_SetObject("exitfunc", (PyObject *)NULL);
1687 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1689 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1690 PySys_WriteStderr("Error in sys.exitfunc:\n");
1694 Py_DECREF(exitfunc);
1702 call_ll_exitfuncs(void)
1704 while (nexitfuncs > 0)
1705 (*exitfuncs[--nexitfuncs])();
1723 PyOS_setsig(SIGPIPE, SIG_IGN);
1726 PyOS_setsig(SIGXFZ, SIG_IGN);
1729 PyOS_setsig(SIGXFSZ, SIG_IGN);
1731 PyOS_InitInterrupts(); /* May imply initsignal() */
1736 * The file descriptor fd is considered ``interactive'' if either
1737 * a) isatty(fd) is TRUE, or
1738 * b) the -i flag was given, and the filename associated with
1739 * the descriptor is NULL or "<stdin>" or "???".
1742 Py_FdIsInteractive(FILE *fp, const char *filename)
1744 if (isatty((int)fileno(fp)))
1746 if (!Py_InteractiveFlag)
1748 return (filename == NULL) ||
1749 (strcmp(filename, "<stdin>") == 0) ||
1750 (strcmp(filename, "???") == 0);
1754 #if defined(USE_STACKCHECK)
1755 #if defined(WIN32) && defined(_MSC_VER)
1757 /* Stack checking for Microsoft C */
1763 * Return non-zero when we run out of memory on the stack; zero otherwise.
1766 PyOS_CheckStack(void)
1769 /* alloca throws a stack overflow exception if there's
1770 not enough space left on the stack */
1771 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1773 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1774 EXCEPTION_EXECUTE_HANDLER :
1775 EXCEPTION_CONTINUE_SEARCH) {
1776 int errcode = _resetstkoflw();
1779 Py_FatalError("Could not reset the stack!");
1785 #endif /* WIN32 && _MSC_VER */
1787 /* Alternate implementations can be added here... */
1789 #endif /* USE_STACKCHECK */
1792 /* Wrappers around sigaction() or signal(). */
1795 PyOS_getsig(int sig)
1797 #ifdef HAVE_SIGACTION
1798 struct sigaction context;
1799 if (sigaction(sig, NULL, &context) == -1)
1801 return context.sa_handler;
1803 PyOS_sighandler_t handler;
1804 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1805 #if defined(_MSC_VER) && _MSC_VER >= 1400
1807 /* Only these signals are valid */
1816 /* Don't call signal() with other values or it will assert */
1820 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1821 handler = signal(sig, SIG_IGN);
1822 if (handler != SIG_ERR)
1823 signal(sig, handler);
1829 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1831 #ifdef HAVE_SIGACTION
1832 struct sigaction context, ocontext;
1833 context.sa_handler = handler;
1834 sigemptyset(&context.sa_mask);
1835 context.sa_flags = 0;
1836 if (sigaction(sig, &context, &ocontext) == -1)
1838 return ocontext.sa_handler;
1840 PyOS_sighandler_t oldhandler;
1841 oldhandler = signal(sig, handler);
1842 #ifdef HAVE_SIGINTERRUPT
1843 siginterrupt(sig, 1);
1849 /* Deprecated C API functions still provided for binary compatiblity */
1851 #undef PyParser_SimpleParseFile
1853 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1855 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1858 #undef PyParser_SimpleParseString
1860 PyParser_SimpleParseString(const char *str, int start)
1862 return PyParser_SimpleParseStringFlags(str, start, 0);
1865 #undef PyRun_AnyFile
1867 PyRun_AnyFile(FILE *fp, const char *name)
1869 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1872 #undef PyRun_AnyFileEx
1874 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1876 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1879 #undef PyRun_AnyFileFlags
1881 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1883 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1887 PyAPI_FUNC(PyObject *)
1888 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1890 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1894 PyAPI_FUNC(PyObject *)
1895 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1897 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1900 #undef PyRun_FileFlags
1901 PyAPI_FUNC(PyObject *)
1902 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1903 PyCompilerFlags *flags)
1905 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1908 #undef PyRun_SimpleFile
1910 PyRun_SimpleFile(FILE *f, const char *p)
1912 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1915 #undef PyRun_SimpleFileEx
1917 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1919 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1924 PyAPI_FUNC(PyObject *)
1925 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1927 return PyRun_StringFlags(str, s, g, l, NULL);
1930 #undef PyRun_SimpleString
1932 PyRun_SimpleString(const char *s)
1934 return PyRun_SimpleStringFlags(s, NULL);
1937 #undef Py_CompileString
1938 PyAPI_FUNC(PyObject *)
1939 Py_CompileString(const char *str, const char *p, int s)
1941 return Py_CompileStringFlags(str, p, s, NULL);
1944 #undef PyRun_InteractiveOne
1946 PyRun_InteractiveOne(FILE *f, const char *p)
1948 return PyRun_InteractiveOneFlags(f, p, NULL);
1951 #undef PyRun_InteractiveLoop
1953 PyRun_InteractiveLoop(FILE *f, const char *p)
1955 return PyRun_InteractiveLoopFlags(f, p, NULL);