2 /* Python interpreter top-level routines, including init/exit */
6 #include "Python-ast.h"
22 #ifdef HAVE_LANGINFO_H
33 # define PRINT_TOTAL_REFS()
34 #else /* Py_REF_DEBUG */
35 # if defined(MS_WIN64)
36 # define PRINT_TOTAL_REFS() fprintf(stderr, "[%Id refs]\n", _Py_RefTotal);
37 # else /* ! MS_WIN64 */
38 # define PRINT_TOTAL_REFS() fprintf(stderr, "[%ld refs]\n", \
39 Py_SAFE_DOWNCAST(_Py_RefTotal, Py_ssize_t, long));
40 # endif /* MS_WIN64 */
43 extern char *Py_GetPath(void);
45 extern grammar _PyParser_Grammar
; /* From graminit.c */
48 static void initmain(void);
49 static void initsite(void);
50 static PyObject
*run_mod(mod_ty
, const char *, PyObject
*, PyObject
*,
51 PyCompilerFlags
*, PyArena
*);
52 static PyObject
*run_pyc_file(FILE *, const char *, PyObject
*, PyObject
*,
54 static void err_input(perrdetail
*);
55 static void initsigs(void);
56 static void call_sys_exitfunc(void);
57 static void call_ll_exitfuncs(void);
58 extern void _PyUnicode_Init(void);
59 extern void _PyUnicode_Fini(void);
62 extern void _PyGILState_Init(PyInterpreterState
*, PyThreadState
*);
63 extern void _PyGILState_Fini(void);
64 #endif /* WITH_THREAD */
66 int Py_DebugFlag
; /* Needed by parser.c */
67 int Py_VerboseFlag
; /* Needed by import.c */
68 int Py_InteractiveFlag
; /* Needed by Py_FdIsInteractive() below */
69 int Py_NoSiteFlag
; /* Suppress 'import site' */
70 int Py_UseClassExceptionsFlag
= 1; /* Needed by bltinmodule.c: deprecated */
71 int Py_FrozenFlag
; /* Needed by getpath.c */
72 int Py_UnicodeFlag
= 0; /* Needed by compile.c */
73 int Py_IgnoreEnvironmentFlag
; /* e.g. PYTHONPATH, PYTHONHOME */
74 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
75 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
76 true divisions (which they will be in 2.3). */
79 /* Reference to 'warnings' module, to avoid importing it
80 on the fly when the import lock may be held. See 683658/771097
82 static PyObject
*warnings_module
= NULL
;
84 /* Returns a borrowed reference to the 'warnings' module, or NULL.
85 If the module is returned, it is guaranteed to have been obtained
86 without acquiring the import lock
88 PyObject
*PyModule_GetWarningsModule(void)
90 PyObject
*typ
, *val
, *tb
;
91 PyObject
*all_modules
;
92 /* If we managed to get the module at init time, just use it */
94 return warnings_module
;
95 /* If it wasn't available at init time, it may be available
96 now in sys.modules (common scenario is frozen apps: import
97 at init time fails, but the frozen init code sets up sys.path
98 correctly, then does an implicit import of warnings for us
100 /* Save and restore any exceptions */
101 PyErr_Fetch(&typ
, &val
, &tb
);
103 all_modules
= PySys_GetObject("modules");
105 warnings_module
= PyDict_GetItemString(all_modules
, "warnings");
106 /* We keep a ref in the global */
107 Py_XINCREF(warnings_module
);
109 PyErr_Restore(typ
, val
, tb
);
110 return warnings_module
;
113 static int initialized
= 0;
115 /* API to access the initialized flag -- useful for esoteric use */
118 Py_IsInitialized(void)
123 /* Global initializations. Can be undone by Py_Finalize(). Don't
124 call this twice without an intervening Py_Finalize() call. When
125 initializations fail, a fatal error is issued and the function does
126 not return. On return, the first thread and interpreter state have
129 Locking: you must hold the interpreter lock while calling this.
130 (If the lock has not yet been initialized, that's equivalent to
131 having the lock, but you cannot use multiple threads.)
136 add_flag(int flag
, const char *envs
)
138 int env
= atoi(envs
);
147 Py_InitializeEx(int install_sigs
)
149 PyInterpreterState
*interp
;
150 PyThreadState
*tstate
;
151 PyObject
*bimod
, *sysmod
;
153 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
156 PyObject
*sys_stream
, *sys_isatty
;
158 extern void _Py_ReadyTypes(void);
164 if ((p
= Py_GETENV("PYTHONDEBUG")) && *p
!= '\0')
165 Py_DebugFlag
= add_flag(Py_DebugFlag
, p
);
166 if ((p
= Py_GETENV("PYTHONVERBOSE")) && *p
!= '\0')
167 Py_VerboseFlag
= add_flag(Py_VerboseFlag
, p
);
168 if ((p
= Py_GETENV("PYTHONOPTIMIZE")) && *p
!= '\0')
169 Py_OptimizeFlag
= add_flag(Py_OptimizeFlag
, p
);
171 interp
= PyInterpreterState_New();
173 Py_FatalError("Py_Initialize: can't make first interpreter");
175 tstate
= PyThreadState_New(interp
);
177 Py_FatalError("Py_Initialize: can't make first thread");
178 (void) PyThreadState_Swap(tstate
);
182 if (!_PyFrame_Init())
183 Py_FatalError("Py_Initialize: can't init frames");
186 Py_FatalError("Py_Initialize: can't init ints");
190 interp
->modules
= PyDict_New();
191 if (interp
->modules
== NULL
)
192 Py_FatalError("Py_Initialize: can't make modules dictionary");
194 #ifdef Py_USING_UNICODE
195 /* Init Unicode implementation; relies on the codec registry */
199 bimod
= _PyBuiltin_Init();
201 Py_FatalError("Py_Initialize: can't initialize __builtin__");
202 interp
->builtins
= PyModule_GetDict(bimod
);
203 Py_INCREF(interp
->builtins
);
205 sysmod
= _PySys_Init();
207 Py_FatalError("Py_Initialize: can't initialize sys");
208 interp
->sysdict
= PyModule_GetDict(sysmod
);
209 Py_INCREF(interp
->sysdict
);
210 _PyImport_FixupExtension("sys", "sys");
211 PySys_SetPath(Py_GetPath());
212 PyDict_SetItemString(interp
->sysdict
, "modules",
217 /* initialize builtin exceptions */
219 _PyImport_FixupExtension("exceptions", "exceptions");
221 /* phase 2 of builtins */
222 _PyImport_FixupExtension("__builtin__", "__builtin__");
224 _PyImportHooks_Init();
227 initsigs(); /* Signal handling stuff, including initintr() */
229 initmain(); /* Module __main__ */
231 initsite(); /* Module site */
233 /* auto-thread-state API, if available */
235 _PyGILState_Init(interp
, tstate
);
236 #endif /* WITH_THREAD */
238 warnings_module
= PyImport_ImportModule("warnings");
239 if (!warnings_module
)
242 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
243 /* On Unix, set the file system encoding according to the
244 user's preference, if the CODESET names a well-known
245 Python codec, and Py_FileSystemDefaultEncoding isn't
246 initialized by other means. Also set the encoding of
247 stdin and stdout if these are terminals. */
249 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
250 setlocale(LC_CTYPE
, "");
251 codeset
= nl_langinfo(CODESET
);
252 if (codeset
&& *codeset
) {
253 PyObject
*enc
= PyCodec_Encoder(codeset
);
255 codeset
= strdup(codeset
);
263 setlocale(LC_CTYPE
, saved_locale
);
267 sys_stream
= PySys_GetObject("stdin");
268 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
271 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
272 if (!PyFile_SetEncoding(sys_stream
, codeset
))
273 Py_FatalError("Cannot set codeset of stdin");
275 Py_XDECREF(sys_isatty
);
277 sys_stream
= PySys_GetObject("stdout");
278 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
281 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
282 if (!PyFile_SetEncoding(sys_stream
, codeset
))
283 Py_FatalError("Cannot set codeset of stdout");
285 Py_XDECREF(sys_isatty
);
287 if (!Py_FileSystemDefaultEncoding
)
288 Py_FileSystemDefaultEncoding
= codeset
;
303 extern void dump_counts(void);
306 /* Undo the effect of Py_Initialize().
308 Beware: if multiple interpreter and/or thread states exist, these
309 are not wiped out; only the current thread and interpreter state
310 are deleted. But since everything else is deleted, those other
311 interpreter and thread states should no longer be used.
313 (XXX We should do better, e.g. wipe out all interpreters and
323 PyInterpreterState
*interp
;
324 PyThreadState
*tstate
;
329 /* The interpreter is still entirely intact at this point, and the
330 * exit funcs may be relying on that. In particular, if some thread
331 * or exit func is still waiting to do an import, the import machinery
332 * expects Py_IsInitialized() to return true. So don't say the
333 * interpreter is uninitialized until after the exit funcs have run.
334 * Note that Threading.py uses an exit func to do a join on all the
335 * threads created thru it, so this also protects pending imports in
336 * the threads created via Threading.
341 /* Get current thread state and interpreter pointer */
342 tstate
= PyThreadState_GET();
343 interp
= tstate
->interp
;
345 /* Disable signal handling */
346 PyOS_FiniInterrupts();
348 /* drop module references we saved */
349 Py_XDECREF(warnings_module
);
350 warnings_module
= NULL
;
352 /* Collect garbage. This may call finalizers; it's nice to call these
353 * before all modules are destroyed.
354 * XXX If a __del__ or weakref callback is triggered here, and tries to
355 * XXX import a module, bad things can happen, because Python no
356 * XXX longer believes it's initialized.
357 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
358 * XXX is easy to provoke that way. I've also seen, e.g.,
359 * XXX Exception exceptions.ImportError: 'No module named sha'
360 * XXX in <function callback at 0x008F5718> ignored
361 * XXX but I'm unclear on exactly how that one happens. In any case,
362 * XXX I haven't seen a real-life report of either of these.
366 /* Destroy all modules */
369 /* Collect final garbage. This disposes of cycles created by
370 * new-style class definitions, for example.
371 * XXX This is disabled because it caused too many problems. If
372 * XXX a __del__ or weakref callback triggers here, Python code has
373 * XXX a hard time running, because even the sys module has been
374 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
375 * XXX One symptom is a sequence of information-free messages
376 * XXX coming from threads (if a __del__ or callback is invoked,
377 * XXX other threads can execute too, and any exception they encounter
378 * XXX triggers a comedy of errors as subsystem after subsystem
379 * XXX fails to find what it *expects* to find in sys to help report
380 * XXX the exception and consequent unexpected failures). I've also
381 * XXX seen segfaults then, after adding print statements to the
382 * XXX Python code getting called.
388 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
391 /* Debugging stuff */
399 /* Display all objects still alive -- this can invoke arbitrary
400 * __repr__ overrides, so requires a mostly-intact interpreter.
401 * Alas, a lot of stuff may still be alive now that will be cleaned
404 if (Py_GETENV("PYTHONDUMPREFS"))
405 _Py_PrintReferences(stderr
);
406 #endif /* Py_TRACE_REFS */
408 /* Cleanup auto-thread-state */
411 #endif /* WITH_THREAD */
413 /* Clear interpreter state */
414 PyInterpreterState_Clear(interp
);
416 /* Now we decref the exception classes. After this point nothing
417 can raise an exception. That's okay, because each Fini() method
418 below has been checked to make sure no exceptions are ever
424 /* Delete current thread */
425 PyThreadState_Swap(NULL
);
426 PyInterpreterState_Delete(interp
);
428 /* Sundry finalizers */
439 #ifdef Py_USING_UNICODE
440 /* Cleanup Unicode implementation */
444 /* XXX Still allocated:
445 - various static ad-hoc pointers to interned strings
446 - int and float free list blocks
447 - whatever various modules and libraries allocate
450 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
453 /* Display addresses (& refcnts) of all objects still alive.
454 * An address can be used to find the repr of the object, printed
455 * above by _Py_PrintReferences.
457 if (Py_GETENV("PYTHONDUMPREFS"))
458 _Py_PrintReferenceAddresses(stderr
);
459 #endif /* Py_TRACE_REFS */
460 #ifdef PYMALLOC_DEBUG
461 if (Py_GETENV("PYTHONMALLOCSTATS"))
462 _PyObject_DebugMallocStats();
468 /* Create and initialize a new interpreter and thread, and return the
469 new thread. This requires that Py_Initialize() has been called
472 Unsuccessful initialization yields a NULL pointer. Note that *no*
473 exception information is available even in this case -- the
474 exception information is held in the thread, and there is no
482 Py_NewInterpreter(void)
484 PyInterpreterState
*interp
;
485 PyThreadState
*tstate
, *save_tstate
;
486 PyObject
*bimod
, *sysmod
;
489 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
491 interp
= PyInterpreterState_New();
495 tstate
= PyThreadState_New(interp
);
496 if (tstate
== NULL
) {
497 PyInterpreterState_Delete(interp
);
501 save_tstate
= PyThreadState_Swap(tstate
);
503 /* XXX The following is lax in error checking */
505 interp
->modules
= PyDict_New();
507 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
509 interp
->builtins
= PyModule_GetDict(bimod
);
510 Py_INCREF(interp
->builtins
);
512 sysmod
= _PyImport_FindExtension("sys", "sys");
513 if (bimod
!= NULL
&& sysmod
!= NULL
) {
514 interp
->sysdict
= PyModule_GetDict(sysmod
);
515 Py_INCREF(interp
->sysdict
);
516 PySys_SetPath(Py_GetPath());
517 PyDict_SetItemString(interp
->sysdict
, "modules",
519 _PyImportHooks_Init();
525 if (!PyErr_Occurred())
528 /* Oops, it didn't work. Undo it all. */
531 PyThreadState_Clear(tstate
);
532 PyThreadState_Swap(save_tstate
);
533 PyThreadState_Delete(tstate
);
534 PyInterpreterState_Delete(interp
);
539 /* Delete an interpreter and its last thread. This requires that the
540 given thread state is current, that the thread has no remaining
541 frames, and that it is its interpreter's only remaining thread.
542 It is a fatal error to violate these constraints.
544 (Py_Finalize() doesn't have these constraints -- it zaps
545 everything, regardless.)
552 Py_EndInterpreter(PyThreadState
*tstate
)
554 PyInterpreterState
*interp
= tstate
->interp
;
556 if (tstate
!= PyThreadState_GET())
557 Py_FatalError("Py_EndInterpreter: thread is not current");
558 if (tstate
->frame
!= NULL
)
559 Py_FatalError("Py_EndInterpreter: thread still has a frame");
560 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
561 Py_FatalError("Py_EndInterpreter: not the last thread");
564 PyInterpreterState_Clear(interp
);
565 PyThreadState_Swap(NULL
);
566 PyInterpreterState_Delete(interp
);
569 static char *progname
= "python";
572 Py_SetProgramName(char *pn
)
579 Py_GetProgramName(void)
584 static char *default_home
= NULL
;
587 Py_SetPythonHome(char *home
)
593 Py_GetPythonHome(void)
595 char *home
= default_home
;
596 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
597 home
= Py_GETENV("PYTHONHOME");
601 /* Create __main__ module */
607 m
= PyImport_AddModule("__main__");
609 Py_FatalError("can't create __main__ module");
610 d
= PyModule_GetDict(m
);
611 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
612 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
614 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
615 Py_FatalError("can't add __builtins__ to __main__");
620 /* Import the site module (not into __main__ though) */
626 m
= PyImport_ImportModule("site");
628 f
= PySys_GetObject("stderr");
629 if (Py_VerboseFlag
) {
631 "'import site' failed; traceback:\n", f
);
636 "'import site' failed; use -v for traceback\n", f
);
645 /* Parse input from a file and execute it */
648 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
649 PyCompilerFlags
*flags
)
651 if (filename
== NULL
)
653 if (Py_FdIsInteractive(fp
, filename
)) {
654 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
660 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
664 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
668 PyCompilerFlags local_flags
;
671 flags
= &local_flags
;
672 local_flags
.cf_flags
= 0;
674 v
= PySys_GetObject("ps1");
676 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
679 v
= PySys_GetObject("ps2");
681 PySys_SetObject("ps2", v
= PyString_FromString("... "));
685 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
696 /* compute parser flags based on compiler flags */
697 #define PARSER_FLAGS(flags) \
698 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
699 PyPARSE_DONT_IMPLY_DEDENT : 0) \
700 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
701 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
704 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
706 PyObject
*m
, *d
, *v
, *w
;
709 char *ps1
= "", *ps2
= "";
712 v
= PySys_GetObject("ps1");
717 else if (PyString_Check(v
))
718 ps1
= PyString_AsString(v
);
720 w
= PySys_GetObject("ps2");
725 else if (PyString_Check(w
))
726 ps2
= PyString_AsString(w
);
728 arena
= PyArena_New();
729 mod
= PyParser_ASTFromFile(fp
, filename
,
730 Py_single_input
, ps1
, ps2
,
731 flags
, &errcode
, arena
);
736 if (errcode
== E_EOF
) {
743 m
= PyImport_AddModule("__main__");
748 d
= PyModule_GetDict(m
);
749 v
= run_mod(mod
, filename
, d
, d
, flags
, arena
);
761 /* Check whether a file maybe a pyc file: Look at the extension,
762 the file type, and, if we may close it, at the first few bytes. */
765 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
767 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
770 /* Only look into the file if we are allowed to close it, since
771 it then should also be seekable. */
773 /* Read only two bytes of the magic. If the file was opened in
774 text mode, the bytes 3 and 4 of the magic (\r\n) might not
775 be read as they are on disk. */
776 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
777 unsigned char buf
[2];
778 /* Mess: In case of -x, the stream is NOT at its start now,
779 and ungetc() was used to push back the first newline,
780 which makes the current stream position formally undefined,
781 and a x-platform nightmare.
782 Unfortunately, we have no direct way to know whether -x
783 was specified. So we use a terrible hack: if the current
784 stream position is not 0, we assume -x was specified, and
785 give up. Bug 132850 on SourceForge spells out the
786 hopelessness of trying anything else (fseek and ftell
787 don't work predictably x-platform for text-mode files).
790 if (ftell(fp
) == 0) {
791 if (fread(buf
, 1, 2, fp
) == 2 &&
792 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
802 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
803 PyCompilerFlags
*flags
)
808 m
= PyImport_AddModule("__main__");
811 d
= PyModule_GetDict(m
);
812 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
813 PyObject
*f
= PyString_FromString(filename
);
816 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
822 ext
= filename
+ strlen(filename
) - 4;
823 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
824 /* Try to run a pyc file. First, re-open in binary */
827 if ((fp
= fopen(filename
, "rb")) == NULL
) {
828 fprintf(stderr
, "python: Can't reopen .pyc file\n");
831 /* Turn on optimization if a .pyo file is given */
832 if (strcmp(ext
, ".pyo") == 0)
834 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
836 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
850 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
853 m
= PyImport_AddModule("__main__");
856 d
= PyModule_GetDict(m
);
857 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
869 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
870 int *lineno
, int *offset
, const char **text
)
875 /* old style errors */
876 if (PyTuple_Check(err
))
877 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
878 lineno
, offset
, text
);
880 /* new style errors. `err' is an instance */
882 if (! (v
= PyObject_GetAttrString(err
, "msg")))
886 if (!(v
= PyObject_GetAttrString(err
, "filename")))
890 else if (! (*filename
= PyString_AsString(v
)))
894 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
896 hold
= PyInt_AsLong(v
);
899 if (hold
< 0 && PyErr_Occurred())
903 if (!(v
= PyObject_GetAttrString(err
, "offset")))
910 hold
= PyInt_AsLong(v
);
913 if (hold
< 0 && PyErr_Occurred())
918 if (!(v
= PyObject_GetAttrString(err
, "text")))
922 else if (! (*text
= PyString_AsString(v
)))
939 print_error_text(PyObject
*f
, int offset
, const char *text
)
943 if (offset
> 0 && offset
== (int)strlen(text
))
946 nl
= strchr(text
, '\n');
947 if (nl
== NULL
|| nl
-text
>= offset
)
949 offset
-= (int)(nl
+1-text
);
952 while (*text
== ' ' || *text
== '\t') {
957 PyFile_WriteString(" ", f
);
958 PyFile_WriteString(text
, f
);
959 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
960 PyFile_WriteString("\n", f
);
963 PyFile_WriteString(" ", f
);
966 PyFile_WriteString(" ", f
);
969 PyFile_WriteString("^\n", f
);
973 handle_system_exit(void)
975 PyObject
*exception
, *value
, *tb
;
978 PyErr_Fetch(&exception
, &value
, &tb
);
982 if (value
== NULL
|| value
== Py_None
)
984 if (PyExceptionInstance_Check(value
)) {
985 /* The error code should be in the `code' attribute. */
986 PyObject
*code
= PyObject_GetAttrString(value
, "code");
990 if (value
== Py_None
)
993 /* If we failed to dig out the 'code' attribute,
994 just let the else clause below print the error. */
996 if (PyInt_Check(value
))
997 exitcode
= (int)PyInt_AsLong(value
);
999 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1000 PySys_WriteStderr("\n");
1004 /* Restore and clear the exception info, in order to properly decref
1005 * the exception, value, and traceback. If we just exit instead,
1006 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1007 * some finalizers from running.
1009 PyErr_Restore(exception
, value
, tb
);
1016 PyErr_PrintEx(int set_sys_last_vars
)
1018 PyObject
*exception
, *v
, *tb
, *hook
;
1020 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1021 handle_system_exit();
1023 PyErr_Fetch(&exception
, &v
, &tb
);
1024 if (exception
== NULL
)
1026 PyErr_NormalizeException(&exception
, &v
, &tb
);
1027 if (exception
== NULL
)
1029 /* Now we know v != NULL too */
1030 if (set_sys_last_vars
) {
1031 PySys_SetObject("last_type", exception
);
1032 PySys_SetObject("last_value", v
);
1033 PySys_SetObject("last_traceback", tb
);
1035 hook
= PySys_GetObject("excepthook");
1037 PyObject
*args
= PyTuple_Pack(3,
1038 exception
, v
, tb
? tb
: Py_None
);
1039 PyObject
*result
= PyEval_CallObject(hook
, args
);
1040 if (result
== NULL
) {
1041 PyObject
*exception2
, *v2
, *tb2
;
1042 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1043 handle_system_exit();
1045 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1046 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1050 PySys_WriteStderr("Error in sys.excepthook:\n");
1051 PyErr_Display(exception2
, v2
, tb2
);
1052 PySys_WriteStderr("\nOriginal exception was:\n");
1053 PyErr_Display(exception
, v
, tb
);
1054 Py_XDECREF(exception2
);
1061 PySys_WriteStderr("sys.excepthook is missing\n");
1062 PyErr_Display(exception
, v
, tb
);
1064 Py_XDECREF(exception
);
1069 void PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1072 PyObject
*f
= PySys_GetObject("stderr");
1075 fprintf(stderr
, "lost sys.stderr\n");
1080 if (tb
&& tb
!= Py_None
)
1081 err
= PyTraceBack_Print(tb
, f
);
1083 PyObject_HasAttrString(value
, "print_file_and_line"))
1086 const char *filename
, *text
;
1088 if (!parse_syntax_error(value
, &message
, &filename
,
1089 &lineno
, &offset
, &text
))
1093 PyFile_WriteString(" File \"", f
);
1094 if (filename
== NULL
)
1095 PyFile_WriteString("<string>", f
);
1097 PyFile_WriteString(filename
, f
);
1098 PyFile_WriteString("\", line ", f
);
1099 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1100 PyFile_WriteString(buf
, f
);
1101 PyFile_WriteString("\n", f
);
1103 print_error_text(f
, offset
, text
);
1106 /* Can't be bothered to check all those
1107 PyFile_WriteString() calls */
1108 if (PyErr_Occurred())
1113 /* Don't do anything else */
1115 else if (PyExceptionClass_Check(exception
)) {
1116 char* className
= PyExceptionClass_Name(exception
);
1117 PyObject
* moduleName
=
1118 PyObject_GetAttrString(exception
, "__module__");
1120 if (moduleName
== NULL
)
1121 err
= PyFile_WriteString("<unknown>", f
);
1123 char* modstr
= PyString_AsString(moduleName
);
1124 Py_DECREF(moduleName
);
1125 if (modstr
&& strcmp(modstr
, "exceptions"))
1127 err
= PyFile_WriteString(modstr
, f
);
1128 err
+= PyFile_WriteString(".", f
);
1132 if (className
== NULL
)
1133 err
= PyFile_WriteString("<unknown>", f
);
1135 err
= PyFile_WriteString(className
, f
);
1139 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1140 if (err
== 0 && (value
!= Py_None
)) {
1141 PyObject
*s
= PyObject_Str(value
);
1142 /* only print colon if the str() of the
1143 object is not the empty string
1147 else if (!PyString_Check(s
) ||
1148 PyString_GET_SIZE(s
) != 0)
1149 err
= PyFile_WriteString(": ", f
);
1151 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1155 err
= PyFile_WriteString("\n", f
);
1158 /* If an error happened here, don't show it.
1159 XXX This is wrong, but too many callers rely on this behavior. */
1165 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1166 PyObject
*locals
, PyCompilerFlags
*flags
)
1168 PyObject
*ret
= NULL
;
1169 PyArena
*arena
= PyArena_New();
1170 mod_ty mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
,
1173 ret
= run_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1174 PyArena_Free(arena
);
1179 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1180 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1183 PyArena
*arena
= PyArena_New();
1184 mod_ty mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1185 flags
, NULL
, arena
);
1187 PyArena_Free(arena
);
1192 ret
= run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1193 PyArena_Free(arena
);
1198 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1199 PyCompilerFlags
*flags
, PyArena
*arena
)
1203 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1206 v
= PyEval_EvalCode(co
, globals
, locals
);
1212 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1213 PyObject
*locals
, PyCompilerFlags
*flags
)
1218 long PyImport_GetMagicNumber(void);
1220 magic
= PyMarshal_ReadLongFromFile(fp
);
1221 if (magic
!= PyImport_GetMagicNumber()) {
1222 PyErr_SetString(PyExc_RuntimeError
,
1223 "Bad magic number in .pyc file");
1226 (void) PyMarshal_ReadLongFromFile(fp
);
1227 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1229 if (v
== NULL
|| !PyCode_Check(v
)) {
1231 PyErr_SetString(PyExc_RuntimeError
,
1232 "Bad code object in .pyc file");
1235 co
= (PyCodeObject
*)v
;
1236 v
= PyEval_EvalCode(co
, globals
, locals
);
1238 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1244 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1245 PyCompilerFlags
*flags
)
1248 PyArena
*arena
= PyArena_New();
1249 mod_ty mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1251 PyArena_Free(arena
);
1254 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1255 PyObject
*result
= PyAST_mod2obj(mod
);
1256 PyArena_Free(arena
);
1259 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1260 PyArena_Free(arena
);
1261 return (PyObject
*)co
;
1265 Py_SymtableString(const char *str
, const char *filename
, int start
)
1267 struct symtable
*st
;
1268 PyArena
*arena
= PyArena_New();
1269 mod_ty mod
= PyParser_ASTFromString(str
, filename
, start
, NULL
, arena
);
1271 PyArena_Free(arena
);
1274 st
= PySymtable_Build(mod
, filename
, 0);
1275 PyArena_Free(arena
);
1279 /* Preferred access to parser is through AST. */
1281 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1282 PyCompilerFlags
*flags
, PyArena
*arena
)
1286 node
*n
= PyParser_ParseStringFlagsFilename(s
, filename
,
1287 &_PyParser_Grammar
, start
, &err
,
1288 PARSER_FLAGS(flags
));
1290 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1301 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1302 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1307 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1308 start
, ps1
, ps2
, &err
, PARSER_FLAGS(flags
));
1310 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1317 *errcode
= err
.error
;
1322 /* Simplified interface to parsefile -- return node or set exception */
1325 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1328 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1329 start
, NULL
, NULL
, &err
, flags
);
1336 /* Simplified interface to parsestring -- return node or set exception */
1339 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1342 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1343 start
, &err
, flags
);
1350 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1351 int start
, int flags
)
1354 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1355 &_PyParser_Grammar
, start
, &err
, flags
);
1362 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1364 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1367 /* May want to move a more generalized form of this to parsetok.c or
1368 even parser modules. */
1371 PyParser_SetError(perrdetail
*err
)
1376 /* Set the error appropriate to the given input error code (see errcode.h) */
1379 err_input(perrdetail
*err
)
1381 PyObject
*v
, *w
, *errtype
;
1384 errtype
= PyExc_SyntaxError
;
1385 switch (err
->error
) {
1387 errtype
= PyExc_IndentationError
;
1388 if (err
->expected
== INDENT
)
1389 msg
= "expected an indented block";
1390 else if (err
->token
== INDENT
)
1391 msg
= "unexpected indent";
1392 else if (err
->token
== DEDENT
)
1393 msg
= "unexpected unindent";
1395 errtype
= PyExc_SyntaxError
;
1396 msg
= "invalid syntax";
1400 msg
= "invalid token";
1403 msg
= "EOF while scanning triple-quoted string";
1406 msg
= "EOL while scanning single-quoted string";
1409 if (!PyErr_Occurred())
1410 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1416 msg
= "unexpected EOF while parsing";
1419 errtype
= PyExc_TabError
;
1420 msg
= "inconsistent use of tabs and spaces in indentation";
1423 msg
= "expression too long";
1426 errtype
= PyExc_IndentationError
;
1427 msg
= "unindent does not match any outer indentation level";
1430 errtype
= PyExc_IndentationError
;
1431 msg
= "too many levels of indentation";
1434 PyObject
*type
, *value
, *tb
;
1435 PyErr_Fetch(&type
, &value
, &tb
);
1436 if (value
!= NULL
) {
1437 u
= PyObject_Str(value
);
1439 msg
= PyString_AsString(u
);
1443 msg
= "unknown decode error";
1450 msg
= "unexpected character after line continuation character";
1453 fprintf(stderr
, "error=%d\n", err
->error
);
1454 msg
= "unknown parsing error";
1457 v
= Py_BuildValue("(ziiz)", err
->filename
,
1458 err
->lineno
, err
->offset
, err
->text
);
1459 if (err
->text
!= NULL
) {
1460 PyMem_DEL(err
->text
);
1465 w
= Py_BuildValue("(sO)", msg
, v
);
1468 PyErr_SetObject(errtype
, w
);
1472 /* Print fatal error message and abort */
1475 Py_FatalError(const char *msg
)
1477 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1479 OutputDebugString("Fatal Python error: ");
1480 OutputDebugString(msg
);
1481 OutputDebugString("\n");
1485 #endif /* MS_WINDOWS */
1489 /* Clean up and exit */
1492 #include "pythread.h"
1495 #define NEXITFUNCS 32
1496 static void (*exitfuncs
[NEXITFUNCS
])(void);
1497 static int nexitfuncs
= 0;
1499 int Py_AtExit(void (*func
)(void))
1501 if (nexitfuncs
>= NEXITFUNCS
)
1503 exitfuncs
[nexitfuncs
++] = func
;
1508 call_sys_exitfunc(void)
1510 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1514 Py_INCREF(exitfunc
);
1515 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1516 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1518 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1519 PySys_WriteStderr("Error in sys.exitfunc:\n");
1523 Py_DECREF(exitfunc
);
1531 call_ll_exitfuncs(void)
1533 while (nexitfuncs
> 0)
1534 (*exitfuncs
[--nexitfuncs
])();
1552 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1555 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1558 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1560 PyOS_InitInterrupts(); /* May imply initsignal() */
1565 * The file descriptor fd is considered ``interactive'' if either
1566 * a) isatty(fd) is TRUE, or
1567 * b) the -i flag was given, and the filename associated with
1568 * the descriptor is NULL or "<stdin>" or "???".
1571 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1573 if (isatty((int)fileno(fp
)))
1575 if (!Py_InteractiveFlag
)
1577 return (filename
== NULL
) ||
1578 (strcmp(filename
, "<stdin>") == 0) ||
1579 (strcmp(filename
, "???") == 0);
1583 #if defined(USE_STACKCHECK)
1584 #if defined(WIN32) && defined(_MSC_VER)
1586 /* Stack checking for Microsoft C */
1592 * Return non-zero when we run out of memory on the stack; zero otherwise.
1595 PyOS_CheckStack(void)
1598 /* alloca throws a stack overflow exception if there's
1599 not enough space left on the stack */
1600 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1602 } __except (EXCEPTION_EXECUTE_HANDLER
) {
1603 /* just ignore all errors */
1608 #endif /* WIN32 && _MSC_VER */
1610 /* Alternate implementations can be added here... */
1612 #endif /* USE_STACKCHECK */
1615 /* Wrappers around sigaction() or signal(). */
1618 PyOS_getsig(int sig
)
1620 #ifdef HAVE_SIGACTION
1621 struct sigaction context
;
1622 if (sigaction(sig
, NULL
, &context
) == -1)
1624 return context
.sa_handler
;
1626 PyOS_sighandler_t handler
;
1627 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1628 #if defined(_MSC_VER) && _MSC_VER >= 1400
1630 /* Only these signals are valid */
1639 /* Don't call signal() with other values or it will assert */
1643 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1644 handler
= signal(sig
, SIG_IGN
);
1645 if (handler
!= SIG_ERR
)
1646 signal(sig
, handler
);
1652 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1654 #ifdef HAVE_SIGACTION
1655 struct sigaction context
, ocontext
;
1656 context
.sa_handler
= handler
;
1657 sigemptyset(&context
.sa_mask
);
1658 context
.sa_flags
= 0;
1659 if (sigaction(sig
, &context
, &ocontext
) == -1)
1661 return ocontext
.sa_handler
;
1663 PyOS_sighandler_t oldhandler
;
1664 oldhandler
= signal(sig
, handler
);
1665 #ifdef HAVE_SIGINTERRUPT
1666 siginterrupt(sig
, 1);
1672 /* Deprecated C API functions still provided for binary compatiblity */
1674 #undef PyParser_SimpleParseFile
1675 #undef PyParser_SimpleParseString
1678 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1680 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1684 PyParser_SimpleParseString(const char *str
, int start
)
1686 return PyParser_SimpleParseStringFlags(str
, start
, 0);