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
;
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 ext
= filename
+ strlen(filename
) - 4;
916 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
917 /* Try to run a pyc file. First, re-open in binary */
920 if ((fp
= fopen(filename
, "rb")) == NULL
) {
921 fprintf(stderr
, "python: Can't reopen .pyc file\n");
925 /* Turn on optimization if a .pyo file is given */
926 if (strcmp(ext
, ".pyo") == 0)
928 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
930 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
943 if (set_file_name
&& PyDict_DelItemString(d
, "__file__"))
949 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
952 m
= PyImport_AddModule("__main__");
955 d
= PyModule_GetDict(m
);
956 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
968 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
969 int *lineno
, int *offset
, const char **text
)
974 /* old style errors */
975 if (PyTuple_Check(err
))
976 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
977 lineno
, offset
, text
);
979 /* new style errors. `err' is an instance */
981 if (! (v
= PyObject_GetAttrString(err
, "msg")))
985 if (!(v
= PyObject_GetAttrString(err
, "filename")))
989 else if (! (*filename
= PyString_AsString(v
)))
993 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
995 hold
= PyInt_AsLong(v
);
998 if (hold
< 0 && PyErr_Occurred())
1000 *lineno
= (int)hold
;
1002 if (!(v
= PyObject_GetAttrString(err
, "offset")))
1009 hold
= PyInt_AsLong(v
);
1012 if (hold
< 0 && PyErr_Occurred())
1014 *offset
= (int)hold
;
1017 if (!(v
= PyObject_GetAttrString(err
, "text")))
1021 else if (! (*text
= PyString_AsString(v
)))
1038 print_error_text(PyObject
*f
, int offset
, const char *text
)
1042 if (offset
> 0 && offset
== (int)strlen(text
))
1045 nl
= strchr(text
, '\n');
1046 if (nl
== NULL
|| nl
-text
>= offset
)
1048 offset
-= (int)(nl
+1-text
);
1051 while (*text
== ' ' || *text
== '\t') {
1056 PyFile_WriteString(" ", f
);
1057 PyFile_WriteString(text
, f
);
1058 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
1059 PyFile_WriteString("\n", f
);
1062 PyFile_WriteString(" ", f
);
1064 while (offset
> 0) {
1065 PyFile_WriteString(" ", f
);
1068 PyFile_WriteString("^\n", f
);
1072 handle_system_exit(void)
1074 PyObject
*exception
, *value
, *tb
;
1078 /* Don't exit if -i flag was given. This flag is set to 0
1079 * when entering interactive mode for inspecting. */
1082 PyErr_Fetch(&exception
, &value
, &tb
);
1086 if (value
== NULL
|| value
== Py_None
)
1088 if (PyExceptionInstance_Check(value
)) {
1089 /* The error code should be in the `code' attribute. */
1090 PyObject
*code
= PyObject_GetAttrString(value
, "code");
1094 if (value
== Py_None
)
1097 /* If we failed to dig out the 'code' attribute,
1098 just let the else clause below print the error. */
1100 if (PyInt_Check(value
))
1101 exitcode
= (int)PyInt_AsLong(value
);
1103 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1104 PySys_WriteStderr("\n");
1108 /* Restore and clear the exception info, in order to properly decref
1109 * the exception, value, and traceback. If we just exit instead,
1110 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1111 * some finalizers from running.
1113 PyErr_Restore(exception
, value
, tb
);
1120 PyErr_PrintEx(int set_sys_last_vars
)
1122 PyObject
*exception
, *v
, *tb
, *hook
;
1124 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1125 handle_system_exit();
1127 PyErr_Fetch(&exception
, &v
, &tb
);
1128 if (exception
== NULL
)
1130 PyErr_NormalizeException(&exception
, &v
, &tb
);
1131 if (exception
== NULL
)
1133 /* Now we know v != NULL too */
1134 if (set_sys_last_vars
) {
1135 PySys_SetObject("last_type", exception
);
1136 PySys_SetObject("last_value", v
);
1137 PySys_SetObject("last_traceback", tb
);
1139 hook
= PySys_GetObject("excepthook");
1141 PyObject
*args
= PyTuple_Pack(3,
1142 exception
, v
, tb
? tb
: Py_None
);
1143 PyObject
*result
= PyEval_CallObject(hook
, args
);
1144 if (result
== NULL
) {
1145 PyObject
*exception2
, *v2
, *tb2
;
1146 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1147 handle_system_exit();
1149 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1150 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1151 /* It should not be possible for exception2 or v2
1152 to be NULL. However PyErr_Display() can't
1153 tolerate NULLs, so just be safe. */
1154 if (exception2
== NULL
) {
1155 exception2
= Py_None
;
1156 Py_INCREF(exception2
);
1165 PySys_WriteStderr("Error in sys.excepthook:\n");
1166 PyErr_Display(exception2
, v2
, tb2
);
1167 PySys_WriteStderr("\nOriginal exception was:\n");
1168 PyErr_Display(exception
, v
, tb
);
1169 Py_DECREF(exception2
);
1176 PySys_WriteStderr("sys.excepthook is missing\n");
1177 PyErr_Display(exception
, v
, tb
);
1179 Py_XDECREF(exception
);
1185 PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1188 PyObject
*f
= PySys_GetObject("stderr");
1191 fprintf(stderr
, "lost sys.stderr\n");
1196 if (tb
&& tb
!= Py_None
)
1197 err
= PyTraceBack_Print(tb
, f
);
1199 PyObject_HasAttrString(value
, "print_file_and_line"))
1202 const char *filename
, *text
;
1204 if (!parse_syntax_error(value
, &message
, &filename
,
1205 &lineno
, &offset
, &text
))
1209 PyFile_WriteString(" File \"", f
);
1210 if (filename
== NULL
)
1211 PyFile_WriteString("<string>", f
);
1213 PyFile_WriteString(filename
, f
);
1214 PyFile_WriteString("\", line ", f
);
1215 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1216 PyFile_WriteString(buf
, f
);
1217 PyFile_WriteString("\n", f
);
1219 print_error_text(f
, offset
, text
);
1222 /* Can't be bothered to check all those
1223 PyFile_WriteString() calls */
1224 if (PyErr_Occurred())
1229 /* Don't do anything else */
1231 else if (PyExceptionClass_Check(exception
)) {
1232 PyObject
* moduleName
;
1233 char* className
= PyExceptionClass_Name(exception
);
1234 if (className
!= NULL
) {
1235 char *dot
= strrchr(className
, '.');
1240 moduleName
= PyObject_GetAttrString(exception
, "__module__");
1241 if (moduleName
== NULL
)
1242 err
= PyFile_WriteString("<unknown>", f
);
1244 char* modstr
= PyString_AsString(moduleName
);
1245 if (modstr
&& strcmp(modstr
, "exceptions"))
1247 err
= PyFile_WriteString(modstr
, f
);
1248 err
+= PyFile_WriteString(".", f
);
1250 Py_DECREF(moduleName
);
1253 if (className
== NULL
)
1254 err
= PyFile_WriteString("<unknown>", f
);
1256 err
= PyFile_WriteString(className
, f
);
1260 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1261 if (err
== 0 && (value
!= Py_None
)) {
1262 PyObject
*s
= PyObject_Str(value
);
1263 /* only print colon if the str() of the
1264 object is not the empty string
1268 else if (!PyString_Check(s
) ||
1269 PyString_GET_SIZE(s
) != 0)
1270 err
= PyFile_WriteString(": ", f
);
1272 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1275 /* try to write a newline in any case */
1276 err
+= PyFile_WriteString("\n", f
);
1279 /* If an error happened here, don't show it.
1280 XXX This is wrong, but too many callers rely on this behavior. */
1286 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1287 PyObject
*locals
, PyCompilerFlags
*flags
)
1289 PyObject
*ret
= NULL
;
1291 PyArena
*arena
= PyArena_New();
1295 mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
, arena
);
1297 ret
= run_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1298 PyArena_Free(arena
);
1303 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1304 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1308 PyArena
*arena
= PyArena_New();
1312 mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1313 flags
, NULL
, arena
);
1317 PyArena_Free(arena
);
1320 ret
= run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1321 PyArena_Free(arena
);
1326 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1327 PyCompilerFlags
*flags
, PyArena
*arena
)
1331 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1334 v
= PyEval_EvalCode(co
, globals
, locals
);
1340 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1341 PyObject
*locals
, PyCompilerFlags
*flags
)
1346 long PyImport_GetMagicNumber(void);
1348 magic
= PyMarshal_ReadLongFromFile(fp
);
1349 if (magic
!= PyImport_GetMagicNumber()) {
1350 PyErr_SetString(PyExc_RuntimeError
,
1351 "Bad magic number in .pyc file");
1354 (void) PyMarshal_ReadLongFromFile(fp
);
1355 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1357 if (v
== NULL
|| !PyCode_Check(v
)) {
1359 PyErr_SetString(PyExc_RuntimeError
,
1360 "Bad code object in .pyc file");
1363 co
= (PyCodeObject
*)v
;
1364 v
= PyEval_EvalCode(co
, globals
, locals
);
1366 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1372 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1373 PyCompilerFlags
*flags
)
1377 PyArena
*arena
= PyArena_New();
1381 mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1383 PyArena_Free(arena
);
1386 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1387 PyObject
*result
= PyAST_mod2obj(mod
);
1388 PyArena_Free(arena
);
1391 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1392 PyArena_Free(arena
);
1393 return (PyObject
*)co
;
1397 Py_SymtableString(const char *str
, const char *filename
, int start
)
1399 struct symtable
*st
;
1401 PyCompilerFlags flags
;
1402 PyArena
*arena
= PyArena_New();
1408 mod
= PyParser_ASTFromString(str
, filename
, start
, &flags
, arena
);
1410 PyArena_Free(arena
);
1413 st
= PySymtable_Build(mod
, filename
, 0);
1414 PyArena_Free(arena
);
1418 /* Preferred access to parser is through AST. */
1420 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1421 PyCompilerFlags
*flags
, PyArena
*arena
)
1424 PyCompilerFlags localflags
;
1426 int iflags
= PARSER_FLAGS(flags
);
1428 node
*n
= PyParser_ParseStringFlagsFilenameEx(s
, filename
,
1429 &_PyParser_Grammar
, start
, &err
,
1431 if (flags
== NULL
) {
1432 localflags
.cf_flags
= 0;
1433 flags
= &localflags
;
1436 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1437 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1448 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1449 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1453 PyCompilerFlags localflags
;
1455 int iflags
= PARSER_FLAGS(flags
);
1457 node
*n
= PyParser_ParseFileFlagsEx(fp
, filename
, &_PyParser_Grammar
,
1458 start
, ps1
, ps2
, &err
, &iflags
);
1459 if (flags
== NULL
) {
1460 localflags
.cf_flags
= 0;
1461 flags
= &localflags
;
1464 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1465 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1472 *errcode
= err
.error
;
1477 /* Simplified interface to parsefile -- return node or set exception */
1480 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1483 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1484 start
, NULL
, NULL
, &err
, flags
);
1491 /* Simplified interface to parsestring -- return node or set exception */
1494 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1497 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1498 start
, &err
, flags
);
1505 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1506 int start
, int flags
)
1509 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1510 &_PyParser_Grammar
, start
, &err
, flags
);
1517 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1519 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1522 /* May want to move a more generalized form of this to parsetok.c or
1523 even parser modules. */
1526 PyParser_SetError(perrdetail
*err
)
1531 /* Set the error appropriate to the given input error code (see errcode.h) */
1534 err_input(perrdetail
*err
)
1536 PyObject
*v
, *w
, *errtype
;
1539 errtype
= PyExc_SyntaxError
;
1540 switch (err
->error
) {
1542 errtype
= PyExc_IndentationError
;
1543 if (err
->expected
== INDENT
)
1544 msg
= "expected an indented block";
1545 else if (err
->token
== INDENT
)
1546 msg
= "unexpected indent";
1547 else if (err
->token
== DEDENT
)
1548 msg
= "unexpected unindent";
1550 errtype
= PyExc_SyntaxError
;
1551 msg
= "invalid syntax";
1555 msg
= "invalid token";
1558 msg
= "EOF while scanning triple-quoted string literal";
1561 msg
= "EOL while scanning string literal";
1564 if (!PyErr_Occurred())
1565 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1571 msg
= "unexpected EOF while parsing";
1574 errtype
= PyExc_TabError
;
1575 msg
= "inconsistent use of tabs and spaces in indentation";
1578 msg
= "expression too long";
1581 errtype
= PyExc_IndentationError
;
1582 msg
= "unindent does not match any outer indentation level";
1585 errtype
= PyExc_IndentationError
;
1586 msg
= "too many levels of indentation";
1589 PyObject
*type
, *value
, *tb
;
1590 PyErr_Fetch(&type
, &value
, &tb
);
1591 if (value
!= NULL
) {
1592 u
= PyObject_Str(value
);
1594 msg
= PyString_AsString(u
);
1598 msg
= "unknown decode error";
1605 msg
= "unexpected character after line continuation character";
1608 fprintf(stderr
, "error=%d\n", err
->error
);
1609 msg
= "unknown parsing error";
1612 v
= Py_BuildValue("(ziiz)", err
->filename
,
1613 err
->lineno
, err
->offset
, err
->text
);
1616 w
= Py_BuildValue("(sO)", msg
, v
);
1619 PyErr_SetObject(errtype
, w
);
1622 if (err
->text
!= NULL
) {
1623 PyObject_FREE(err
->text
);
1628 /* Print fatal error message and abort */
1631 Py_FatalError(const char *msg
)
1633 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1636 size_t len
= strlen(msg
);
1640 /* Convert the message to wchar_t. This uses a simple one-to-one
1641 conversion, assuming that the this error message actually uses ASCII
1642 only. If this ceases to be true, we will have to convert. */
1643 buffer
= alloca( (len
+1) * (sizeof *buffer
));
1644 for( i
=0; i
<=len
; ++i
)
1646 OutputDebugStringW(L
"Fatal Python error: ");
1647 OutputDebugStringW(buffer
);
1648 OutputDebugStringW(L
"\n");
1653 #endif /* MS_WINDOWS */
1657 /* Clean up and exit */
1660 #include "pythread.h"
1663 #define NEXITFUNCS 32
1664 static void (*exitfuncs
[NEXITFUNCS
])(void);
1665 static int nexitfuncs
= 0;
1667 int Py_AtExit(void (*func
)(void))
1669 if (nexitfuncs
>= NEXITFUNCS
)
1671 exitfuncs
[nexitfuncs
++] = func
;
1676 call_sys_exitfunc(void)
1678 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1682 Py_INCREF(exitfunc
);
1683 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1684 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1686 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1687 PySys_WriteStderr("Error in sys.exitfunc:\n");
1691 Py_DECREF(exitfunc
);
1699 call_ll_exitfuncs(void)
1701 while (nexitfuncs
> 0)
1702 (*exitfuncs
[--nexitfuncs
])();
1720 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1723 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1726 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1728 PyOS_InitInterrupts(); /* May imply initsignal() */
1733 * The file descriptor fd is considered ``interactive'' if either
1734 * a) isatty(fd) is TRUE, or
1735 * b) the -i flag was given, and the filename associated with
1736 * the descriptor is NULL or "<stdin>" or "???".
1739 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1741 if (isatty((int)fileno(fp
)))
1743 if (!Py_InteractiveFlag
)
1745 return (filename
== NULL
) ||
1746 (strcmp(filename
, "<stdin>") == 0) ||
1747 (strcmp(filename
, "???") == 0);
1751 #if defined(USE_STACKCHECK)
1752 #if defined(WIN32) && defined(_MSC_VER)
1754 /* Stack checking for Microsoft C */
1760 * Return non-zero when we run out of memory on the stack; zero otherwise.
1763 PyOS_CheckStack(void)
1766 /* alloca throws a stack overflow exception if there's
1767 not enough space left on the stack */
1768 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1770 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW
?
1771 EXCEPTION_EXECUTE_HANDLER
:
1772 EXCEPTION_CONTINUE_SEARCH
) {
1773 int errcode
= _resetstkoflw();
1776 Py_FatalError("Could not reset the stack!");
1782 #endif /* WIN32 && _MSC_VER */
1784 /* Alternate implementations can be added here... */
1786 #endif /* USE_STACKCHECK */
1789 /* Wrappers around sigaction() or signal(). */
1792 PyOS_getsig(int sig
)
1794 #ifdef HAVE_SIGACTION
1795 struct sigaction context
;
1796 if (sigaction(sig
, NULL
, &context
) == -1)
1798 return context
.sa_handler
;
1800 PyOS_sighandler_t handler
;
1801 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1802 #if defined(_MSC_VER) && _MSC_VER >= 1400
1804 /* Only these signals are valid */
1813 /* Don't call signal() with other values or it will assert */
1817 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1818 handler
= signal(sig
, SIG_IGN
);
1819 if (handler
!= SIG_ERR
)
1820 signal(sig
, handler
);
1826 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1828 #ifdef HAVE_SIGACTION
1829 struct sigaction context
, ocontext
;
1830 context
.sa_handler
= handler
;
1831 sigemptyset(&context
.sa_mask
);
1832 context
.sa_flags
= 0;
1833 if (sigaction(sig
, &context
, &ocontext
) == -1)
1835 return ocontext
.sa_handler
;
1837 PyOS_sighandler_t oldhandler
;
1838 oldhandler
= signal(sig
, handler
);
1839 #ifdef HAVE_SIGINTERRUPT
1840 siginterrupt(sig
, 1);
1846 /* Deprecated C API functions still provided for binary compatiblity */
1848 #undef PyParser_SimpleParseFile
1850 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1852 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1855 #undef PyParser_SimpleParseString
1857 PyParser_SimpleParseString(const char *str
, int start
)
1859 return PyParser_SimpleParseStringFlags(str
, start
, 0);
1862 #undef PyRun_AnyFile
1864 PyRun_AnyFile(FILE *fp
, const char *name
)
1866 return PyRun_AnyFileExFlags(fp
, name
, 0, NULL
);
1869 #undef PyRun_AnyFileEx
1871 PyRun_AnyFileEx(FILE *fp
, const char *name
, int closeit
)
1873 return PyRun_AnyFileExFlags(fp
, name
, closeit
, NULL
);
1876 #undef PyRun_AnyFileFlags
1878 PyRun_AnyFileFlags(FILE *fp
, const char *name
, PyCompilerFlags
*flags
)
1880 return PyRun_AnyFileExFlags(fp
, name
, 0, flags
);
1884 PyAPI_FUNC(PyObject
*)
1885 PyRun_File(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
)
1887 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, NULL
);
1891 PyAPI_FUNC(PyObject
*)
1892 PyRun_FileEx(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
, int c
)
1894 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, c
, NULL
);
1897 #undef PyRun_FileFlags
1898 PyAPI_FUNC(PyObject
*)
1899 PyRun_FileFlags(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
,
1900 PyCompilerFlags
*flags
)
1902 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, flags
);
1905 #undef PyRun_SimpleFile
1907 PyRun_SimpleFile(FILE *f
, const char *p
)
1909 return PyRun_SimpleFileExFlags(f
, p
, 0, NULL
);
1912 #undef PyRun_SimpleFileEx
1914 PyRun_SimpleFileEx(FILE *f
, const char *p
, int c
)
1916 return PyRun_SimpleFileExFlags(f
, p
, c
, NULL
);
1921 PyAPI_FUNC(PyObject
*)
1922 PyRun_String(const char *str
, int s
, PyObject
*g
, PyObject
*l
)
1924 return PyRun_StringFlags(str
, s
, g
, l
, NULL
);
1927 #undef PyRun_SimpleString
1929 PyRun_SimpleString(const char *s
)
1931 return PyRun_SimpleStringFlags(s
, NULL
);
1934 #undef Py_CompileString
1935 PyAPI_FUNC(PyObject
*)
1936 Py_CompileString(const char *str
, const char *p
, int s
)
1938 return Py_CompileStringFlags(str
, p
, s
, NULL
);
1941 #undef PyRun_InteractiveOne
1943 PyRun_InteractiveOne(FILE *f
, const char *p
)
1945 return PyRun_InteractiveOneFlags(f
, p
, NULL
);
1948 #undef PyRun_InteractiveLoop
1950 PyRun_InteractiveLoop(FILE *f
, const char *p
)
1952 return PyRun_InteractiveLoopFlags(f
, p
, NULL
);