2 /* Python interpreter top-level routines, including init/exit */
6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */
25 #ifdef HAVE_LANGINFO_H
36 #define PRINT_TOTAL_REFS()
37 #else /* Py_REF_DEBUG */
38 #define PRINT_TOTAL_REFS() fprintf(stderr, \
39 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
47 extern char *Py_GetPath(void);
49 extern grammar _PyParser_Grammar
; /* From graminit.c */
52 static void initmain(void);
53 static void initsite(void);
54 static PyObject
*run_mod(mod_ty
, const char *, PyObject
*, PyObject
*,
55 PyCompilerFlags
*, PyArena
*);
56 static PyObject
*run_pyc_file(FILE *, const char *, PyObject
*, PyObject
*,
58 static void err_input(perrdetail
*);
59 static void initsigs(void);
60 static void call_sys_exitfunc(void);
61 static void call_ll_exitfuncs(void);
62 extern void _PyUnicode_Init(void);
63 extern void _PyUnicode_Fini(void);
66 extern void _PyGILState_Init(PyInterpreterState
*, PyThreadState
*);
67 extern void _PyGILState_Fini(void);
68 #endif /* WITH_THREAD */
70 int Py_DebugFlag
; /* Needed by parser.c */
71 int Py_VerboseFlag
; /* Needed by import.c */
72 int Py_InteractiveFlag
; /* Needed by Py_FdIsInteractive() below */
73 int Py_InspectFlag
; /* Needed to determine whether to exit at SystemError */
74 int Py_NoSiteFlag
; /* Suppress 'import site' */
75 int Py_BytesWarningFlag
; /* Warn on str(bytes) and str(buffer) */
76 int Py_DontWriteBytecodeFlag
; /* Suppress writing bytecode files (*.py[co]) */
77 int Py_UseClassExceptionsFlag
= 1; /* Needed by bltinmodule.c: deprecated */
78 int Py_FrozenFlag
; /* Needed by getpath.c */
79 int Py_UnicodeFlag
= 0; /* Needed by compile.c */
80 int Py_IgnoreEnvironmentFlag
; /* e.g. PYTHONPATH, PYTHONHOME */
81 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
83 true divisions (which they will be in 2.3). */
85 int Py_NoUserSiteDirectory
= 0; /* for -s and site.py */
87 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
88 since _warnings is builtin. This API should not be used. */
90 PyModule_GetWarningsModule(void)
92 return PyImport_ImportModule("warnings");
95 static int initialized
= 0;
97 /* API to access the initialized flag -- useful for esoteric use */
100 Py_IsInitialized(void)
105 /* Global initializations. Can be undone by Py_Finalize(). Don't
106 call this twice without an intervening Py_Finalize() call. When
107 initializations fail, a fatal error is issued and the function does
108 not return. On return, the first thread and interpreter state have
111 Locking: you must hold the interpreter lock while calling this.
112 (If the lock has not yet been initialized, that's equivalent to
113 having the lock, but you cannot use multiple threads.)
118 add_flag(int flag
, const char *envs
)
120 int env
= atoi(envs
);
129 Py_InitializeEx(int install_sigs
)
131 PyInterpreterState
*interp
;
132 PyThreadState
*tstate
;
133 PyObject
*bimod
, *sysmod
;
135 char *icodeset
; /* On Windows, input codeset may theoretically
136 differ from output codeset. */
137 char *codeset
= NULL
;
139 int free_codeset
= 0;
141 PyObject
*sys_stream
, *sys_isatty
;
142 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
143 char *saved_locale
, *loc_codeset
;
149 extern void _Py_ReadyTypes(void);
155 if ((p
= Py_GETENV("PYTHONDEBUG")) && *p
!= '\0')
156 Py_DebugFlag
= add_flag(Py_DebugFlag
, p
);
157 if ((p
= Py_GETENV("PYTHONVERBOSE")) && *p
!= '\0')
158 Py_VerboseFlag
= add_flag(Py_VerboseFlag
, p
);
159 if ((p
= Py_GETENV("PYTHONOPTIMIZE")) && *p
!= '\0')
160 Py_OptimizeFlag
= add_flag(Py_OptimizeFlag
, p
);
161 if ((p
= Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p
!= '\0')
162 Py_DontWriteBytecodeFlag
= add_flag(Py_DontWriteBytecodeFlag
, p
);
164 interp
= PyInterpreterState_New();
166 Py_FatalError("Py_Initialize: can't make first interpreter");
168 tstate
= PyThreadState_New(interp
);
170 Py_FatalError("Py_Initialize: can't make first thread");
171 (void) PyThreadState_Swap(tstate
);
175 if (!_PyFrame_Init())
176 Py_FatalError("Py_Initialize: can't init frames");
179 Py_FatalError("Py_Initialize: can't init ints");
181 if (!PyByteArray_Init())
182 Py_FatalError("Py_Initialize: can't init bytearray");
186 interp
->modules
= PyDict_New();
187 if (interp
->modules
== NULL
)
188 Py_FatalError("Py_Initialize: can't make modules dictionary");
189 interp
->modules_reloading
= PyDict_New();
190 if (interp
->modules_reloading
== NULL
)
191 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
193 #ifdef Py_USING_UNICODE
194 /* Init Unicode implementation; relies on the codec registry */
198 bimod
= _PyBuiltin_Init();
200 Py_FatalError("Py_Initialize: can't initialize __builtin__");
201 interp
->builtins
= PyModule_GetDict(bimod
);
202 if (interp
->builtins
== NULL
)
203 Py_FatalError("Py_Initialize: can't initialize builtins dict");
204 Py_INCREF(interp
->builtins
);
206 sysmod
= _PySys_Init();
208 Py_FatalError("Py_Initialize: can't initialize sys");
209 interp
->sysdict
= PyModule_GetDict(sysmod
);
210 if (interp
->sysdict
== NULL
)
211 Py_FatalError("Py_Initialize: can't initialize sys dict");
212 Py_INCREF(interp
->sysdict
);
213 _PyImport_FixupExtension("sys", "sys");
214 PySys_SetPath(Py_GetPath());
215 PyDict_SetItemString(interp
->sysdict
, "modules",
220 /* initialize builtin exceptions */
222 _PyImport_FixupExtension("exceptions", "exceptions");
224 /* phase 2 of builtins */
225 _PyImport_FixupExtension("__builtin__", "__builtin__");
227 _PyImportHooks_Init();
230 initsigs(); /* Signal handling stuff, including initintr() */
232 /* Initialize warnings. */
234 if (PySys_HasWarnOptions()) {
235 PyObject
*warnings_module
= PyImport_ImportModule("warnings");
236 if (!warnings_module
)
238 Py_XDECREF(warnings_module
);
241 initmain(); /* Module __main__ */
243 initsite(); /* Module site */
245 /* auto-thread-state API, if available */
247 _PyGILState_Init(interp
, tstate
);
248 #endif /* WITH_THREAD */
250 if ((p
= Py_GETENV("PYTHONIOENCODING")) && *p
!= '\0') {
251 p
= icodeset
= codeset
= strdup(p
);
253 errors
= strchr(p
, ':');
261 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
262 /* On Unix, set the file system encoding according to the
263 user's preference, if the CODESET names a well-known
264 Python codec, and Py_FileSystemDefaultEncoding isn't
265 initialized by other means. Also set the encoding of
266 stdin and stdout if these are terminals, unless overridden. */
268 if (!overridden
|| !Py_FileSystemDefaultEncoding
) {
269 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
270 setlocale(LC_CTYPE
, "");
271 loc_codeset
= nl_langinfo(CODESET
);
272 if (loc_codeset
&& *loc_codeset
) {
273 PyObject
*enc
= PyCodec_Encoder(loc_codeset
);
275 loc_codeset
= strdup(loc_codeset
);
283 setlocale(LC_CTYPE
, saved_locale
);
287 codeset
= icodeset
= loc_codeset
;
291 /* Initialize Py_FileSystemDefaultEncoding from
292 locale even if PYTHONIOENCODING is set. */
293 if (!Py_FileSystemDefaultEncoding
) {
294 Py_FileSystemDefaultEncoding
= loc_codeset
;
305 sprintf(ibuf
, "cp%d", GetConsoleCP());
306 sprintf(buf
, "cp%d", GetConsoleOutputCP());
311 sys_stream
= PySys_GetObject("stdin");
312 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
316 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
317 PyFile_Check(sys_stream
)) {
318 if (!PyFile_SetEncodingAndErrors(sys_stream
, icodeset
, errors
))
319 Py_FatalError("Cannot set codeset of stdin");
321 Py_XDECREF(sys_isatty
);
323 sys_stream
= PySys_GetObject("stdout");
324 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
328 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
329 PyFile_Check(sys_stream
)) {
330 if (!PyFile_SetEncodingAndErrors(sys_stream
, codeset
, errors
))
331 Py_FatalError("Cannot set codeset of stdout");
333 Py_XDECREF(sys_isatty
);
335 sys_stream
= PySys_GetObject("stderr");
336 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
340 (sys_isatty
&& PyObject_IsTrue(sys_isatty
))) &&
341 PyFile_Check(sys_stream
)) {
342 if (!PyFile_SetEncodingAndErrors(sys_stream
, codeset
, errors
))
343 Py_FatalError("Cannot set codeset of stderr");
345 Py_XDECREF(sys_isatty
);
360 extern void dump_counts(FILE*);
363 /* Undo the effect of Py_Initialize().
365 Beware: if multiple interpreter and/or thread states exist, these
366 are not wiped out; only the current thread and interpreter state
367 are deleted. But since everything else is deleted, those other
368 interpreter and thread states should no longer be used.
370 (XXX We should do better, e.g. wipe out all interpreters and
380 PyInterpreterState
*interp
;
381 PyThreadState
*tstate
;
386 /* The interpreter is still entirely intact at this point, and the
387 * exit funcs may be relying on that. In particular, if some thread
388 * or exit func is still waiting to do an import, the import machinery
389 * expects Py_IsInitialized() to return true. So don't say the
390 * interpreter is uninitialized until after the exit funcs have run.
391 * Note that Threading.py uses an exit func to do a join on all the
392 * threads created thru it, so this also protects pending imports in
393 * the threads created via Threading.
398 /* Get current thread state and interpreter pointer */
399 tstate
= PyThreadState_GET();
400 interp
= tstate
->interp
;
402 /* Disable signal handling */
403 PyOS_FiniInterrupts();
405 /* Clear type lookup cache */
408 /* Collect garbage. This may call finalizers; it's nice to call these
409 * before all modules are destroyed.
410 * XXX If a __del__ or weakref callback is triggered here, and tries to
411 * XXX import a module, bad things can happen, because Python no
412 * XXX longer believes it's initialized.
413 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
414 * XXX is easy to provoke that way. I've also seen, e.g.,
415 * XXX Exception exceptions.ImportError: 'No module named sha'
416 * XXX in <function callback at 0x008F5718> ignored
417 * XXX but I'm unclear on exactly how that one happens. In any case,
418 * XXX I haven't seen a real-life report of either of these.
422 /* With COUNT_ALLOCS, it helps to run GC multiple times:
423 each collection might release some types from the type
424 list, so they become garbage. */
425 while (PyGC_Collect() > 0)
429 /* Destroy all modules */
432 /* Collect final garbage. This disposes of cycles created by
433 * new-style class definitions, for example.
434 * XXX This is disabled because it caused too many problems. If
435 * XXX a __del__ or weakref callback triggers here, Python code has
436 * XXX a hard time running, because even the sys module has been
437 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
438 * XXX One symptom is a sequence of information-free messages
439 * XXX coming from threads (if a __del__ or callback is invoked,
440 * XXX other threads can execute too, and any exception they encounter
441 * XXX triggers a comedy of errors as subsystem after subsystem
442 * XXX fails to find what it *expects* to find in sys to help report
443 * XXX the exception and consequent unexpected failures). I've also
444 * XXX seen segfaults then, after adding print statements to the
445 * XXX Python code getting called.
451 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
454 /* Debugging stuff */
462 /* Display all objects still alive -- this can invoke arbitrary
463 * __repr__ overrides, so requires a mostly-intact interpreter.
464 * Alas, a lot of stuff may still be alive now that will be cleaned
467 if (Py_GETENV("PYTHONDUMPREFS"))
468 _Py_PrintReferences(stderr
);
469 #endif /* Py_TRACE_REFS */
471 /* Clear interpreter state */
472 PyInterpreterState_Clear(interp
);
474 /* Now we decref the exception classes. After this point nothing
475 can raise an exception. That's okay, because each Fini() method
476 below has been checked to make sure no exceptions are ever
482 /* Cleanup auto-thread-state */
485 #endif /* WITH_THREAD */
487 /* Delete current thread */
488 PyThreadState_Swap(NULL
);
489 PyInterpreterState_Delete(interp
);
491 /* Sundry finalizers */
504 #ifdef Py_USING_UNICODE
505 /* Cleanup Unicode implementation */
509 /* XXX Still allocated:
510 - various static ad-hoc pointers to interned strings
511 - int and float free list blocks
512 - whatever various modules and libraries allocate
515 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
518 /* Display addresses (& refcnts) of all objects still alive.
519 * An address can be used to find the repr of the object, printed
520 * above by _Py_PrintReferences.
522 if (Py_GETENV("PYTHONDUMPREFS"))
523 _Py_PrintReferenceAddresses(stderr
);
524 #endif /* Py_TRACE_REFS */
525 #ifdef PYMALLOC_DEBUG
526 if (Py_GETENV("PYTHONMALLOCSTATS"))
527 _PyObject_DebugMallocStats();
533 /* Create and initialize a new interpreter and thread, and return the
534 new thread. This requires that Py_Initialize() has been called
537 Unsuccessful initialization yields a NULL pointer. Note that *no*
538 exception information is available even in this case -- the
539 exception information is held in the thread, and there is no
547 Py_NewInterpreter(void)
549 PyInterpreterState
*interp
;
550 PyThreadState
*tstate
, *save_tstate
;
551 PyObject
*bimod
, *sysmod
;
554 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
556 interp
= PyInterpreterState_New();
560 tstate
= PyThreadState_New(interp
);
561 if (tstate
== NULL
) {
562 PyInterpreterState_Delete(interp
);
566 save_tstate
= PyThreadState_Swap(tstate
);
568 /* XXX The following is lax in error checking */
570 interp
->modules
= PyDict_New();
571 interp
->modules_reloading
= PyDict_New();
573 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
575 interp
->builtins
= PyModule_GetDict(bimod
);
576 if (interp
->builtins
== NULL
)
578 Py_INCREF(interp
->builtins
);
580 sysmod
= _PyImport_FindExtension("sys", "sys");
581 if (bimod
!= NULL
&& sysmod
!= NULL
) {
582 interp
->sysdict
= PyModule_GetDict(sysmod
);
583 if (interp
->sysdict
== NULL
)
585 Py_INCREF(interp
->sysdict
);
586 PySys_SetPath(Py_GetPath());
587 PyDict_SetItemString(interp
->sysdict
, "modules",
589 _PyImportHooks_Init();
595 if (!PyErr_Occurred())
599 /* Oops, it didn't work. Undo it all. */
602 PyThreadState_Clear(tstate
);
603 PyThreadState_Swap(save_tstate
);
604 PyThreadState_Delete(tstate
);
605 PyInterpreterState_Delete(interp
);
610 /* Delete an interpreter and its last thread. This requires that the
611 given thread state is current, that the thread has no remaining
612 frames, and that it is its interpreter's only remaining thread.
613 It is a fatal error to violate these constraints.
615 (Py_Finalize() doesn't have these constraints -- it zaps
616 everything, regardless.)
623 Py_EndInterpreter(PyThreadState
*tstate
)
625 PyInterpreterState
*interp
= tstate
->interp
;
627 if (tstate
!= PyThreadState_GET())
628 Py_FatalError("Py_EndInterpreter: thread is not current");
629 if (tstate
->frame
!= NULL
)
630 Py_FatalError("Py_EndInterpreter: thread still has a frame");
631 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
632 Py_FatalError("Py_EndInterpreter: not the last thread");
635 PyInterpreterState_Clear(interp
);
636 PyThreadState_Swap(NULL
);
637 PyInterpreterState_Delete(interp
);
640 static char *progname
= "python";
643 Py_SetProgramName(char *pn
)
650 Py_GetProgramName(void)
655 static char *default_home
= NULL
;
658 Py_SetPythonHome(char *home
)
664 Py_GetPythonHome(void)
666 char *home
= default_home
;
667 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
668 home
= Py_GETENV("PYTHONHOME");
672 /* Create __main__ module */
678 m
= PyImport_AddModule("__main__");
680 Py_FatalError("can't create __main__ module");
681 d
= PyModule_GetDict(m
);
682 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
683 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
685 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
686 Py_FatalError("can't add __builtins__ to __main__");
691 /* Import the site module (not into __main__ though) */
697 m
= PyImport_ImportModule("site");
699 f
= PySys_GetObject("stderr");
700 if (Py_VerboseFlag
) {
702 "'import site' failed; traceback:\n", f
);
707 "'import site' failed; use -v for traceback\n", f
);
716 /* Parse input from a file and execute it */
719 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
720 PyCompilerFlags
*flags
)
722 if (filename
== NULL
)
724 if (Py_FdIsInteractive(fp
, filename
)) {
725 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
731 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
735 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
739 PyCompilerFlags local_flags
;
742 flags
= &local_flags
;
743 local_flags
.cf_flags
= 0;
745 v
= PySys_GetObject("ps1");
747 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
750 v
= PySys_GetObject("ps2");
752 PySys_SetObject("ps2", v
= PyString_FromString("... "));
756 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
768 /* compute parser flags based on compiler flags */
769 #define PARSER_FLAGS(flags) \
770 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
771 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
774 /* Keep an example of flags with future keyword support. */
775 #define PARSER_FLAGS(flags) \
776 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
777 PyPARSE_DONT_IMPLY_DEDENT : 0) \
778 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
779 PyPARSE_PRINT_IS_FUNCTION : 0) \
780 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
781 PyPARSE_UNICODE_LITERALS : 0) \
786 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
788 PyObject
*m
, *d
, *v
, *w
;
791 char *ps1
= "", *ps2
= "";
794 v
= PySys_GetObject("ps1");
799 else if (PyString_Check(v
))
800 ps1
= PyString_AsString(v
);
802 w
= PySys_GetObject("ps2");
807 else if (PyString_Check(w
))
808 ps2
= PyString_AsString(w
);
810 arena
= PyArena_New();
816 mod
= PyParser_ASTFromFile(fp
, filename
,
817 Py_single_input
, ps1
, ps2
,
818 flags
, &errcode
, arena
);
823 if (errcode
== E_EOF
) {
830 m
= PyImport_AddModule("__main__");
835 d
= PyModule_GetDict(m
);
836 v
= run_mod(mod
, filename
, d
, d
, flags
, arena
);
848 /* Check whether a file maybe a pyc file: Look at the extension,
849 the file type, and, if we may close it, at the first few bytes. */
852 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
854 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
857 /* Only look into the file if we are allowed to close it, since
858 it then should also be seekable. */
860 /* Read only two bytes of the magic. If the file was opened in
861 text mode, the bytes 3 and 4 of the magic (\r\n) might not
862 be read as they are on disk. */
863 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
864 unsigned char buf
[2];
865 /* Mess: In case of -x, the stream is NOT at its start now,
866 and ungetc() was used to push back the first newline,
867 which makes the current stream position formally undefined,
868 and a x-platform nightmare.
869 Unfortunately, we have no direct way to know whether -x
870 was specified. So we use a terrible hack: if the current
871 stream position is not 0, we assume -x was specified, and
872 give up. Bug 132850 on SourceForge spells out the
873 hopelessness of trying anything else (fseek and ftell
874 don't work predictably x-platform for text-mode files).
877 if (ftell(fp
) == 0) {
878 if (fread(buf
, 1, 2, fp
) == 2 &&
879 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
889 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
890 PyCompilerFlags
*flags
)
894 int set_file_name
= 0, ret
;
896 m
= PyImport_AddModule("__main__");
899 d
= PyModule_GetDict(m
);
900 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
901 PyObject
*f
= PyString_FromString(filename
);
904 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
911 ext
= filename
+ strlen(filename
) - 4;
912 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
913 /* Try to run a pyc file. First, re-open in binary */
916 if ((fp
= fopen(filename
, "rb")) == NULL
) {
917 fprintf(stderr
, "python: Can't reopen .pyc file\n");
921 /* Turn on optimization if a .pyo file is given */
922 if (strcmp(ext
, ".pyo") == 0)
924 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
926 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
939 if (set_file_name
&& PyDict_DelItemString(d
, "__file__"))
945 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
948 m
= PyImport_AddModule("__main__");
951 d
= PyModule_GetDict(m
);
952 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
964 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
965 int *lineno
, int *offset
, const char **text
)
970 /* old style errors */
971 if (PyTuple_Check(err
))
972 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
973 lineno
, offset
, text
);
975 /* new style errors. `err' is an instance */
977 if (! (v
= PyObject_GetAttrString(err
, "msg")))
981 if (!(v
= PyObject_GetAttrString(err
, "filename")))
985 else if (! (*filename
= PyString_AsString(v
)))
989 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
991 hold
= PyInt_AsLong(v
);
994 if (hold
< 0 && PyErr_Occurred())
998 if (!(v
= PyObject_GetAttrString(err
, "offset")))
1005 hold
= PyInt_AsLong(v
);
1008 if (hold
< 0 && PyErr_Occurred())
1010 *offset
= (int)hold
;
1013 if (!(v
= PyObject_GetAttrString(err
, "text")))
1017 else if (! (*text
= PyString_AsString(v
)))
1034 print_error_text(PyObject
*f
, int offset
, const char *text
)
1038 if (offset
> 0 && offset
== (int)strlen(text
))
1041 nl
= strchr(text
, '\n');
1042 if (nl
== NULL
|| nl
-text
>= offset
)
1044 offset
-= (int)(nl
+1-text
);
1047 while (*text
== ' ' || *text
== '\t') {
1052 PyFile_WriteString(" ", f
);
1053 PyFile_WriteString(text
, f
);
1054 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
1055 PyFile_WriteString("\n", f
);
1058 PyFile_WriteString(" ", f
);
1060 while (offset
> 0) {
1061 PyFile_WriteString(" ", f
);
1064 PyFile_WriteString("^\n", f
);
1068 handle_system_exit(void)
1070 PyObject
*exception
, *value
, *tb
;
1074 /* Don't exit if -i flag was given. This flag is set to 0
1075 * when entering interactive mode for inspecting. */
1078 PyErr_Fetch(&exception
, &value
, &tb
);
1082 if (value
== NULL
|| value
== Py_None
)
1084 if (PyExceptionInstance_Check(value
)) {
1085 /* The error code should be in the `code' attribute. */
1086 PyObject
*code
= PyObject_GetAttrString(value
, "code");
1090 if (value
== Py_None
)
1093 /* If we failed to dig out the 'code' attribute,
1094 just let the else clause below print the error. */
1096 if (PyInt_Check(value
))
1097 exitcode
= (int)PyInt_AsLong(value
);
1099 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1100 PySys_WriteStderr("\n");
1104 /* Restore and clear the exception info, in order to properly decref
1105 * the exception, value, and traceback. If we just exit instead,
1106 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1107 * some finalizers from running.
1109 PyErr_Restore(exception
, value
, tb
);
1116 PyErr_PrintEx(int set_sys_last_vars
)
1118 PyObject
*exception
, *v
, *tb
, *hook
;
1120 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1121 handle_system_exit();
1123 PyErr_Fetch(&exception
, &v
, &tb
);
1124 if (exception
== NULL
)
1126 PyErr_NormalizeException(&exception
, &v
, &tb
);
1127 if (exception
== NULL
)
1129 /* Now we know v != NULL too */
1130 if (set_sys_last_vars
) {
1131 PySys_SetObject("last_type", exception
);
1132 PySys_SetObject("last_value", v
);
1133 PySys_SetObject("last_traceback", tb
);
1135 hook
= PySys_GetObject("excepthook");
1137 PyObject
*args
= PyTuple_Pack(3,
1138 exception
, v
, tb
? tb
: Py_None
);
1139 PyObject
*result
= PyEval_CallObject(hook
, args
);
1140 if (result
== NULL
) {
1141 PyObject
*exception2
, *v2
, *tb2
;
1142 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1143 handle_system_exit();
1145 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1146 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1147 /* It should not be possible for exception2 or v2
1148 to be NULL. However PyErr_Display() can't
1149 tolerate NULLs, so just be safe. */
1150 if (exception2
== NULL
) {
1151 exception2
= Py_None
;
1152 Py_INCREF(exception2
);
1161 PySys_WriteStderr("Error in sys.excepthook:\n");
1162 PyErr_Display(exception2
, v2
, tb2
);
1163 PySys_WriteStderr("\nOriginal exception was:\n");
1164 PyErr_Display(exception
, v
, tb
);
1165 Py_DECREF(exception2
);
1172 PySys_WriteStderr("sys.excepthook is missing\n");
1173 PyErr_Display(exception
, v
, tb
);
1175 Py_XDECREF(exception
);
1181 PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1184 PyObject
*f
= PySys_GetObject("stderr");
1187 fprintf(stderr
, "lost sys.stderr\n");
1192 if (tb
&& tb
!= Py_None
)
1193 err
= PyTraceBack_Print(tb
, f
);
1195 PyObject_HasAttrString(value
, "print_file_and_line"))
1198 const char *filename
, *text
;
1200 if (!parse_syntax_error(value
, &message
, &filename
,
1201 &lineno
, &offset
, &text
))
1205 PyFile_WriteString(" File \"", f
);
1206 if (filename
== NULL
)
1207 PyFile_WriteString("<string>", f
);
1209 PyFile_WriteString(filename
, f
);
1210 PyFile_WriteString("\", line ", f
);
1211 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1212 PyFile_WriteString(buf
, f
);
1213 PyFile_WriteString("\n", f
);
1215 print_error_text(f
, offset
, text
);
1218 /* Can't be bothered to check all those
1219 PyFile_WriteString() calls */
1220 if (PyErr_Occurred())
1225 /* Don't do anything else */
1227 else if (PyExceptionClass_Check(exception
)) {
1228 PyObject
* moduleName
;
1229 char* className
= PyExceptionClass_Name(exception
);
1230 if (className
!= NULL
) {
1231 char *dot
= strrchr(className
, '.');
1236 moduleName
= PyObject_GetAttrString(exception
, "__module__");
1237 if (moduleName
== NULL
)
1238 err
= PyFile_WriteString("<unknown>", f
);
1240 char* modstr
= PyString_AsString(moduleName
);
1241 if (modstr
&& strcmp(modstr
, "exceptions"))
1243 err
= PyFile_WriteString(modstr
, f
);
1244 err
+= PyFile_WriteString(".", f
);
1246 Py_DECREF(moduleName
);
1249 if (className
== NULL
)
1250 err
= PyFile_WriteString("<unknown>", f
);
1252 err
= PyFile_WriteString(className
, f
);
1256 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1257 if (err
== 0 && (value
!= Py_None
)) {
1258 PyObject
*s
= PyObject_Str(value
);
1259 /* only print colon if the str() of the
1260 object is not the empty string
1264 else if (!PyString_Check(s
) ||
1265 PyString_GET_SIZE(s
) != 0)
1266 err
= PyFile_WriteString(": ", f
);
1268 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1271 /* try to write a newline in any case */
1272 err
+= PyFile_WriteString("\n", f
);
1275 /* If an error happened here, don't show it.
1276 XXX This is wrong, but too many callers rely on this behavior. */
1282 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1283 PyObject
*locals
, PyCompilerFlags
*flags
)
1285 PyObject
*ret
= NULL
;
1287 PyArena
*arena
= PyArena_New();
1291 mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
, arena
);
1293 ret
= run_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1294 PyArena_Free(arena
);
1299 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1300 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1304 PyArena
*arena
= PyArena_New();
1308 mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1309 flags
, NULL
, arena
);
1313 PyArena_Free(arena
);
1316 ret
= run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1317 PyArena_Free(arena
);
1322 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1323 PyCompilerFlags
*flags
, PyArena
*arena
)
1327 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1330 v
= PyEval_EvalCode(co
, globals
, locals
);
1336 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1337 PyObject
*locals
, PyCompilerFlags
*flags
)
1342 long PyImport_GetMagicNumber(void);
1344 magic
= PyMarshal_ReadLongFromFile(fp
);
1345 if (magic
!= PyImport_GetMagicNumber()) {
1346 PyErr_SetString(PyExc_RuntimeError
,
1347 "Bad magic number in .pyc file");
1350 (void) PyMarshal_ReadLongFromFile(fp
);
1351 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1353 if (v
== NULL
|| !PyCode_Check(v
)) {
1355 PyErr_SetString(PyExc_RuntimeError
,
1356 "Bad code object in .pyc file");
1359 co
= (PyCodeObject
*)v
;
1360 v
= PyEval_EvalCode(co
, globals
, locals
);
1362 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1368 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1369 PyCompilerFlags
*flags
)
1373 PyArena
*arena
= PyArena_New();
1377 mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1379 PyArena_Free(arena
);
1382 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1383 PyObject
*result
= PyAST_mod2obj(mod
);
1384 PyArena_Free(arena
);
1387 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1388 PyArena_Free(arena
);
1389 return (PyObject
*)co
;
1393 Py_SymtableString(const char *str
, const char *filename
, int start
)
1395 struct symtable
*st
;
1397 PyCompilerFlags flags
;
1398 PyArena
*arena
= PyArena_New();
1404 mod
= PyParser_ASTFromString(str
, filename
, start
, &flags
, arena
);
1406 PyArena_Free(arena
);
1409 st
= PySymtable_Build(mod
, filename
, 0);
1410 PyArena_Free(arena
);
1414 /* Preferred access to parser is through AST. */
1416 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1417 PyCompilerFlags
*flags
, PyArena
*arena
)
1421 int iflags
= PARSER_FLAGS(flags
);
1423 node
*n
= PyParser_ParseStringFlagsFilenameEx(s
, filename
,
1424 &_PyParser_Grammar
, start
, &err
,
1428 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1430 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1441 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1442 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1447 int iflags
= PARSER_FLAGS(flags
);
1449 node
*n
= PyParser_ParseFileFlagsEx(fp
, filename
, &_PyParser_Grammar
,
1450 start
, ps1
, ps2
, &err
, &iflags
);
1453 flags
->cf_flags
|= iflags
& PyCF_MASK
;
1455 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1462 *errcode
= err
.error
;
1467 /* Simplified interface to parsefile -- return node or set exception */
1470 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1473 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1474 start
, NULL
, NULL
, &err
, flags
);
1481 /* Simplified interface to parsestring -- return node or set exception */
1484 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1487 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1488 start
, &err
, flags
);
1495 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1496 int start
, int flags
)
1499 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1500 &_PyParser_Grammar
, start
, &err
, flags
);
1507 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1509 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1512 /* May want to move a more generalized form of this to parsetok.c or
1513 even parser modules. */
1516 PyParser_SetError(perrdetail
*err
)
1521 /* Set the error appropriate to the given input error code (see errcode.h) */
1524 err_input(perrdetail
*err
)
1526 PyObject
*v
, *w
, *errtype
;
1529 errtype
= PyExc_SyntaxError
;
1530 switch (err
->error
) {
1532 errtype
= PyExc_IndentationError
;
1533 if (err
->expected
== INDENT
)
1534 msg
= "expected an indented block";
1535 else if (err
->token
== INDENT
)
1536 msg
= "unexpected indent";
1537 else if (err
->token
== DEDENT
)
1538 msg
= "unexpected unindent";
1540 errtype
= PyExc_SyntaxError
;
1541 msg
= "invalid syntax";
1545 msg
= "invalid token";
1548 msg
= "EOF while scanning triple-quoted string literal";
1551 msg
= "EOL while scanning string literal";
1554 if (!PyErr_Occurred())
1555 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1561 msg
= "unexpected EOF while parsing";
1564 errtype
= PyExc_TabError
;
1565 msg
= "inconsistent use of tabs and spaces in indentation";
1568 msg
= "expression too long";
1571 errtype
= PyExc_IndentationError
;
1572 msg
= "unindent does not match any outer indentation level";
1575 errtype
= PyExc_IndentationError
;
1576 msg
= "too many levels of indentation";
1579 PyObject
*type
, *value
, *tb
;
1580 PyErr_Fetch(&type
, &value
, &tb
);
1581 if (value
!= NULL
) {
1582 u
= PyObject_Str(value
);
1584 msg
= PyString_AsString(u
);
1588 msg
= "unknown decode error";
1595 msg
= "unexpected character after line continuation character";
1598 fprintf(stderr
, "error=%d\n", err
->error
);
1599 msg
= "unknown parsing error";
1602 v
= Py_BuildValue("(ziiz)", err
->filename
,
1603 err
->lineno
, err
->offset
, err
->text
);
1606 w
= Py_BuildValue("(sO)", msg
, v
);
1609 PyErr_SetObject(errtype
, w
);
1612 if (err
->text
!= NULL
) {
1613 PyObject_FREE(err
->text
);
1618 /* Print fatal error message and abort */
1621 Py_FatalError(const char *msg
)
1623 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1625 OutputDebugString("Fatal Python error: ");
1626 OutputDebugString(msg
);
1627 OutputDebugString("\n");
1631 #endif /* MS_WINDOWS */
1635 /* Clean up and exit */
1638 #include "pythread.h"
1641 #define NEXITFUNCS 32
1642 static void (*exitfuncs
[NEXITFUNCS
])(void);
1643 static int nexitfuncs
= 0;
1645 int Py_AtExit(void (*func
)(void))
1647 if (nexitfuncs
>= NEXITFUNCS
)
1649 exitfuncs
[nexitfuncs
++] = func
;
1654 call_sys_exitfunc(void)
1656 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1660 Py_INCREF(exitfunc
);
1661 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1662 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1664 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1665 PySys_WriteStderr("Error in sys.exitfunc:\n");
1669 Py_DECREF(exitfunc
);
1677 call_ll_exitfuncs(void)
1679 while (nexitfuncs
> 0)
1680 (*exitfuncs
[--nexitfuncs
])();
1698 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1701 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1704 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1706 PyOS_InitInterrupts(); /* May imply initsignal() */
1711 * The file descriptor fd is considered ``interactive'' if either
1712 * a) isatty(fd) is TRUE, or
1713 * b) the -i flag was given, and the filename associated with
1714 * the descriptor is NULL or "<stdin>" or "???".
1717 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1719 if (isatty((int)fileno(fp
)))
1721 if (!Py_InteractiveFlag
)
1723 return (filename
== NULL
) ||
1724 (strcmp(filename
, "<stdin>") == 0) ||
1725 (strcmp(filename
, "???") == 0);
1729 #if defined(USE_STACKCHECK)
1730 #if defined(WIN32) && defined(_MSC_VER)
1732 /* Stack checking for Microsoft C */
1738 * Return non-zero when we run out of memory on the stack; zero otherwise.
1741 PyOS_CheckStack(void)
1744 /* alloca throws a stack overflow exception if there's
1745 not enough space left on the stack */
1746 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1748 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW
?
1749 EXCEPTION_EXECUTE_HANDLER
:
1750 EXCEPTION_CONTINUE_SEARCH
) {
1751 int errcode
= _resetstkoflw();
1754 Py_FatalError("Could not reset the stack!");
1760 #endif /* WIN32 && _MSC_VER */
1762 /* Alternate implementations can be added here... */
1764 #endif /* USE_STACKCHECK */
1767 /* Wrappers around sigaction() or signal(). */
1770 PyOS_getsig(int sig
)
1772 #ifdef HAVE_SIGACTION
1773 struct sigaction context
;
1774 if (sigaction(sig
, NULL
, &context
) == -1)
1776 return context
.sa_handler
;
1778 PyOS_sighandler_t handler
;
1779 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1780 #if defined(_MSC_VER) && _MSC_VER >= 1400
1782 /* Only these signals are valid */
1791 /* Don't call signal() with other values or it will assert */
1795 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1796 handler
= signal(sig
, SIG_IGN
);
1797 if (handler
!= SIG_ERR
)
1798 signal(sig
, handler
);
1804 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1806 #ifdef HAVE_SIGACTION
1807 struct sigaction context
, ocontext
;
1808 context
.sa_handler
= handler
;
1809 sigemptyset(&context
.sa_mask
);
1810 context
.sa_flags
= 0;
1811 if (sigaction(sig
, &context
, &ocontext
) == -1)
1813 return ocontext
.sa_handler
;
1815 PyOS_sighandler_t oldhandler
;
1816 oldhandler
= signal(sig
, handler
);
1817 #ifdef HAVE_SIGINTERRUPT
1818 siginterrupt(sig
, 1);
1824 /* Deprecated C API functions still provided for binary compatiblity */
1826 #undef PyParser_SimpleParseFile
1828 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1830 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1833 #undef PyParser_SimpleParseString
1835 PyParser_SimpleParseString(const char *str
, int start
)
1837 return PyParser_SimpleParseStringFlags(str
, start
, 0);
1840 #undef PyRun_AnyFile
1842 PyRun_AnyFile(FILE *fp
, const char *name
)
1844 return PyRun_AnyFileExFlags(fp
, name
, 0, NULL
);
1847 #undef PyRun_AnyFileEx
1849 PyRun_AnyFileEx(FILE *fp
, const char *name
, int closeit
)
1851 return PyRun_AnyFileExFlags(fp
, name
, closeit
, NULL
);
1854 #undef PyRun_AnyFileFlags
1856 PyRun_AnyFileFlags(FILE *fp
, const char *name
, PyCompilerFlags
*flags
)
1858 return PyRun_AnyFileExFlags(fp
, name
, 0, flags
);
1862 PyAPI_FUNC(PyObject
*)
1863 PyRun_File(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
)
1865 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, NULL
);
1869 PyAPI_FUNC(PyObject
*)
1870 PyRun_FileEx(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
, int c
)
1872 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, c
, NULL
);
1875 #undef PyRun_FileFlags
1876 PyAPI_FUNC(PyObject
*)
1877 PyRun_FileFlags(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
,
1878 PyCompilerFlags
*flags
)
1880 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, flags
);
1883 #undef PyRun_SimpleFile
1885 PyRun_SimpleFile(FILE *f
, const char *p
)
1887 return PyRun_SimpleFileExFlags(f
, p
, 0, NULL
);
1890 #undef PyRun_SimpleFileEx
1892 PyRun_SimpleFileEx(FILE *f
, const char *p
, int c
)
1894 return PyRun_SimpleFileExFlags(f
, p
, c
, NULL
);
1899 PyAPI_FUNC(PyObject
*)
1900 PyRun_String(const char *str
, int s
, PyObject
*g
, PyObject
*l
)
1902 return PyRun_StringFlags(str
, s
, g
, l
, NULL
);
1905 #undef PyRun_SimpleString
1907 PyRun_SimpleString(const char *s
)
1909 return PyRun_SimpleStringFlags(s
, NULL
);
1912 #undef Py_CompileString
1913 PyAPI_FUNC(PyObject
*)
1914 Py_CompileString(const char *str
, const char *p
, int s
)
1916 return Py_CompileStringFlags(str
, p
, s
, NULL
);
1919 #undef PyRun_InteractiveOne
1921 PyRun_InteractiveOne(FILE *f
, const char *p
)
1923 return PyRun_InteractiveOneFlags(f
, p
, NULL
);
1926 #undef PyRun_InteractiveLoop
1928 PyRun_InteractiveLoop(FILE *f
, const char *p
)
1930 return PyRun_InteractiveLoopFlags(f
, p
, NULL
);