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");
186 Py_FatalError("Py_Initialize: can't init longs");
188 if (!PyByteArray_Init())
189 Py_FatalError("Py_Initialize: can't init bytearray");
193 interp
->modules
= PyDict_New();
194 if (interp
->modules
== NULL
)
195 Py_FatalError("Py_Initialize: can't make modules dictionary");
196 interp
->modules_reloading
= PyDict_New();
197 if (interp
->modules_reloading
== NULL
)
198 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
200 #ifdef Py_USING_UNICODE
201 /* Init Unicode implementation; relies on the codec registry */
205 bimod
= _PyBuiltin_Init();
207 Py_FatalError("Py_Initialize: can't initialize __builtin__");
208 interp
->builtins
= PyModule_GetDict(bimod
);
209 if (interp
->builtins
== NULL
)
210 Py_FatalError("Py_Initialize: can't initialize builtins dict");
211 Py_INCREF(interp
->builtins
);
213 sysmod
= _PySys_Init();
215 Py_FatalError("Py_Initialize: can't initialize sys");
216 interp
->sysdict
= PyModule_GetDict(sysmod
);
217 if (interp
->sysdict
== NULL
)
218 Py_FatalError("Py_Initialize: can't initialize sys dict");
219 Py_INCREF(interp
->sysdict
);
220 _PyImport_FixupExtension("sys", "sys");
221 PySys_SetPath(Py_GetPath());
222 PyDict_SetItemString(interp
->sysdict
, "modules",
227 /* initialize builtin exceptions */
229 _PyImport_FixupExtension("exceptions", "exceptions");
231 /* phase 2 of builtins */
232 _PyImport_FixupExtension("__builtin__", "__builtin__");
234 _PyImportHooks_Init();
237 initsigs(); /* Signal handling stuff, including initintr() */
239 /* Initialize warnings. */
241 if (PySys_HasWarnOptions()) {
242 PyObject
*warnings_module
= PyImport_ImportModule("warnings");
243 if (!warnings_module
)
245 Py_XDECREF(warnings_module
);
248 initmain(); /* Module __main__ */
250 initsite(); /* Module site */
252 /* auto-thread-state API, if available */
254 _PyGILState_Init(interp
, tstate
);
255 #endif /* WITH_THREAD */
257 if ((p
= Py_GETENV("PYTHONIOENCODING")) && *p
!= '\0') {
258 p
= icodeset
= codeset
= strdup(p
);
260 errors
= strchr(p
, ':');
268 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
269 /* On Unix, set the file system encoding according to the
270 user's preference, if the CODESET names a well-known
271 Python codec, and Py_FileSystemDefaultEncoding isn't
272 initialized by other means. Also set the encoding of
273 stdin and stdout if these are terminals, unless overridden. */
275 if (!overridden
|| !Py_FileSystemDefaultEncoding
) {
276 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
277 setlocale(LC_CTYPE
, "");
278 loc_codeset
= nl_langinfo(CODESET
);
279 if (loc_codeset
&& *loc_codeset
) {
280 PyObject
*enc
= PyCodec_Encoder(loc_codeset
);
282 loc_codeset
= strdup(loc_codeset
);
290 setlocale(LC_CTYPE
, saved_locale
);
294 codeset
= icodeset
= loc_codeset
;
298 /* Initialize Py_FileSystemDefaultEncoding from
299 locale even if PYTHONIOENCODING is set. */
300 if (!Py_FileSystemDefaultEncoding
) {
301 Py_FileSystemDefaultEncoding
= loc_codeset
;
312 sprintf(ibuf
, "cp%d", GetConsoleCP());
313 sprintf(buf
, "cp%d", GetConsoleOutputCP());
318 sys_stream
= PySys_GetObject("stdin");
319 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
323 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
324 PyFile_Check(sys_stream
)) {
325 if (!PyFile_SetEncodingAndErrors(sys_stream
, icodeset
, errors
))
326 Py_FatalError("Cannot set codeset of stdin");
328 Py_XDECREF(sys_isatty
);
330 sys_stream
= PySys_GetObject("stdout");
331 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
335 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
336 PyFile_Check(sys_stream
)) {
337 if (!PyFile_SetEncodingAndErrors(sys_stream
, codeset
, errors
))
338 Py_FatalError("Cannot set codeset of stdout");
340 Py_XDECREF(sys_isatty
);
342 sys_stream
= PySys_GetObject("stderr");
343 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
347 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
348 PyFile_Check(sys_stream
)) {
349 if (!PyFile_SetEncodingAndErrors(sys_stream
, codeset
, errors
))
350 Py_FatalError("Cannot set codeset of stderr");
352 Py_XDECREF(sys_isatty
);
367 extern void dump_counts(FILE*);
370 /* Undo the effect of Py_Initialize().
372 Beware: if multiple interpreter and/or thread states exist, these
373 are not wiped out; only the current thread and interpreter state
374 are deleted. But since everything else is deleted, those other
375 interpreter and thread states should no longer be used.
377 (XXX We should do better, e.g. wipe out all interpreters and
387 PyInterpreterState
*interp
;
388 PyThreadState
*tstate
;
393 /* The interpreter is still entirely intact at this point, and the
394 * exit funcs may be relying on that. In particular, if some thread
395 * or exit func is still waiting to do an import, the import machinery
396 * expects Py_IsInitialized() to return true. So don't say the
397 * interpreter is uninitialized until after the exit funcs have run.
398 * Note that Threading.py uses an exit func to do a join on all the
399 * threads created thru it, so this also protects pending imports in
400 * the threads created via Threading.
405 /* Get current thread state and interpreter pointer */
406 tstate
= PyThreadState_GET();
407 interp
= tstate
->interp
;
409 /* Disable signal handling */
410 PyOS_FiniInterrupts();
412 /* Clear type lookup cache */
415 /* Collect garbage. This may call finalizers; it's nice to call these
416 * before all modules are destroyed.
417 * XXX If a __del__ or weakref callback is triggered here, and tries to
418 * XXX import a module, bad things can happen, because Python no
419 * XXX longer believes it's initialized.
420 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
421 * XXX is easy to provoke that way. I've also seen, e.g.,
422 * XXX Exception exceptions.ImportError: 'No module named sha'
423 * XXX in <function callback at 0x008F5718> ignored
424 * XXX but I'm unclear on exactly how that one happens. In any case,
425 * XXX I haven't seen a real-life report of either of these.
429 /* With COUNT_ALLOCS, it helps to run GC multiple times:
430 each collection might release some types from the type
431 list, so they become garbage. */
432 while (PyGC_Collect() > 0)
436 /* Destroy all modules */
439 /* Collect final garbage. This disposes of cycles created by
440 * new-style class definitions, for example.
441 * XXX This is disabled because it caused too many problems. If
442 * XXX a __del__ or weakref callback triggers here, Python code has
443 * XXX a hard time running, because even the sys module has been
444 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
445 * XXX One symptom is a sequence of information-free messages
446 * XXX coming from threads (if a __del__ or callback is invoked,
447 * XXX other threads can execute too, and any exception they encounter
448 * XXX triggers a comedy of errors as subsystem after subsystem
449 * XXX fails to find what it *expects* to find in sys to help report
450 * XXX the exception and consequent unexpected failures). I've also
451 * XXX seen segfaults then, after adding print statements to the
452 * XXX Python code getting called.
458 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
461 /* Debugging stuff */
469 /* Display all objects still alive -- this can invoke arbitrary
470 * __repr__ overrides, so requires a mostly-intact interpreter.
471 * Alas, a lot of stuff may still be alive now that will be cleaned
474 if (Py_GETENV("PYTHONDUMPREFS"))
475 _Py_PrintReferences(stderr
);
476 #endif /* Py_TRACE_REFS */
478 /* Clear interpreter state */
479 PyInterpreterState_Clear(interp
);
481 /* Now we decref the exception classes. After this point nothing
482 can raise an exception. That's okay, because each Fini() method
483 below has been checked to make sure no exceptions are ever
489 /* Cleanup auto-thread-state */
492 #endif /* WITH_THREAD */
494 /* Delete current thread */
495 PyThreadState_Swap(NULL
);
496 PyInterpreterState_Delete(interp
);
498 /* Sundry finalizers */
511 #ifdef Py_USING_UNICODE
512 /* Cleanup Unicode implementation */
516 /* XXX Still allocated:
517 - various static ad-hoc pointers to interned strings
518 - int and float free list blocks
519 - whatever various modules and libraries allocate
522 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
525 /* Display addresses (& refcnts) of all objects still alive.
526 * An address can be used to find the repr of the object, printed
527 * above by _Py_PrintReferences.
529 if (Py_GETENV("PYTHONDUMPREFS"))
530 _Py_PrintReferenceAddresses(stderr
);
531 #endif /* Py_TRACE_REFS */
532 #ifdef PYMALLOC_DEBUG
533 if (Py_GETENV("PYTHONMALLOCSTATS"))
534 _PyObject_DebugMallocStats();
540 /* Create and initialize a new interpreter and thread, and return the
541 new thread. This requires that Py_Initialize() has been called
544 Unsuccessful initialization yields a NULL pointer. Note that *no*
545 exception information is available even in this case -- the
546 exception information is held in the thread, and there is no
554 Py_NewInterpreter(void)
556 PyInterpreterState
*interp
;
557 PyThreadState
*tstate
, *save_tstate
;
558 PyObject
*bimod
, *sysmod
;
561 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
563 interp
= PyInterpreterState_New();
567 tstate
= PyThreadState_New(interp
);
568 if (tstate
== NULL
) {
569 PyInterpreterState_Delete(interp
);
573 save_tstate
= PyThreadState_Swap(tstate
);
575 /* XXX The following is lax in error checking */
577 interp
->modules
= PyDict_New();
578 interp
->modules_reloading
= PyDict_New();
580 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
582 interp
->builtins
= PyModule_GetDict(bimod
);
583 if (interp
->builtins
== NULL
)
585 Py_INCREF(interp
->builtins
);
587 sysmod
= _PyImport_FindExtension("sys", "sys");
588 if (bimod
!= NULL
&& sysmod
!= NULL
) {
589 interp
->sysdict
= PyModule_GetDict(sysmod
);
590 if (interp
->sysdict
== NULL
)
592 Py_INCREF(interp
->sysdict
);
593 PySys_SetPath(Py_GetPath());
594 PyDict_SetItemString(interp
->sysdict
, "modules",
596 _PyImportHooks_Init();
602 if (!PyErr_Occurred())
606 /* Oops, it didn't work. Undo it all. */
609 PyThreadState_Clear(tstate
);
610 PyThreadState_Swap(save_tstate
);
611 PyThreadState_Delete(tstate
);
612 PyInterpreterState_Delete(interp
);
617 /* Delete an interpreter and its last thread. This requires that the
618 given thread state is current, that the thread has no remaining
619 frames, and that it is its interpreter's only remaining thread.
620 It is a fatal error to violate these constraints.
622 (Py_Finalize() doesn't have these constraints -- it zaps
623 everything, regardless.)
630 Py_EndInterpreter(PyThreadState
*tstate
)
632 PyInterpreterState
*interp
= tstate
->interp
;
634 if (tstate
!= PyThreadState_GET())
635 Py_FatalError("Py_EndInterpreter: thread is not current");
636 if (tstate
->frame
!= NULL
)
637 Py_FatalError("Py_EndInterpreter: thread still has a frame");
638 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
639 Py_FatalError("Py_EndInterpreter: not the last thread");
642 PyInterpreterState_Clear(interp
);
643 PyThreadState_Swap(NULL
);
644 PyInterpreterState_Delete(interp
);
647 static char *progname
= "python";
650 Py_SetProgramName(char *pn
)
657 Py_GetProgramName(void)
662 static char *default_home
= NULL
;
665 Py_SetPythonHome(char *home
)
671 Py_GetPythonHome(void)
673 char *home
= default_home
;
674 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
675 home
= Py_GETENV("PYTHONHOME");
679 /* Create __main__ module */
685 m
= PyImport_AddModule("__main__");
687 Py_FatalError("can't create __main__ module");
688 d
= PyModule_GetDict(m
);
689 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
690 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
692 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
693 Py_FatalError("can't add __builtins__ to __main__");
698 /* Import the site module (not into __main__ though) */
704 m
= PyImport_ImportModule("site");
706 f
= PySys_GetObject("stderr");
707 if (Py_VerboseFlag
) {
709 "'import site' failed; traceback:\n", f
);
714 "'import site' failed; use -v for traceback\n", f
);
723 /* Parse input from a file and execute it */
726 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
727 PyCompilerFlags
*flags
)
729 if (filename
== NULL
)
731 if (Py_FdIsInteractive(fp
, filename
)) {
732 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
738 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
742 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
746 PyCompilerFlags local_flags
;
749 flags
= &local_flags
;
750 local_flags
.cf_flags
= 0;
752 v
= PySys_GetObject("ps1");
754 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
757 v
= PySys_GetObject("ps2");
759 PySys_SetObject("ps2", v
= PyString_FromString("... "));
763 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
775 /* compute parser flags based on compiler flags */
776 #define PARSER_FLAGS(flags) \
777 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
778 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
781 /* Keep an example of flags with future keyword support. */
782 #define PARSER_FLAGS(flags) \
783 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
784 PyPARSE_DONT_IMPLY_DEDENT : 0) \
785 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
786 PyPARSE_PRINT_IS_FUNCTION : 0) \
787 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
788 PyPARSE_UNICODE_LITERALS : 0) \
793 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
795 PyObject
*m
, *d
, *v
, *w
;
798 char *ps1
= "", *ps2
= "";
801 v
= PySys_GetObject("ps1");
806 else if (PyString_Check(v
))
807 ps1
= PyString_AsString(v
);
809 w
= PySys_GetObject("ps2");
814 else if (PyString_Check(w
))
815 ps2
= PyString_AsString(w
);
817 arena
= PyArena_New();
823 mod
= PyParser_ASTFromFile(fp
, filename
,
824 Py_single_input
, ps1
, ps2
,
825 flags
, &errcode
, arena
);
830 if (errcode
== E_EOF
) {
837 m
= PyImport_AddModule("__main__");
842 d
= PyModule_GetDict(m
);
843 v
= run_mod(mod
, filename
, d
, d
, flags
, arena
);
855 /* Check whether a file maybe a pyc file: Look at the extension,
856 the file type, and, if we may close it, at the first few bytes. */
859 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
861 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
864 /* Only look into the file if we are allowed to close it, since
865 it then should also be seekable. */
867 /* Read only two bytes of the magic. If the file was opened in
868 text mode, the bytes 3 and 4 of the magic (\r\n) might not
869 be read as they are on disk. */
870 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
871 unsigned char buf
[2];
872 /* Mess: In case of -x, the stream is NOT at its start now,
873 and ungetc() was used to push back the first newline,
874 which makes the current stream position formally undefined,
875 and a x-platform nightmare.
876 Unfortunately, we have no direct way to know whether -x
877 was specified. So we use a terrible hack: if the current
878 stream position is not 0, we assume -x was specified, and
879 give up. Bug 132850 on SourceForge spells out the
880 hopelessness of trying anything else (fseek and ftell
881 don't work predictably x-platform for text-mode files).
884 if (ftell(fp
) == 0) {
885 if (fread(buf
, 1, 2, fp
) == 2 &&
886 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
896 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
897 PyCompilerFlags
*flags
)
901 int set_file_name
= 0, ret
, len
;
903 m
= PyImport_AddModule("__main__");
906 d
= PyModule_GetDict(m
);
907 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
908 PyObject
*f
= PyString_FromString(filename
);
911 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
918 len
= strlen(filename
);
919 ext
= filename
+ len
- (len
> 4 ? 4 : 0);
920 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
921 /* Try to run a pyc file. First, re-open in binary */
924 if ((fp
= fopen(filename
, "rb")) == NULL
) {
925 fprintf(stderr
, "python: Can't reopen .pyc file\n");
929 /* Turn on optimization if a .pyo file is given */
930 if (strcmp(ext
, ".pyo") == 0)
932 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
934 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
947 if (set_file_name
&& PyDict_DelItemString(d
, "__file__"))
953 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
956 m
= PyImport_AddModule("__main__");
959 d
= PyModule_GetDict(m
);
960 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
972 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
973 int *lineno
, int *offset
, const char **text
)
978 /* old style errors */
979 if (PyTuple_Check(err
))
980 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
981 lineno
, offset
, text
);
983 /* new style errors. `err' is an instance */
985 if (! (v
= PyObject_GetAttrString(err
, "msg")))
989 if (!(v
= PyObject_GetAttrString(err
, "filename")))
993 else if (! (*filename
= PyString_AsString(v
)))
997 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
999 hold
= PyInt_AsLong(v
);
1002 if (hold
< 0 && PyErr_Occurred())
1004 *lineno
= (int)hold
;
1006 if (!(v
= PyObject_GetAttrString(err
, "offset")))
1013 hold
= PyInt_AsLong(v
);
1016 if (hold
< 0 && PyErr_Occurred())
1018 *offset
= (int)hold
;
1021 if (!(v
= PyObject_GetAttrString(err
, "text")))
1025 else if (! (*text
= PyString_AsString(v
)))
1042 print_error_text(PyObject
*f
, int offset
, const char *text
)
1046 if (offset
> 0 && offset
== (int)strlen(text
))
1049 nl
= strchr(text
, '\n');
1050 if (nl
== NULL
|| nl
-text
>= offset
)
1052 offset
-= (int)(nl
+1-text
);
1055 while (*text
== ' ' || *text
== '\t') {
1060 PyFile_WriteString(" ", f
);
1061 PyFile_WriteString(text
, f
);
1062 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
1063 PyFile_WriteString("\n", f
);
1066 PyFile_WriteString(" ", f
);
1068 while (offset
> 0) {
1069 PyFile_WriteString(" ", f
);
1072 PyFile_WriteString("^\n", f
);
1076 handle_system_exit(void)
1078 PyObject
*exception
, *value
, *tb
;
1082 /* Don't exit if -i flag was given. This flag is set to 0
1083 * when entering interactive mode for inspecting. */
1086 PyErr_Fetch(&exception
, &value
, &tb
);
1090 if (value
== NULL
|| value
== Py_None
)
1092 if (PyExceptionInstance_Check(value
)) {
1093 /* The error code should be in the `code' attribute. */
1094 PyObject
*code
= PyObject_GetAttrString(value
, "code");
1098 if (value
== Py_None
)
1101 /* If we failed to dig out the 'code' attribute,
1102 just let the else clause below print the error. */
1104 if (PyInt_Check(value
))
1105 exitcode
= (int)PyInt_AsLong(value
);
1107 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1108 PySys_WriteStderr("\n");
1112 /* Restore and clear the exception info, in order to properly decref
1113 * the exception, value, and traceback. If we just exit instead,
1114 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1115 * some finalizers from running.
1117 PyErr_Restore(exception
, value
, tb
);
1124 PyErr_PrintEx(int set_sys_last_vars
)
1126 PyObject
*exception
, *v
, *tb
, *hook
;
1128 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1129 handle_system_exit();
1131 PyErr_Fetch(&exception
, &v
, &tb
);
1132 if (exception
== NULL
)
1134 PyErr_NormalizeException(&exception
, &v
, &tb
);
1135 if (exception
== NULL
)
1137 /* Now we know v != NULL too */
1138 if (set_sys_last_vars
) {
1139 PySys_SetObject("last_type", exception
);
1140 PySys_SetObject("last_value", v
);
1141 PySys_SetObject("last_traceback", tb
);
1143 hook
= PySys_GetObject("excepthook");
1145 PyObject
*args
= PyTuple_Pack(3,
1146 exception
, v
, tb
? tb
: Py_None
);
1147 PyObject
*result
= PyEval_CallObject(hook
, args
);
1148 if (result
== NULL
) {
1149 PyObject
*exception2
, *v2
, *tb2
;
1150 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1151 handle_system_exit();
1153 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1154 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1155 /* It should not be possible for exception2 or v2
1156 to be NULL. However PyErr_Display() can't
1157 tolerate NULLs, so just be safe. */
1158 if (exception2
== NULL
) {
1159 exception2
= Py_None
;
1160 Py_INCREF(exception2
);
1169 PySys_WriteStderr("Error in sys.excepthook:\n");
1170 PyErr_Display(exception2
, v2
, tb2
);
1171 PySys_WriteStderr("\nOriginal exception was:\n");
1172 PyErr_Display(exception
, v
, tb
);
1173 Py_DECREF(exception2
);
1180 PySys_WriteStderr("sys.excepthook is missing\n");
1181 PyErr_Display(exception
, v
, tb
);
1183 Py_XDECREF(exception
);
1189 PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1192 PyObject
*f
= PySys_GetObject("stderr");
1195 fprintf(stderr
, "lost sys.stderr\n");
1200 if (tb
&& tb
!= Py_None
)
1201 err
= PyTraceBack_Print(tb
, f
);
1203 PyObject_HasAttrString(value
, "print_file_and_line"))
1206 const char *filename
, *text
;
1208 if (!parse_syntax_error(value
, &message
, &filename
,
1209 &lineno
, &offset
, &text
))
1213 PyFile_WriteString(" File \"", f
);
1214 if (filename
== NULL
)
1215 PyFile_WriteString("<string>", f
);
1217 PyFile_WriteString(filename
, f
);
1218 PyFile_WriteString("\", line ", f
);
1219 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1220 PyFile_WriteString(buf
, f
);
1221 PyFile_WriteString("\n", f
);
1223 print_error_text(f
, offset
, text
);
1226 /* Can't be bothered to check all those
1227 PyFile_WriteString() calls */
1228 if (PyErr_Occurred())
1233 /* Don't do anything else */
1235 else if (PyExceptionClass_Check(exception
)) {
1236 PyObject
* moduleName
;
1237 char* className
= PyExceptionClass_Name(exception
);
1238 if (className
!= NULL
) {
1239 char *dot
= strrchr(className
, '.');
1244 moduleName
= PyObject_GetAttrString(exception
, "__module__");
1245 if (moduleName
== NULL
)
1246 err
= PyFile_WriteString("<unknown>", f
);
1248 char* modstr
= PyString_AsString(moduleName
);
1249 if (modstr
&& strcmp(modstr
, "exceptions"))
1251 err
= PyFile_WriteString(modstr
, f
);
1252 err
+= PyFile_WriteString(".", f
);
1254 Py_DECREF(moduleName
);
1257 if (className
== NULL
)
1258 err
= PyFile_WriteString("<unknown>", f
);
1260 err
= PyFile_WriteString(className
, f
);
1264 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1265 if (err
== 0 && (value
!= Py_None
)) {
1266 PyObject
*s
= PyObject_Str(value
);
1267 /* only print colon if the str() of the
1268 object is not the empty string
1272 else if (!PyString_Check(s
) ||
1273 PyString_GET_SIZE(s
) != 0)
1274 err
= PyFile_WriteString(": ", f
);
1276 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1279 /* try to write a newline in any case */
1280 err
+= PyFile_WriteString("\n", f
);
1283 /* If an error happened here, don't show it.
1284 XXX This is wrong, but too many callers rely on this behavior. */
1290 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1291 PyObject
*locals
, PyCompilerFlags
*flags
)
1293 PyObject
*ret
= NULL
;
1295 PyArena
*arena
= PyArena_New();
1299 mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
, arena
);
1301 ret
= run_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1302 PyArena_Free(arena
);
1307 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1308 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1312 PyArena
*arena
= PyArena_New();
1316 mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1317 flags
, NULL
, arena
);
1321 PyArena_Free(arena
);
1324 ret
= run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1325 PyArena_Free(arena
);
1330 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1331 PyCompilerFlags
*flags
, PyArena
*arena
)
1335 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1338 v
= PyEval_EvalCode(co
, globals
, locals
);
1344 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1345 PyObject
*locals
, PyCompilerFlags
*flags
)
1350 long PyImport_GetMagicNumber(void);
1352 magic
= PyMarshal_ReadLongFromFile(fp
);
1353 if (magic
!= PyImport_GetMagicNumber()) {
1354 PyErr_SetString(PyExc_RuntimeError
,
1355 "Bad magic number in .pyc file");
1358 (void) PyMarshal_ReadLongFromFile(fp
);
1359 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1361 if (v
== NULL
|| !PyCode_Check(v
)) {
1363 PyErr_SetString(PyExc_RuntimeError
,
1364 "Bad code object in .pyc file");
1367 co
= (PyCodeObject
*)v
;
1368 v
= PyEval_EvalCode(co
, globals
, locals
);
1370 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1376 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1377 PyCompilerFlags
*flags
)
1381 PyArena
*arena
= PyArena_New();
1385 mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1387 PyArena_Free(arena
);
1390 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1391 PyObject
*result
= PyAST_mod2obj(mod
);
1392 PyArena_Free(arena
);
1395 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1396 PyArena_Free(arena
);
1397 return (PyObject
*)co
;
1401 Py_SymtableString(const char *str
, const char *filename
, int start
)
1403 struct symtable
*st
;
1405 PyCompilerFlags flags
;
1406 PyArena
*arena
= PyArena_New();
1412 mod
= PyParser_ASTFromString(str
, filename
, start
, &flags
, arena
);
1414 PyArena_Free(arena
);
1417 st
= PySymtable_Build(mod
, filename
, 0);
1418 PyArena_Free(arena
);
1422 /* Preferred access to parser is through AST. */
1424 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1425 PyCompilerFlags
*flags
, PyArena
*arena
)
1428 PyCompilerFlags localflags
;
1430 int iflags
= PARSER_FLAGS(flags
);
1432 node
*n
= PyParser_ParseStringFlagsFilenameEx(s
, filename
,
1433 &_PyParser_Grammar
, start
, &err
,
1435 if (flags
== NULL
) {
1436 localflags
.cf_flags
= 0;
1437 flags
= &localflags
;
1440 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1441 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1452 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1453 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1457 PyCompilerFlags localflags
;
1459 int iflags
= PARSER_FLAGS(flags
);
1461 node
*n
= PyParser_ParseFileFlagsEx(fp
, filename
, &_PyParser_Grammar
,
1462 start
, ps1
, ps2
, &err
, &iflags
);
1463 if (flags
== NULL
) {
1464 localflags
.cf_flags
= 0;
1465 flags
= &localflags
;
1468 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1469 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1476 *errcode
= err
.error
;
1481 /* Simplified interface to parsefile -- return node or set exception */
1484 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1487 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1488 start
, NULL
, NULL
, &err
, flags
);
1495 /* Simplified interface to parsestring -- return node or set exception */
1498 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1501 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1502 start
, &err
, flags
);
1509 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1510 int start
, int flags
)
1513 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1514 &_PyParser_Grammar
, start
, &err
, flags
);
1521 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1523 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1526 /* May want to move a more generalized form of this to parsetok.c or
1527 even parser modules. */
1530 PyParser_SetError(perrdetail
*err
)
1535 /* Set the error appropriate to the given input error code (see errcode.h) */
1538 err_input(perrdetail
*err
)
1540 PyObject
*v
, *w
, *errtype
;
1543 errtype
= PyExc_SyntaxError
;
1544 switch (err
->error
) {
1546 errtype
= PyExc_IndentationError
;
1547 if (err
->expected
== INDENT
)
1548 msg
= "expected an indented block";
1549 else if (err
->token
== INDENT
)
1550 msg
= "unexpected indent";
1551 else if (err
->token
== DEDENT
)
1552 msg
= "unexpected unindent";
1554 errtype
= PyExc_SyntaxError
;
1555 msg
= "invalid syntax";
1559 msg
= "invalid token";
1562 msg
= "EOF while scanning triple-quoted string literal";
1565 msg
= "EOL while scanning string literal";
1568 if (!PyErr_Occurred())
1569 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1575 msg
= "unexpected EOF while parsing";
1578 errtype
= PyExc_TabError
;
1579 msg
= "inconsistent use of tabs and spaces in indentation";
1582 msg
= "expression too long";
1585 errtype
= PyExc_IndentationError
;
1586 msg
= "unindent does not match any outer indentation level";
1589 errtype
= PyExc_IndentationError
;
1590 msg
= "too many levels of indentation";
1593 PyObject
*type
, *value
, *tb
;
1594 PyErr_Fetch(&type
, &value
, &tb
);
1595 if (value
!= NULL
) {
1596 u
= PyObject_Str(value
);
1598 msg
= PyString_AsString(u
);
1602 msg
= "unknown decode error";
1609 msg
= "unexpected character after line continuation character";
1612 fprintf(stderr
, "error=%d\n", err
->error
);
1613 msg
= "unknown parsing error";
1616 v
= Py_BuildValue("(ziiz)", err
->filename
,
1617 err
->lineno
, err
->offset
, err
->text
);
1620 w
= Py_BuildValue("(sO)", msg
, v
);
1623 PyErr_SetObject(errtype
, w
);
1626 if (err
->text
!= NULL
) {
1627 PyObject_FREE(err
->text
);
1632 /* Print fatal error message and abort */
1635 Py_FatalError(const char *msg
)
1637 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1638 fflush(stderr
); /* it helps in Windows debug build */
1642 size_t len
= strlen(msg
);
1646 /* Convert the message to wchar_t. This uses a simple one-to-one
1647 conversion, assuming that the this error message actually uses ASCII
1648 only. If this ceases to be true, we will have to convert. */
1649 buffer
= alloca( (len
+1) * (sizeof *buffer
));
1650 for( i
=0; i
<=len
; ++i
)
1652 OutputDebugStringW(L
"Fatal Python error: ");
1653 OutputDebugStringW(buffer
);
1654 OutputDebugStringW(L
"\n");
1659 #endif /* MS_WINDOWS */
1663 /* Clean up and exit */
1666 #include "pythread.h"
1669 #define NEXITFUNCS 32
1670 static void (*exitfuncs
[NEXITFUNCS
])(void);
1671 static int nexitfuncs
= 0;
1673 int Py_AtExit(void (*func
)(void))
1675 if (nexitfuncs
>= NEXITFUNCS
)
1677 exitfuncs
[nexitfuncs
++] = func
;
1682 call_sys_exitfunc(void)
1684 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1688 Py_INCREF(exitfunc
);
1689 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1690 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1692 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1693 PySys_WriteStderr("Error in sys.exitfunc:\n");
1697 Py_DECREF(exitfunc
);
1705 call_ll_exitfuncs(void)
1707 while (nexitfuncs
> 0)
1708 (*exitfuncs
[--nexitfuncs
])();
1726 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1729 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1732 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1734 PyOS_InitInterrupts(); /* May imply initsignal() */
1739 * The file descriptor fd is considered ``interactive'' if either
1740 * a) isatty(fd) is TRUE, or
1741 * b) the -i flag was given, and the filename associated with
1742 * the descriptor is NULL or "<stdin>" or "???".
1745 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1747 if (isatty((int)fileno(fp
)))
1749 if (!Py_InteractiveFlag
)
1751 return (filename
== NULL
) ||
1752 (strcmp(filename
, "<stdin>") == 0) ||
1753 (strcmp(filename
, "???") == 0);
1757 #if defined(USE_STACKCHECK)
1758 #if defined(WIN32) && defined(_MSC_VER)
1760 /* Stack checking for Microsoft C */
1766 * Return non-zero when we run out of memory on the stack; zero otherwise.
1769 PyOS_CheckStack(void)
1772 /* alloca throws a stack overflow exception if there's
1773 not enough space left on the stack */
1774 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1776 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW
?
1777 EXCEPTION_EXECUTE_HANDLER
:
1778 EXCEPTION_CONTINUE_SEARCH
) {
1779 int errcode
= _resetstkoflw();
1782 Py_FatalError("Could not reset the stack!");
1788 #endif /* WIN32 && _MSC_VER */
1790 /* Alternate implementations can be added here... */
1792 #endif /* USE_STACKCHECK */
1795 /* Wrappers around sigaction() or signal(). */
1798 PyOS_getsig(int sig
)
1800 #ifdef HAVE_SIGACTION
1801 struct sigaction context
;
1802 if (sigaction(sig
, NULL
, &context
) == -1)
1804 return context
.sa_handler
;
1806 PyOS_sighandler_t handler
;
1807 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1808 #if defined(_MSC_VER) && _MSC_VER >= 1400
1810 /* Only these signals are valid */
1819 /* Don't call signal() with other values or it will assert */
1823 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1824 handler
= signal(sig
, SIG_IGN
);
1825 if (handler
!= SIG_ERR
)
1826 signal(sig
, handler
);
1832 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1834 #ifdef HAVE_SIGACTION
1835 struct sigaction context
, ocontext
;
1836 context
.sa_handler
= handler
;
1837 sigemptyset(&context
.sa_mask
);
1838 context
.sa_flags
= 0;
1839 if (sigaction(sig
, &context
, &ocontext
) == -1)
1841 return ocontext
.sa_handler
;
1843 PyOS_sighandler_t oldhandler
;
1844 oldhandler
= signal(sig
, handler
);
1845 #ifdef HAVE_SIGINTERRUPT
1846 siginterrupt(sig
, 1);
1852 /* Deprecated C API functions still provided for binary compatiblity */
1854 #undef PyParser_SimpleParseFile
1856 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1858 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1861 #undef PyParser_SimpleParseString
1863 PyParser_SimpleParseString(const char *str
, int start
)
1865 return PyParser_SimpleParseStringFlags(str
, start
, 0);
1868 #undef PyRun_AnyFile
1870 PyRun_AnyFile(FILE *fp
, const char *name
)
1872 return PyRun_AnyFileExFlags(fp
, name
, 0, NULL
);
1875 #undef PyRun_AnyFileEx
1877 PyRun_AnyFileEx(FILE *fp
, const char *name
, int closeit
)
1879 return PyRun_AnyFileExFlags(fp
, name
, closeit
, NULL
);
1882 #undef PyRun_AnyFileFlags
1884 PyRun_AnyFileFlags(FILE *fp
, const char *name
, PyCompilerFlags
*flags
)
1886 return PyRun_AnyFileExFlags(fp
, name
, 0, flags
);
1890 PyAPI_FUNC(PyObject
*)
1891 PyRun_File(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
)
1893 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, NULL
);
1897 PyAPI_FUNC(PyObject
*)
1898 PyRun_FileEx(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
, int c
)
1900 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, c
, NULL
);
1903 #undef PyRun_FileFlags
1904 PyAPI_FUNC(PyObject
*)
1905 PyRun_FileFlags(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
,
1906 PyCompilerFlags
*flags
)
1908 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, flags
);
1911 #undef PyRun_SimpleFile
1913 PyRun_SimpleFile(FILE *f
, const char *p
)
1915 return PyRun_SimpleFileExFlags(f
, p
, 0, NULL
);
1918 #undef PyRun_SimpleFileEx
1920 PyRun_SimpleFileEx(FILE *f
, const char *p
, int c
)
1922 return PyRun_SimpleFileExFlags(f
, p
, c
, NULL
);
1927 PyAPI_FUNC(PyObject
*)
1928 PyRun_String(const char *str
, int s
, PyObject
*g
, PyObject
*l
)
1930 return PyRun_StringFlags(str
, s
, g
, l
, NULL
);
1933 #undef PyRun_SimpleString
1935 PyRun_SimpleString(const char *s
)
1937 return PyRun_SimpleStringFlags(s
, NULL
);
1940 #undef Py_CompileString
1941 PyAPI_FUNC(PyObject
*)
1942 Py_CompileString(const char *str
, const char *p
, int s
)
1944 return Py_CompileStringFlags(str
, p
, s
, NULL
);
1947 #undef PyRun_InteractiveOne
1949 PyRun_InteractiveOne(FILE *f
, const char *p
)
1951 return PyRun_InteractiveOneFlags(f
, p
, NULL
);
1954 #undef PyRun_InteractiveLoop
1956 PyRun_InteractiveLoop(FILE *f
, const char *p
)
1958 return PyRun_InteractiveLoopFlags(f
, p
, NULL
);