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_DontWriteBytecodeFlag
; /* Suppress writing bytecode files (*.py[co]) */
76 int Py_UseClassExceptionsFlag
= 1; /* Needed by bltinmodule.c: deprecated */
77 int Py_FrozenFlag
; /* Needed by getpath.c */
78 int Py_UnicodeFlag
= 0; /* Needed by compile.c */
79 int Py_IgnoreEnvironmentFlag
; /* e.g. PYTHONPATH, PYTHONHOME */
80 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
81 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
82 true divisions (which they will be in 2.3). */
85 /* Reference to 'warnings' module, to avoid importing it
86 on the fly when the import lock may be held. See 683658/771097
88 static PyObject
*warnings_module
= NULL
;
90 /* Returns a borrowed reference to the 'warnings' module, or NULL.
91 If the module is returned, it is guaranteed to have been obtained
92 without acquiring the import lock
94 PyObject
*PyModule_GetWarningsModule(void)
96 PyObject
*typ
, *val
, *tb
;
97 PyObject
*all_modules
;
98 /* If we managed to get the module at init time, just use it */
100 return warnings_module
;
101 /* If it wasn't available at init time, it may be available
102 now in sys.modules (common scenario is frozen apps: import
103 at init time fails, but the frozen init code sets up sys.path
104 correctly, then does an implicit import of warnings for us
106 /* Save and restore any exceptions */
107 PyErr_Fetch(&typ
, &val
, &tb
);
109 all_modules
= PySys_GetObject("modules");
111 warnings_module
= PyDict_GetItemString(all_modules
, "warnings");
112 /* We keep a ref in the global */
113 Py_XINCREF(warnings_module
);
115 PyErr_Restore(typ
, val
, tb
);
116 return warnings_module
;
119 static int initialized
= 0;
121 /* API to access the initialized flag -- useful for esoteric use */
124 Py_IsInitialized(void)
129 /* Global initializations. Can be undone by Py_Finalize(). Don't
130 call this twice without an intervening Py_Finalize() call. When
131 initializations fail, a fatal error is issued and the function does
132 not return. On return, the first thread and interpreter state have
135 Locking: you must hold the interpreter lock while calling this.
136 (If the lock has not yet been initialized, that's equivalent to
137 having the lock, but you cannot use multiple threads.)
142 add_flag(int flag
, const char *envs
)
144 int env
= atoi(envs
);
153 Py_InitializeEx(int install_sigs
)
155 PyInterpreterState
*interp
;
156 PyThreadState
*tstate
;
157 PyObject
*bimod
, *sysmod
;
159 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
162 PyObject
*sys_stream
, *sys_isatty
;
164 extern void _Py_ReadyTypes(void);
170 if ((p
= Py_GETENV("PYTHONDEBUG")) && *p
!= '\0')
171 Py_DebugFlag
= add_flag(Py_DebugFlag
, p
);
172 if ((p
= Py_GETENV("PYTHONVERBOSE")) && *p
!= '\0')
173 Py_VerboseFlag
= add_flag(Py_VerboseFlag
, p
);
174 if ((p
= Py_GETENV("PYTHONOPTIMIZE")) && *p
!= '\0')
175 Py_OptimizeFlag
= add_flag(Py_OptimizeFlag
, p
);
176 if ((p
= Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p
!= '\0')
177 Py_DontWriteBytecodeFlag
= add_flag(Py_DontWriteBytecodeFlag
, p
);
179 interp
= PyInterpreterState_New();
181 Py_FatalError("Py_Initialize: can't make first interpreter");
183 tstate
= PyThreadState_New(interp
);
185 Py_FatalError("Py_Initialize: can't make first thread");
186 (void) PyThreadState_Swap(tstate
);
190 if (!_PyFrame_Init())
191 Py_FatalError("Py_Initialize: can't init frames");
194 Py_FatalError("Py_Initialize: can't init ints");
198 interp
->modules
= PyDict_New();
199 if (interp
->modules
== NULL
)
200 Py_FatalError("Py_Initialize: can't make modules dictionary");
201 interp
->modules_reloading
= PyDict_New();
202 if (interp
->modules_reloading
== NULL
)
203 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
205 #ifdef Py_USING_UNICODE
206 /* Init Unicode implementation; relies on the codec registry */
210 bimod
= _PyBuiltin_Init();
212 Py_FatalError("Py_Initialize: can't initialize __builtin__");
213 interp
->builtins
= PyModule_GetDict(bimod
);
214 if (interp
->builtins
== NULL
)
215 Py_FatalError("Py_Initialize: can't initialize builtins dict");
216 Py_INCREF(interp
->builtins
);
218 sysmod
= _PySys_Init();
220 Py_FatalError("Py_Initialize: can't initialize sys");
221 interp
->sysdict
= PyModule_GetDict(sysmod
);
222 if (interp
->sysdict
== NULL
)
223 Py_FatalError("Py_Initialize: can't initialize sys dict");
224 Py_INCREF(interp
->sysdict
);
225 _PyImport_FixupExtension("sys", "sys");
226 PySys_SetPath(Py_GetPath());
227 PyDict_SetItemString(interp
->sysdict
, "modules",
232 /* initialize builtin exceptions */
234 _PyImport_FixupExtension("exceptions", "exceptions");
236 /* phase 2 of builtins */
237 _PyImport_FixupExtension("__builtin__", "__builtin__");
239 _PyImportHooks_Init();
242 initsigs(); /* Signal handling stuff, including initintr() */
244 initmain(); /* Module __main__ */
246 initsite(); /* Module site */
248 /* auto-thread-state API, if available */
250 _PyGILState_Init(interp
, tstate
);
251 #endif /* WITH_THREAD */
253 warnings_module
= PyImport_ImportModule("warnings");
254 if (!warnings_module
)
257 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
258 /* On Unix, set the file system encoding according to the
259 user's preference, if the CODESET names a well-known
260 Python codec, and Py_FileSystemDefaultEncoding isn't
261 initialized by other means. Also set the encoding of
262 stdin and stdout if these are terminals. */
264 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
265 setlocale(LC_CTYPE
, "");
266 codeset
= nl_langinfo(CODESET
);
267 if (codeset
&& *codeset
) {
268 PyObject
*enc
= PyCodec_Encoder(codeset
);
270 codeset
= strdup(codeset
);
278 setlocale(LC_CTYPE
, saved_locale
);
282 sys_stream
= PySys_GetObject("stdin");
283 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
286 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
) &&
287 PyFile_Check(sys_stream
)) {
288 if (!PyFile_SetEncoding(sys_stream
, codeset
))
289 Py_FatalError("Cannot set codeset of stdin");
291 Py_XDECREF(sys_isatty
);
293 sys_stream
= PySys_GetObject("stdout");
294 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
297 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
) &&
298 PyFile_Check(sys_stream
)) {
299 if (!PyFile_SetEncoding(sys_stream
, codeset
))
300 Py_FatalError("Cannot set codeset of stdout");
302 Py_XDECREF(sys_isatty
);
304 sys_stream
= PySys_GetObject("stderr");
305 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
308 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
) &&
309 PyFile_Check(sys_stream
)) {
310 if (!PyFile_SetEncoding(sys_stream
, codeset
))
311 Py_FatalError("Cannot set codeset of stderr");
313 Py_XDECREF(sys_isatty
);
315 if (!Py_FileSystemDefaultEncoding
)
316 Py_FileSystemDefaultEncoding
= codeset
;
331 extern void dump_counts(FILE*);
334 /* Undo the effect of Py_Initialize().
336 Beware: if multiple interpreter and/or thread states exist, these
337 are not wiped out; only the current thread and interpreter state
338 are deleted. But since everything else is deleted, those other
339 interpreter and thread states should no longer be used.
341 (XXX We should do better, e.g. wipe out all interpreters and
351 PyInterpreterState
*interp
;
352 PyThreadState
*tstate
;
357 /* The interpreter is still entirely intact at this point, and the
358 * exit funcs may be relying on that. In particular, if some thread
359 * or exit func is still waiting to do an import, the import machinery
360 * expects Py_IsInitialized() to return true. So don't say the
361 * interpreter is uninitialized until after the exit funcs have run.
362 * Note that Threading.py uses an exit func to do a join on all the
363 * threads created thru it, so this also protects pending imports in
364 * the threads created via Threading.
369 /* Get current thread state and interpreter pointer */
370 tstate
= PyThreadState_GET();
371 interp
= tstate
->interp
;
373 /* Disable signal handling */
374 PyOS_FiniInterrupts();
376 /* drop module references we saved */
377 Py_XDECREF(warnings_module
);
378 warnings_module
= NULL
;
380 /* Clear type lookup cache */
383 /* Collect garbage. This may call finalizers; it's nice to call these
384 * before all modules are destroyed.
385 * XXX If a __del__ or weakref callback is triggered here, and tries to
386 * XXX import a module, bad things can happen, because Python no
387 * XXX longer believes it's initialized.
388 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
389 * XXX is easy to provoke that way. I've also seen, e.g.,
390 * XXX Exception exceptions.ImportError: 'No module named sha'
391 * XXX in <function callback at 0x008F5718> ignored
392 * XXX but I'm unclear on exactly how that one happens. In any case,
393 * XXX I haven't seen a real-life report of either of these.
397 /* With COUNT_ALLOCS, it helps to run GC multiple times:
398 each collection might release some types from the type
399 list, so they become garbage. */
400 while (PyGC_Collect() > 0)
404 /* Destroy all modules */
407 /* Collect final garbage. This disposes of cycles created by
408 * new-style class definitions, for example.
409 * XXX This is disabled because it caused too many problems. If
410 * XXX a __del__ or weakref callback triggers here, Python code has
411 * XXX a hard time running, because even the sys module has been
412 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
413 * XXX One symptom is a sequence of information-free messages
414 * XXX coming from threads (if a __del__ or callback is invoked,
415 * XXX other threads can execute too, and any exception they encounter
416 * XXX triggers a comedy of errors as subsystem after subsystem
417 * XXX fails to find what it *expects* to find in sys to help report
418 * XXX the exception and consequent unexpected failures). I've also
419 * XXX seen segfaults then, after adding print statements to the
420 * XXX Python code getting called.
426 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
429 /* Debugging stuff */
437 /* Display all objects still alive -- this can invoke arbitrary
438 * __repr__ overrides, so requires a mostly-intact interpreter.
439 * Alas, a lot of stuff may still be alive now that will be cleaned
442 if (Py_GETENV("PYTHONDUMPREFS"))
443 _Py_PrintReferences(stderr
);
444 #endif /* Py_TRACE_REFS */
446 /* Clear interpreter state */
447 PyInterpreterState_Clear(interp
);
449 /* Now we decref the exception classes. After this point nothing
450 can raise an exception. That's okay, because each Fini() method
451 below has been checked to make sure no exceptions are ever
457 /* Cleanup auto-thread-state */
460 #endif /* WITH_THREAD */
462 /* Delete current thread */
463 PyThreadState_Swap(NULL
);
464 PyInterpreterState_Delete(interp
);
466 /* Sundry finalizers */
477 #ifdef Py_USING_UNICODE
478 /* Cleanup Unicode implementation */
482 /* XXX Still allocated:
483 - various static ad-hoc pointers to interned strings
484 - int and float free list blocks
485 - whatever various modules and libraries allocate
488 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
491 /* Display addresses (& refcnts) of all objects still alive.
492 * An address can be used to find the repr of the object, printed
493 * above by _Py_PrintReferences.
495 if (Py_GETENV("PYTHONDUMPREFS"))
496 _Py_PrintReferenceAddresses(stderr
);
497 #endif /* Py_TRACE_REFS */
498 #ifdef PYMALLOC_DEBUG
499 if (Py_GETENV("PYTHONMALLOCSTATS"))
500 _PyObject_DebugMallocStats();
506 /* Create and initialize a new interpreter and thread, and return the
507 new thread. This requires that Py_Initialize() has been called
510 Unsuccessful initialization yields a NULL pointer. Note that *no*
511 exception information is available even in this case -- the
512 exception information is held in the thread, and there is no
520 Py_NewInterpreter(void)
522 PyInterpreterState
*interp
;
523 PyThreadState
*tstate
, *save_tstate
;
524 PyObject
*bimod
, *sysmod
;
527 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
529 interp
= PyInterpreterState_New();
533 tstate
= PyThreadState_New(interp
);
534 if (tstate
== NULL
) {
535 PyInterpreterState_Delete(interp
);
539 save_tstate
= PyThreadState_Swap(tstate
);
541 /* XXX The following is lax in error checking */
543 interp
->modules
= PyDict_New();
544 interp
->modules_reloading
= PyDict_New();
546 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
548 interp
->builtins
= PyModule_GetDict(bimod
);
549 if (interp
->builtins
== NULL
)
551 Py_INCREF(interp
->builtins
);
553 sysmod
= _PyImport_FindExtension("sys", "sys");
554 if (bimod
!= NULL
&& sysmod
!= NULL
) {
555 interp
->sysdict
= PyModule_GetDict(sysmod
);
556 if (interp
->sysdict
== NULL
)
558 Py_INCREF(interp
->sysdict
);
559 PySys_SetPath(Py_GetPath());
560 PyDict_SetItemString(interp
->sysdict
, "modules",
562 _PyImportHooks_Init();
568 if (!PyErr_Occurred())
572 /* Oops, it didn't work. Undo it all. */
575 PyThreadState_Clear(tstate
);
576 PyThreadState_Swap(save_tstate
);
577 PyThreadState_Delete(tstate
);
578 PyInterpreterState_Delete(interp
);
583 /* Delete an interpreter and its last thread. This requires that the
584 given thread state is current, that the thread has no remaining
585 frames, and that it is its interpreter's only remaining thread.
586 It is a fatal error to violate these constraints.
588 (Py_Finalize() doesn't have these constraints -- it zaps
589 everything, regardless.)
596 Py_EndInterpreter(PyThreadState
*tstate
)
598 PyInterpreterState
*interp
= tstate
->interp
;
600 if (tstate
!= PyThreadState_GET())
601 Py_FatalError("Py_EndInterpreter: thread is not current");
602 if (tstate
->frame
!= NULL
)
603 Py_FatalError("Py_EndInterpreter: thread still has a frame");
604 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
605 Py_FatalError("Py_EndInterpreter: not the last thread");
608 PyInterpreterState_Clear(interp
);
609 PyThreadState_Swap(NULL
);
610 PyInterpreterState_Delete(interp
);
613 static char *progname
= "python";
616 Py_SetProgramName(char *pn
)
623 Py_GetProgramName(void)
628 static char *default_home
= NULL
;
631 Py_SetPythonHome(char *home
)
637 Py_GetPythonHome(void)
639 char *home
= default_home
;
640 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
641 home
= Py_GETENV("PYTHONHOME");
645 /* Create __main__ module */
651 m
= PyImport_AddModule("__main__");
653 Py_FatalError("can't create __main__ module");
654 d
= PyModule_GetDict(m
);
655 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
656 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
658 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
659 Py_FatalError("can't add __builtins__ to __main__");
664 /* Import the site module (not into __main__ though) */
670 m
= PyImport_ImportModule("site");
672 f
= PySys_GetObject("stderr");
673 if (Py_VerboseFlag
) {
675 "'import site' failed; traceback:\n", f
);
680 "'import site' failed; use -v for traceback\n", f
);
689 /* Parse input from a file and execute it */
692 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
693 PyCompilerFlags
*flags
)
695 if (filename
== NULL
)
697 if (Py_FdIsInteractive(fp
, filename
)) {
698 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
704 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
708 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
712 PyCompilerFlags local_flags
;
715 flags
= &local_flags
;
716 local_flags
.cf_flags
= 0;
718 v
= PySys_GetObject("ps1");
720 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
723 v
= PySys_GetObject("ps2");
725 PySys_SetObject("ps2", v
= PyString_FromString("... "));
729 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
740 /* compute parser flags based on compiler flags */
741 #define PARSER_FLAGS(flags) \
742 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
743 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
746 /* Keep an example of flags with future keyword support. */
747 #define PARSER_FLAGS(flags) \
748 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
749 PyPARSE_DONT_IMPLY_DEDENT : 0) \
750 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
751 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
755 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
757 PyObject
*m
, *d
, *v
, *w
;
760 char *ps1
= "", *ps2
= "";
763 v
= PySys_GetObject("ps1");
768 else if (PyString_Check(v
))
769 ps1
= PyString_AsString(v
);
771 w
= PySys_GetObject("ps2");
776 else if (PyString_Check(w
))
777 ps2
= PyString_AsString(w
);
779 arena
= PyArena_New();
785 mod
= PyParser_ASTFromFile(fp
, filename
,
786 Py_single_input
, ps1
, ps2
,
787 flags
, &errcode
, arena
);
792 if (errcode
== E_EOF
) {
799 m
= PyImport_AddModule("__main__");
804 d
= PyModule_GetDict(m
);
805 v
= run_mod(mod
, filename
, d
, d
, flags
, arena
);
817 /* Check whether a file maybe a pyc file: Look at the extension,
818 the file type, and, if we may close it, at the first few bytes. */
821 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
823 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
826 /* Only look into the file if we are allowed to close it, since
827 it then should also be seekable. */
829 /* Read only two bytes of the magic. If the file was opened in
830 text mode, the bytes 3 and 4 of the magic (\r\n) might not
831 be read as they are on disk. */
832 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
833 unsigned char buf
[2];
834 /* Mess: In case of -x, the stream is NOT at its start now,
835 and ungetc() was used to push back the first newline,
836 which makes the current stream position formally undefined,
837 and a x-platform nightmare.
838 Unfortunately, we have no direct way to know whether -x
839 was specified. So we use a terrible hack: if the current
840 stream position is not 0, we assume -x was specified, and
841 give up. Bug 132850 on SourceForge spells out the
842 hopelessness of trying anything else (fseek and ftell
843 don't work predictably x-platform for text-mode files).
846 if (ftell(fp
) == 0) {
847 if (fread(buf
, 1, 2, fp
) == 2 &&
848 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
858 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
859 PyCompilerFlags
*flags
)
863 int set_file_name
= 0, ret
;
865 m
= PyImport_AddModule("__main__");
868 d
= PyModule_GetDict(m
);
869 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
870 PyObject
*f
= PyString_FromString(filename
);
873 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
880 ext
= filename
+ strlen(filename
) - 4;
881 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
882 /* Try to run a pyc file. First, re-open in binary */
885 if ((fp
= fopen(filename
, "rb")) == NULL
) {
886 fprintf(stderr
, "python: Can't reopen .pyc file\n");
890 /* Turn on optimization if a .pyo file is given */
891 if (strcmp(ext
, ".pyo") == 0)
893 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
895 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
908 if (set_file_name
&& PyDict_DelItemString(d
, "__file__"))
914 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
917 m
= PyImport_AddModule("__main__");
920 d
= PyModule_GetDict(m
);
921 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
933 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
934 int *lineno
, int *offset
, const char **text
)
939 /* old style errors */
940 if (PyTuple_Check(err
))
941 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
942 lineno
, offset
, text
);
944 /* new style errors. `err' is an instance */
946 if (! (v
= PyObject_GetAttrString(err
, "msg")))
950 if (!(v
= PyObject_GetAttrString(err
, "filename")))
954 else if (! (*filename
= PyString_AsString(v
)))
958 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
960 hold
= PyInt_AsLong(v
);
963 if (hold
< 0 && PyErr_Occurred())
967 if (!(v
= PyObject_GetAttrString(err
, "offset")))
974 hold
= PyInt_AsLong(v
);
977 if (hold
< 0 && PyErr_Occurred())
982 if (!(v
= PyObject_GetAttrString(err
, "text")))
986 else if (! (*text
= PyString_AsString(v
)))
1003 print_error_text(PyObject
*f
, int offset
, const char *text
)
1007 if (offset
> 0 && offset
== (int)strlen(text
))
1010 nl
= strchr(text
, '\n');
1011 if (nl
== NULL
|| nl
-text
>= offset
)
1013 offset
-= (int)(nl
+1-text
);
1016 while (*text
== ' ' || *text
== '\t') {
1021 PyFile_WriteString(" ", f
);
1022 PyFile_WriteString(text
, f
);
1023 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
1024 PyFile_WriteString("\n", f
);
1027 PyFile_WriteString(" ", f
);
1029 while (offset
> 0) {
1030 PyFile_WriteString(" ", f
);
1033 PyFile_WriteString("^\n", f
);
1037 handle_system_exit(void)
1039 PyObject
*exception
, *value
, *tb
;
1043 /* Don't exit if -i flag was given. This flag is set to 0
1044 * when entering interactive mode for inspecting. */
1047 PyErr_Fetch(&exception
, &value
, &tb
);
1051 if (value
== NULL
|| value
== Py_None
)
1053 if (PyExceptionInstance_Check(value
)) {
1054 /* The error code should be in the `code' attribute. */
1055 PyObject
*code
= PyObject_GetAttrString(value
, "code");
1059 if (value
== Py_None
)
1062 /* If we failed to dig out the 'code' attribute,
1063 just let the else clause below print the error. */
1065 if (PyInt_Check(value
))
1066 exitcode
= (int)PyInt_AsLong(value
);
1068 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1069 PySys_WriteStderr("\n");
1073 /* Restore and clear the exception info, in order to properly decref
1074 * the exception, value, and traceback. If we just exit instead,
1075 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1076 * some finalizers from running.
1078 PyErr_Restore(exception
, value
, tb
);
1085 PyErr_PrintEx(int set_sys_last_vars
)
1087 PyObject
*exception
, *v
, *tb
, *hook
;
1089 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1090 handle_system_exit();
1092 PyErr_Fetch(&exception
, &v
, &tb
);
1093 if (exception
== NULL
)
1095 PyErr_NormalizeException(&exception
, &v
, &tb
);
1096 if (exception
== NULL
)
1098 /* Now we know v != NULL too */
1099 if (set_sys_last_vars
) {
1100 PySys_SetObject("last_type", exception
);
1101 PySys_SetObject("last_value", v
);
1102 PySys_SetObject("last_traceback", tb
);
1104 hook
= PySys_GetObject("excepthook");
1106 PyObject
*args
= PyTuple_Pack(3,
1107 exception
, v
, tb
? tb
: Py_None
);
1108 PyObject
*result
= PyEval_CallObject(hook
, args
);
1109 if (result
== NULL
) {
1110 PyObject
*exception2
, *v2
, *tb2
;
1111 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1112 handle_system_exit();
1114 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1115 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1116 /* It should not be possible for exception2 or v2
1117 to be NULL. However PyErr_Display() can't
1118 tolerate NULLs, so just be safe. */
1119 if (exception2
== NULL
) {
1120 exception2
= Py_None
;
1121 Py_INCREF(exception2
);
1130 PySys_WriteStderr("Error in sys.excepthook:\n");
1131 PyErr_Display(exception2
, v2
, tb2
);
1132 PySys_WriteStderr("\nOriginal exception was:\n");
1133 PyErr_Display(exception
, v
, tb
);
1134 Py_DECREF(exception2
);
1141 PySys_WriteStderr("sys.excepthook is missing\n");
1142 PyErr_Display(exception
, v
, tb
);
1144 Py_XDECREF(exception
);
1150 PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1153 PyObject
*f
= PySys_GetObject("stderr");
1156 fprintf(stderr
, "lost sys.stderr\n");
1161 if (tb
&& tb
!= Py_None
)
1162 err
= PyTraceBack_Print(tb
, f
);
1164 PyObject_HasAttrString(value
, "print_file_and_line"))
1167 const char *filename
, *text
;
1169 if (!parse_syntax_error(value
, &message
, &filename
,
1170 &lineno
, &offset
, &text
))
1174 PyFile_WriteString(" File \"", f
);
1175 if (filename
== NULL
)
1176 PyFile_WriteString("<string>", f
);
1178 PyFile_WriteString(filename
, f
);
1179 PyFile_WriteString("\", line ", f
);
1180 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1181 PyFile_WriteString(buf
, f
);
1182 PyFile_WriteString("\n", f
);
1184 print_error_text(f
, offset
, text
);
1187 /* Can't be bothered to check all those
1188 PyFile_WriteString() calls */
1189 if (PyErr_Occurred())
1194 /* Don't do anything else */
1196 else if (PyExceptionClass_Check(exception
)) {
1197 PyObject
* moduleName
;
1198 char* className
= PyExceptionClass_Name(exception
);
1199 if (className
!= NULL
) {
1200 char *dot
= strrchr(className
, '.');
1205 moduleName
= PyObject_GetAttrString(exception
, "__module__");
1206 if (moduleName
== NULL
)
1207 err
= PyFile_WriteString("<unknown>", f
);
1209 char* modstr
= PyString_AsString(moduleName
);
1210 if (modstr
&& strcmp(modstr
, "exceptions"))
1212 err
= PyFile_WriteString(modstr
, f
);
1213 err
+= PyFile_WriteString(".", f
);
1215 Py_DECREF(moduleName
);
1218 if (className
== NULL
)
1219 err
= PyFile_WriteString("<unknown>", f
);
1221 err
= PyFile_WriteString(className
, f
);
1225 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1226 if (err
== 0 && (value
!= Py_None
)) {
1227 PyObject
*s
= PyObject_Str(value
);
1228 /* only print colon if the str() of the
1229 object is not the empty string
1233 else if (!PyString_Check(s
) ||
1234 PyString_GET_SIZE(s
) != 0)
1235 err
= PyFile_WriteString(": ", f
);
1237 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1240 /* try to write a newline in any case */
1241 err
+= PyFile_WriteString("\n", f
);
1244 /* If an error happened here, don't show it.
1245 XXX This is wrong, but too many callers rely on this behavior. */
1251 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1252 PyObject
*locals
, PyCompilerFlags
*flags
)
1254 PyObject
*ret
= NULL
;
1256 PyArena
*arena
= PyArena_New();
1260 mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
, arena
);
1262 ret
= run_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1263 PyArena_Free(arena
);
1268 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1269 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1273 PyArena
*arena
= PyArena_New();
1277 mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1278 flags
, NULL
, arena
);
1282 PyArena_Free(arena
);
1285 ret
= run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1286 PyArena_Free(arena
);
1291 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1292 PyCompilerFlags
*flags
, PyArena
*arena
)
1296 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1299 v
= PyEval_EvalCode(co
, globals
, locals
);
1305 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1306 PyObject
*locals
, PyCompilerFlags
*flags
)
1311 long PyImport_GetMagicNumber(void);
1313 magic
= PyMarshal_ReadLongFromFile(fp
);
1314 if (magic
!= PyImport_GetMagicNumber()) {
1315 PyErr_SetString(PyExc_RuntimeError
,
1316 "Bad magic number in .pyc file");
1319 (void) PyMarshal_ReadLongFromFile(fp
);
1320 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1322 if (v
== NULL
|| !PyCode_Check(v
)) {
1324 PyErr_SetString(PyExc_RuntimeError
,
1325 "Bad code object in .pyc file");
1328 co
= (PyCodeObject
*)v
;
1329 v
= PyEval_EvalCode(co
, globals
, locals
);
1331 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1337 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1338 PyCompilerFlags
*flags
)
1342 PyArena
*arena
= PyArena_New();
1346 mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1348 PyArena_Free(arena
);
1351 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1352 PyObject
*result
= PyAST_mod2obj(mod
);
1353 PyArena_Free(arena
);
1356 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1357 PyArena_Free(arena
);
1358 return (PyObject
*)co
;
1362 Py_SymtableString(const char *str
, const char *filename
, int start
)
1364 struct symtable
*st
;
1366 PyArena
*arena
= PyArena_New();
1370 mod
= PyParser_ASTFromString(str
, filename
, start
, NULL
, arena
);
1372 PyArena_Free(arena
);
1375 st
= PySymtable_Build(mod
, filename
, 0);
1376 PyArena_Free(arena
);
1380 /* Preferred access to parser is through AST. */
1382 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1383 PyCompilerFlags
*flags
, PyArena
*arena
)
1387 node
*n
= PyParser_ParseStringFlagsFilename(s
, filename
,
1388 &_PyParser_Grammar
, start
, &err
,
1389 PARSER_FLAGS(flags
));
1391 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1402 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1403 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1408 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1409 start
, ps1
, ps2
, &err
, PARSER_FLAGS(flags
));
1411 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1418 *errcode
= err
.error
;
1423 /* Simplified interface to parsefile -- return node or set exception */
1426 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1429 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1430 start
, NULL
, NULL
, &err
, flags
);
1437 /* Simplified interface to parsestring -- return node or set exception */
1440 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1443 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1444 start
, &err
, flags
);
1451 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1452 int start
, int flags
)
1455 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1456 &_PyParser_Grammar
, start
, &err
, flags
);
1463 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1465 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1468 /* May want to move a more generalized form of this to parsetok.c or
1469 even parser modules. */
1472 PyParser_SetError(perrdetail
*err
)
1477 /* Set the error appropriate to the given input error code (see errcode.h) */
1480 err_input(perrdetail
*err
)
1482 PyObject
*v
, *w
, *errtype
;
1485 errtype
= PyExc_SyntaxError
;
1486 switch (err
->error
) {
1488 errtype
= PyExc_IndentationError
;
1489 if (err
->expected
== INDENT
)
1490 msg
= "expected an indented block";
1491 else if (err
->token
== INDENT
)
1492 msg
= "unexpected indent";
1493 else if (err
->token
== DEDENT
)
1494 msg
= "unexpected unindent";
1496 errtype
= PyExc_SyntaxError
;
1497 msg
= "invalid syntax";
1501 msg
= "invalid token";
1504 msg
= "EOF while scanning triple-quoted string";
1507 msg
= "EOL while scanning single-quoted string";
1510 if (!PyErr_Occurred())
1511 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1517 msg
= "unexpected EOF while parsing";
1520 errtype
= PyExc_TabError
;
1521 msg
= "inconsistent use of tabs and spaces in indentation";
1524 msg
= "expression too long";
1527 errtype
= PyExc_IndentationError
;
1528 msg
= "unindent does not match any outer indentation level";
1531 errtype
= PyExc_IndentationError
;
1532 msg
= "too many levels of indentation";
1535 PyObject
*type
, *value
, *tb
;
1536 PyErr_Fetch(&type
, &value
, &tb
);
1537 if (value
!= NULL
) {
1538 u
= PyObject_Str(value
);
1540 msg
= PyString_AsString(u
);
1544 msg
= "unknown decode error";
1551 msg
= "unexpected character after line continuation character";
1554 fprintf(stderr
, "error=%d\n", err
->error
);
1555 msg
= "unknown parsing error";
1558 v
= Py_BuildValue("(ziiz)", err
->filename
,
1559 err
->lineno
, err
->offset
, err
->text
);
1560 if (err
->text
!= NULL
) {
1561 PyObject_FREE(err
->text
);
1566 w
= Py_BuildValue("(sO)", msg
, v
);
1569 PyErr_SetObject(errtype
, w
);
1573 /* Print fatal error message and abort */
1576 Py_FatalError(const char *msg
)
1578 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1580 OutputDebugString("Fatal Python error: ");
1581 OutputDebugString(msg
);
1582 OutputDebugString("\n");
1586 #endif /* MS_WINDOWS */
1590 /* Clean up and exit */
1593 #include "pythread.h"
1596 #define NEXITFUNCS 32
1597 static void (*exitfuncs
[NEXITFUNCS
])(void);
1598 static int nexitfuncs
= 0;
1600 int Py_AtExit(void (*func
)(void))
1602 if (nexitfuncs
>= NEXITFUNCS
)
1604 exitfuncs
[nexitfuncs
++] = func
;
1609 call_sys_exitfunc(void)
1611 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1615 Py_INCREF(exitfunc
);
1616 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1617 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1619 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1620 PySys_WriteStderr("Error in sys.exitfunc:\n");
1624 Py_DECREF(exitfunc
);
1632 call_ll_exitfuncs(void)
1634 while (nexitfuncs
> 0)
1635 (*exitfuncs
[--nexitfuncs
])();
1653 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1656 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1659 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1661 PyOS_InitInterrupts(); /* May imply initsignal() */
1666 * The file descriptor fd is considered ``interactive'' if either
1667 * a) isatty(fd) is TRUE, or
1668 * b) the -i flag was given, and the filename associated with
1669 * the descriptor is NULL or "<stdin>" or "???".
1672 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1674 if (isatty((int)fileno(fp
)))
1676 if (!Py_InteractiveFlag
)
1678 return (filename
== NULL
) ||
1679 (strcmp(filename
, "<stdin>") == 0) ||
1680 (strcmp(filename
, "???") == 0);
1684 #if defined(USE_STACKCHECK)
1685 #if defined(WIN32) && defined(_MSC_VER)
1687 /* Stack checking for Microsoft C */
1693 * Return non-zero when we run out of memory on the stack; zero otherwise.
1696 PyOS_CheckStack(void)
1699 /* alloca throws a stack overflow exception if there's
1700 not enough space left on the stack */
1701 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1703 } __except (EXCEPTION_EXECUTE_HANDLER
) {
1704 /* just ignore all errors */
1709 #endif /* WIN32 && _MSC_VER */
1711 /* Alternate implementations can be added here... */
1713 #endif /* USE_STACKCHECK */
1716 /* Wrappers around sigaction() or signal(). */
1719 PyOS_getsig(int sig
)
1721 #ifdef HAVE_SIGACTION
1722 struct sigaction context
;
1723 if (sigaction(sig
, NULL
, &context
) == -1)
1725 return context
.sa_handler
;
1727 PyOS_sighandler_t handler
;
1728 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1729 #if defined(_MSC_VER) && _MSC_VER >= 1400
1731 /* Only these signals are valid */
1740 /* Don't call signal() with other values or it will assert */
1744 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1745 handler
= signal(sig
, SIG_IGN
);
1746 if (handler
!= SIG_ERR
)
1747 signal(sig
, handler
);
1753 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1755 #ifdef HAVE_SIGACTION
1756 struct sigaction context
, ocontext
;
1757 context
.sa_handler
= handler
;
1758 sigemptyset(&context
.sa_mask
);
1759 context
.sa_flags
= 0;
1760 if (sigaction(sig
, &context
, &ocontext
) == -1)
1762 return ocontext
.sa_handler
;
1764 PyOS_sighandler_t oldhandler
;
1765 oldhandler
= signal(sig
, handler
);
1766 #ifdef HAVE_SIGINTERRUPT
1767 siginterrupt(sig
, 1);
1773 /* Deprecated C API functions still provided for binary compatiblity */
1775 #undef PyParser_SimpleParseFile
1777 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1779 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1782 #undef PyParser_SimpleParseString
1784 PyParser_SimpleParseString(const char *str
, int start
)
1786 return PyParser_SimpleParseStringFlags(str
, start
, 0);
1789 #undef PyRun_AnyFile
1791 PyRun_AnyFile(FILE *fp
, const char *name
)
1793 return PyRun_AnyFileExFlags(fp
, name
, 0, NULL
);
1796 #undef PyRun_AnyFileEx
1798 PyRun_AnyFileEx(FILE *fp
, const char *name
, int closeit
)
1800 return PyRun_AnyFileExFlags(fp
, name
, closeit
, NULL
);
1803 #undef PyRun_AnyFileFlags
1805 PyRun_AnyFileFlags(FILE *fp
, const char *name
, PyCompilerFlags
*flags
)
1807 return PyRun_AnyFileExFlags(fp
, name
, 0, flags
);
1811 PyAPI_FUNC(PyObject
*)
1812 PyRun_File(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
)
1814 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, NULL
);
1818 PyAPI_FUNC(PyObject
*)
1819 PyRun_FileEx(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
, int c
)
1821 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, c
, NULL
);
1824 #undef PyRun_FileFlags
1825 PyAPI_FUNC(PyObject
*)
1826 PyRun_FileFlags(FILE *fp
, const char *p
, int s
, PyObject
*g
, PyObject
*l
,
1827 PyCompilerFlags
*flags
)
1829 return PyRun_FileExFlags(fp
, p
, s
, g
, l
, 0, flags
);
1832 #undef PyRun_SimpleFile
1834 PyRun_SimpleFile(FILE *f
, const char *p
)
1836 return PyRun_SimpleFileExFlags(f
, p
, 0, NULL
);
1839 #undef PyRun_SimpleFileEx
1841 PyRun_SimpleFileEx(FILE *f
, const char *p
, int c
)
1843 return PyRun_SimpleFileExFlags(f
, p
, c
, NULL
);
1848 PyAPI_FUNC(PyObject
*)
1849 PyRun_String(const char *str
, int s
, PyObject
*g
, PyObject
*l
)
1851 return PyRun_StringFlags(str
, s
, g
, l
, NULL
);
1854 #undef PyRun_SimpleString
1856 PyRun_SimpleString(const char *s
)
1858 return PyRun_SimpleStringFlags(s
, NULL
);
1861 #undef Py_CompileString
1862 PyAPI_FUNC(PyObject
*)
1863 Py_CompileString(const char *str
, const char *p
, int s
)
1865 return Py_CompileStringFlags(str
, p
, s
, NULL
);
1868 #undef PyRun_InteractiveOne
1870 PyRun_InteractiveOne(FILE *f
, const char *p
)
1872 return PyRun_InteractiveOneFlags(f
, p
, NULL
);
1875 #undef PyRun_InteractiveLoop
1877 PyRun_InteractiveLoop(FILE *f
, const char *p
)
1879 return PyRun_InteractiveLoopFlags(f
, p
, NULL
);