2 /* Python interpreter top-level routines, including init/exit */
6 #include "Python-ast.h"
22 #ifdef HAVE_LANGINFO_H
32 extern char *Py_GetPath(void);
34 extern grammar _PyParser_Grammar
; /* From graminit.c */
37 static void initmain(void);
38 static void initsite(void);
39 static PyObject
*run_err_mod(mod_ty
, const char *, PyObject
*, PyObject
*,
40 PyCompilerFlags
*, PyArena
*arena
);
41 static PyObject
*run_mod(mod_ty
, const char *, PyObject
*, PyObject
*,
42 PyCompilerFlags
*, PyArena
*);
43 static PyObject
*run_pyc_file(FILE *, const char *, PyObject
*, PyObject
*,
45 static void err_input(perrdetail
*);
46 static void initsigs(void);
47 static void call_sys_exitfunc(void);
48 static void call_ll_exitfuncs(void);
49 extern void _PyUnicode_Init(void);
50 extern void _PyUnicode_Fini(void);
53 extern void _PyGILState_Init(PyInterpreterState
*, PyThreadState
*);
54 extern void _PyGILState_Fini(void);
55 #endif /* WITH_THREAD */
57 int Py_DebugFlag
; /* Needed by parser.c */
58 int Py_VerboseFlag
; /* Needed by import.c */
59 int Py_InteractiveFlag
; /* Needed by Py_FdIsInteractive() below */
60 int Py_NoSiteFlag
; /* Suppress 'import site' */
61 int Py_UseClassExceptionsFlag
= 1; /* Needed by bltinmodule.c: deprecated */
62 int Py_FrozenFlag
; /* Needed by getpath.c */
63 int Py_UnicodeFlag
= 0; /* Needed by compile.c */
64 int Py_IgnoreEnvironmentFlag
; /* e.g. PYTHONPATH, PYTHONHOME */
65 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
66 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
67 true divisions (which they will be in 2.3). */
70 /* Reference to 'warnings' module, to avoid importing it
71 on the fly when the import lock may be held. See 683658/771097
73 static PyObject
*warnings_module
= NULL
;
75 /* Returns a borrowed reference to the 'warnings' module, or NULL.
76 If the module is returned, it is guaranteed to have been obtained
77 without acquiring the import lock
79 PyObject
*PyModule_GetWarningsModule(void)
81 PyObject
*typ
, *val
, *tb
;
82 PyObject
*all_modules
;
83 /* If we managed to get the module at init time, just use it */
85 return warnings_module
;
86 /* If it wasn't available at init time, it may be available
87 now in sys.modules (common scenario is frozen apps: import
88 at init time fails, but the frozen init code sets up sys.path
89 correctly, then does an implicit import of warnings for us
91 /* Save and restore any exceptions */
92 PyErr_Fetch(&typ
, &val
, &tb
);
94 all_modules
= PySys_GetObject("modules");
96 warnings_module
= PyDict_GetItemString(all_modules
, "warnings");
97 /* We keep a ref in the global */
98 Py_XINCREF(warnings_module
);
100 PyErr_Restore(typ
, val
, tb
);
101 return warnings_module
;
104 static int initialized
= 0;
106 /* API to access the initialized flag -- useful for esoteric use */
109 Py_IsInitialized(void)
114 /* Global initializations. Can be undone by Py_Finalize(). Don't
115 call this twice without an intervening Py_Finalize() call. When
116 initializations fail, a fatal error is issued and the function does
117 not return. On return, the first thread and interpreter state have
120 Locking: you must hold the interpreter lock while calling this.
121 (If the lock has not yet been initialized, that's equivalent to
122 having the lock, but you cannot use multiple threads.)
127 add_flag(int flag
, const char *envs
)
129 int env
= atoi(envs
);
138 Py_InitializeEx(int install_sigs
)
140 PyInterpreterState
*interp
;
141 PyThreadState
*tstate
;
142 PyObject
*bimod
, *sysmod
;
144 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
147 PyObject
*sys_stream
, *sys_isatty
;
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
);
162 interp
= PyInterpreterState_New();
164 Py_FatalError("Py_Initialize: can't make first interpreter");
166 tstate
= PyThreadState_New(interp
);
168 Py_FatalError("Py_Initialize: can't make first thread");
169 (void) PyThreadState_Swap(tstate
);
173 if (!_PyFrame_Init())
174 Py_FatalError("Py_Initialize: can't init frames");
177 Py_FatalError("Py_Initialize: can't init ints");
181 interp
->modules
= PyDict_New();
182 if (interp
->modules
== NULL
)
183 Py_FatalError("Py_Initialize: can't make modules dictionary");
185 #ifdef Py_USING_UNICODE
186 /* Init Unicode implementation; relies on the codec registry */
190 bimod
= _PyBuiltin_Init();
192 Py_FatalError("Py_Initialize: can't initialize __builtin__");
193 interp
->builtins
= PyModule_GetDict(bimod
);
194 Py_INCREF(interp
->builtins
);
196 sysmod
= _PySys_Init();
198 Py_FatalError("Py_Initialize: can't initialize sys");
199 interp
->sysdict
= PyModule_GetDict(sysmod
);
200 Py_INCREF(interp
->sysdict
);
201 _PyImport_FixupExtension("sys", "sys");
202 PySys_SetPath(Py_GetPath());
203 PyDict_SetItemString(interp
->sysdict
, "modules",
208 /* initialize builtin exceptions */
210 _PyImport_FixupExtension("exceptions", "exceptions");
212 /* phase 2 of builtins */
213 _PyImport_FixupExtension("__builtin__", "__builtin__");
215 _PyImportHooks_Init();
218 initsigs(); /* Signal handling stuff, including initintr() */
220 initmain(); /* Module __main__ */
222 initsite(); /* Module site */
224 /* auto-thread-state API, if available */
226 _PyGILState_Init(interp
, tstate
);
227 #endif /* WITH_THREAD */
229 warnings_module
= PyImport_ImportModule("warnings");
230 if (!warnings_module
)
233 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
234 /* On Unix, set the file system encoding according to the
235 user's preference, if the CODESET names a well-known
236 Python codec, and Py_FileSystemDefaultEncoding isn't
237 initialized by other means. Also set the encoding of
238 stdin and stdout if these are terminals. */
240 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
241 setlocale(LC_CTYPE
, "");
242 codeset
= nl_langinfo(CODESET
);
243 if (codeset
&& *codeset
) {
244 PyObject
*enc
= PyCodec_Encoder(codeset
);
246 codeset
= strdup(codeset
);
254 setlocale(LC_CTYPE
, saved_locale
);
258 sys_stream
= PySys_GetObject("stdin");
259 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
262 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
263 if (!PyFile_SetEncoding(sys_stream
, codeset
))
264 Py_FatalError("Cannot set codeset of stdin");
266 Py_XDECREF(sys_isatty
);
268 sys_stream
= PySys_GetObject("stdout");
269 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
272 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
273 if (!PyFile_SetEncoding(sys_stream
, codeset
))
274 Py_FatalError("Cannot set codeset of stdout");
276 Py_XDECREF(sys_isatty
);
278 if (!Py_FileSystemDefaultEncoding
)
279 Py_FileSystemDefaultEncoding
= codeset
;
294 extern void dump_counts(void);
297 /* Undo the effect of Py_Initialize().
299 Beware: if multiple interpreter and/or thread states exist, these
300 are not wiped out; only the current thread and interpreter state
301 are deleted. But since everything else is deleted, those other
302 interpreter and thread states should no longer be used.
304 (XXX We should do better, e.g. wipe out all interpreters and
314 PyInterpreterState
*interp
;
315 PyThreadState
*tstate
;
320 /* The interpreter is still entirely intact at this point, and the
321 * exit funcs may be relying on that. In particular, if some thread
322 * or exit func is still waiting to do an import, the import machinery
323 * expects Py_IsInitialized() to return true. So don't say the
324 * interpreter is uninitialized until after the exit funcs have run.
325 * Note that Threading.py uses an exit func to do a join on all the
326 * threads created thru it, so this also protects pending imports in
327 * the threads created via Threading.
332 /* Get current thread state and interpreter pointer */
333 tstate
= PyThreadState_GET();
334 interp
= tstate
->interp
;
336 /* Disable signal handling */
337 PyOS_FiniInterrupts();
339 /* drop module references we saved */
340 Py_XDECREF(warnings_module
);
341 warnings_module
= NULL
;
343 /* Collect garbage. This may call finalizers; it's nice to call these
344 * before all modules are destroyed.
345 * XXX If a __del__ or weakref callback is triggered here, and tries to
346 * XXX import a module, bad things can happen, because Python no
347 * XXX longer believes it's initialized.
348 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
349 * XXX is easy to provoke that way. I've also seen, e.g.,
350 * XXX Exception exceptions.ImportError: 'No module named sha'
351 * XXX in <function callback at 0x008F5718> ignored
352 * XXX but I'm unclear on exactly how that one happens. In any case,
353 * XXX I haven't seen a real-life report of either of these.
357 /* Destroy all modules */
360 /* Collect final garbage. This disposes of cycles created by
361 * new-style class definitions, for example.
362 * XXX This is disabled because it caused too many problems. If
363 * XXX a __del__ or weakref callback triggers here, Python code has
364 * XXX a hard time running, because even the sys module has been
365 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
366 * XXX One symptom is a sequence of information-free messages
367 * XXX coming from threads (if a __del__ or callback is invoked,
368 * XXX other threads can execute too, and any exception they encounter
369 * XXX triggers a comedy of errors as subsystem after subsystem
370 * XXX fails to find what it *expects* to find in sys to help report
371 * XXX the exception and consequent unexpected failures). I've also
372 * XXX seen segfaults then, after adding print statements to the
373 * XXX Python code getting called.
379 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
382 /* Debugging stuff */
388 fprintf(stderr
, "[%ld refs]\n", _Py_RefTotal
);
392 /* Display all objects still alive -- this can invoke arbitrary
393 * __repr__ overrides, so requires a mostly-intact interpreter.
394 * Alas, a lot of stuff may still be alive now that will be cleaned
397 if (Py_GETENV("PYTHONDUMPREFS"))
398 _Py_PrintReferences(stderr
);
399 #endif /* Py_TRACE_REFS */
401 /* Cleanup auto-thread-state */
404 #endif /* WITH_THREAD */
406 /* Clear interpreter state */
407 PyInterpreterState_Clear(interp
);
409 /* Now we decref the exception classes. After this point nothing
410 can raise an exception. That's okay, because each Fini() method
411 below has been checked to make sure no exceptions are ever
417 /* Delete current thread */
418 PyThreadState_Swap(NULL
);
419 PyInterpreterState_Delete(interp
);
421 /* Sundry finalizers */
432 #ifdef Py_USING_UNICODE
433 /* Cleanup Unicode implementation */
437 /* XXX Still allocated:
438 - various static ad-hoc pointers to interned strings
439 - int and float free list blocks
440 - whatever various modules and libraries allocate
443 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
446 /* Display addresses (& refcnts) of all objects still alive.
447 * An address can be used to find the repr of the object, printed
448 * above by _Py_PrintReferences.
450 if (Py_GETENV("PYTHONDUMPREFS"))
451 _Py_PrintReferenceAddresses(stderr
);
452 #endif /* Py_TRACE_REFS */
453 #ifdef PYMALLOC_DEBUG
454 if (Py_GETENV("PYTHONMALLOCSTATS"))
455 _PyObject_DebugMallocStats();
461 /* Create and initialize a new interpreter and thread, and return the
462 new thread. This requires that Py_Initialize() has been called
465 Unsuccessful initialization yields a NULL pointer. Note that *no*
466 exception information is available even in this case -- the
467 exception information is held in the thread, and there is no
475 Py_NewInterpreter(void)
477 PyInterpreterState
*interp
;
478 PyThreadState
*tstate
, *save_tstate
;
479 PyObject
*bimod
, *sysmod
;
482 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
484 interp
= PyInterpreterState_New();
488 tstate
= PyThreadState_New(interp
);
489 if (tstate
== NULL
) {
490 PyInterpreterState_Delete(interp
);
494 save_tstate
= PyThreadState_Swap(tstate
);
496 /* XXX The following is lax in error checking */
498 interp
->modules
= PyDict_New();
500 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
502 interp
->builtins
= PyModule_GetDict(bimod
);
503 Py_INCREF(interp
->builtins
);
505 sysmod
= _PyImport_FindExtension("sys", "sys");
506 if (bimod
!= NULL
&& sysmod
!= NULL
) {
507 interp
->sysdict
= PyModule_GetDict(sysmod
);
508 Py_INCREF(interp
->sysdict
);
509 PySys_SetPath(Py_GetPath());
510 PyDict_SetItemString(interp
->sysdict
, "modules",
512 _PyImportHooks_Init();
518 if (!PyErr_Occurred())
521 /* Oops, it didn't work. Undo it all. */
524 PyThreadState_Clear(tstate
);
525 PyThreadState_Swap(save_tstate
);
526 PyThreadState_Delete(tstate
);
527 PyInterpreterState_Delete(interp
);
532 /* Delete an interpreter and its last thread. This requires that the
533 given thread state is current, that the thread has no remaining
534 frames, and that it is its interpreter's only remaining thread.
535 It is a fatal error to violate these constraints.
537 (Py_Finalize() doesn't have these constraints -- it zaps
538 everything, regardless.)
545 Py_EndInterpreter(PyThreadState
*tstate
)
547 PyInterpreterState
*interp
= tstate
->interp
;
549 if (tstate
!= PyThreadState_GET())
550 Py_FatalError("Py_EndInterpreter: thread is not current");
551 if (tstate
->frame
!= NULL
)
552 Py_FatalError("Py_EndInterpreter: thread still has a frame");
553 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
554 Py_FatalError("Py_EndInterpreter: not the last thread");
557 PyInterpreterState_Clear(interp
);
558 PyThreadState_Swap(NULL
);
559 PyInterpreterState_Delete(interp
);
562 static char *progname
= "python";
565 Py_SetProgramName(char *pn
)
572 Py_GetProgramName(void)
577 static char *default_home
= NULL
;
580 Py_SetPythonHome(char *home
)
586 Py_GetPythonHome(void)
588 char *home
= default_home
;
589 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
590 home
= Py_GETENV("PYTHONHOME");
594 /* Create __main__ module */
600 m
= PyImport_AddModule("__main__");
602 Py_FatalError("can't create __main__ module");
603 d
= PyModule_GetDict(m
);
604 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
605 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
607 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
608 Py_FatalError("can't add __builtins__ to __main__");
613 /* Import the site module (not into __main__ though) */
619 m
= PyImport_ImportModule("site");
621 f
= PySys_GetObject("stderr");
622 if (Py_VerboseFlag
) {
624 "'import site' failed; traceback:\n", f
);
629 "'import site' failed; use -v for traceback\n", f
);
638 /* Parse input from a file and execute it */
641 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
642 PyCompilerFlags
*flags
)
644 if (filename
== NULL
)
646 if (Py_FdIsInteractive(fp
, filename
)) {
647 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
653 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
657 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
661 PyCompilerFlags local_flags
;
664 flags
= &local_flags
;
665 local_flags
.cf_flags
= 0;
667 v
= PySys_GetObject("ps1");
669 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
672 v
= PySys_GetObject("ps2");
674 PySys_SetObject("ps2", v
= PyString_FromString("... "));
678 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
680 fprintf(stderr
, "[%ld refs]\n", _Py_RefTotal
);
691 /* compute parser flags based on compiler flags */
692 #define PARSER_FLAGS(flags) \
693 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
694 PyPARSE_DONT_IMPLY_DEDENT : 0) \
695 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
696 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
699 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
701 PyObject
*m
, *d
, *v
, *w
;
704 char *ps1
= "", *ps2
= "";
707 v
= PySys_GetObject("ps1");
712 else if (PyString_Check(v
))
713 ps1
= PyString_AsString(v
);
715 w
= PySys_GetObject("ps2");
720 else if (PyString_Check(w
))
721 ps2
= PyString_AsString(w
);
723 arena
= PyArena_New();
724 mod
= PyParser_ASTFromFile(fp
, filename
,
725 Py_single_input
, ps1
, ps2
,
726 flags
, &errcode
, arena
);
731 if (errcode
== E_EOF
) {
738 m
= PyImport_AddModule("__main__");
743 d
= PyModule_GetDict(m
);
744 v
= run_mod(mod
, filename
, d
, d
, flags
, arena
);
756 /* Check whether a file maybe a pyc file: Look at the extension,
757 the file type, and, if we may close it, at the first few bytes. */
760 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
762 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
765 /* Only look into the file if we are allowed to close it, since
766 it then should also be seekable. */
768 /* Read only two bytes of the magic. If the file was opened in
769 text mode, the bytes 3 and 4 of the magic (\r\n) might not
770 be read as they are on disk. */
771 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
772 unsigned char buf
[2];
773 /* Mess: In case of -x, the stream is NOT at its start now,
774 and ungetc() was used to push back the first newline,
775 which makes the current stream position formally undefined,
776 and a x-platform nightmare.
777 Unfortunately, we have no direct way to know whether -x
778 was specified. So we use a terrible hack: if the current
779 stream position is not 0, we assume -x was specified, and
780 give up. Bug 132850 on SourceForge spells out the
781 hopelessness of trying anything else (fseek and ftell
782 don't work predictably x-platform for text-mode files).
785 if (ftell(fp
) == 0) {
786 if (fread(buf
, 1, 2, fp
) == 2 &&
787 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
797 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
798 PyCompilerFlags
*flags
)
803 m
= PyImport_AddModule("__main__");
806 d
= PyModule_GetDict(m
);
807 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
808 PyObject
*f
= PyString_FromString(filename
);
811 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
817 ext
= filename
+ strlen(filename
) - 4;
818 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
819 /* Try to run a pyc file. First, re-open in binary */
822 if ((fp
= fopen(filename
, "rb")) == NULL
) {
823 fprintf(stderr
, "python: Can't reopen .pyc file\n");
826 /* Turn on optimization if a .pyo file is given */
827 if (strcmp(ext
, ".pyo") == 0)
829 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
831 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
845 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
848 m
= PyImport_AddModule("__main__");
851 d
= PyModule_GetDict(m
);
852 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
864 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
865 int *lineno
, int *offset
, const char **text
)
870 /* old style errors */
871 if (PyTuple_Check(err
))
872 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
873 lineno
, offset
, text
);
875 /* new style errors. `err' is an instance */
877 if (! (v
= PyObject_GetAttrString(err
, "msg")))
881 if (!(v
= PyObject_GetAttrString(err
, "filename")))
885 else if (! (*filename
= PyString_AsString(v
)))
889 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
891 hold
= PyInt_AsLong(v
);
894 if (hold
< 0 && PyErr_Occurred())
898 if (!(v
= PyObject_GetAttrString(err
, "offset")))
905 hold
= PyInt_AsLong(v
);
908 if (hold
< 0 && PyErr_Occurred())
913 if (!(v
= PyObject_GetAttrString(err
, "text")))
917 else if (! (*text
= PyString_AsString(v
)))
934 print_error_text(PyObject
*f
, int offset
, const char *text
)
938 if (offset
> 0 && offset
== (int)strlen(text
))
941 nl
= strchr(text
, '\n');
942 if (nl
== NULL
|| nl
-text
>= offset
)
944 offset
-= (int)(nl
+1-text
);
947 while (*text
== ' ' || *text
== '\t') {
952 PyFile_WriteString(" ", f
);
953 PyFile_WriteString(text
, f
);
954 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
955 PyFile_WriteString("\n", f
);
958 PyFile_WriteString(" ", f
);
961 PyFile_WriteString(" ", f
);
964 PyFile_WriteString("^\n", f
);
968 handle_system_exit(void)
970 PyObject
*exception
, *value
, *tb
;
973 PyErr_Fetch(&exception
, &value
, &tb
);
977 if (value
== NULL
|| value
== Py_None
)
979 if (PyExceptionInstance_Check(value
)) {
980 /* The error code should be in the `code' attribute. */
981 PyObject
*code
= PyObject_GetAttrString(value
, "code");
985 if (value
== Py_None
)
988 /* If we failed to dig out the 'code' attribute,
989 just let the else clause below print the error. */
991 if (PyInt_Check(value
))
992 exitcode
= (int)PyInt_AsLong(value
);
994 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
995 PySys_WriteStderr("\n");
999 /* Restore and clear the exception info, in order to properly decref
1000 * the exception, value, and traceback. If we just exit instead,
1001 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1002 * some finalizers from running.
1004 PyErr_Restore(exception
, value
, tb
);
1011 PyErr_PrintEx(int set_sys_last_vars
)
1013 PyObject
*exception
, *v
, *tb
, *hook
;
1015 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1016 handle_system_exit();
1018 PyErr_Fetch(&exception
, &v
, &tb
);
1019 if (exception
== NULL
)
1021 PyErr_NormalizeException(&exception
, &v
, &tb
);
1022 if (exception
== NULL
)
1024 if (set_sys_last_vars
) {
1025 PySys_SetObject("last_type", exception
);
1026 PySys_SetObject("last_value", v
);
1027 PySys_SetObject("last_traceback", tb
);
1029 hook
= PySys_GetObject("excepthook");
1031 PyObject
*args
= PyTuple_Pack(3,
1032 exception
, v
? v
: Py_None
, tb
? tb
: Py_None
);
1033 PyObject
*result
= PyEval_CallObject(hook
, args
);
1034 if (result
== NULL
) {
1035 PyObject
*exception2
, *v2
, *tb2
;
1036 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1037 handle_system_exit();
1039 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1040 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1044 PySys_WriteStderr("Error in sys.excepthook:\n");
1045 PyErr_Display(exception2
, v2
, tb2
);
1046 PySys_WriteStderr("\nOriginal exception was:\n");
1047 PyErr_Display(exception
, v
, tb
);
1048 Py_XDECREF(exception2
);
1055 PySys_WriteStderr("sys.excepthook is missing\n");
1056 PyErr_Display(exception
, v
, tb
);
1058 Py_XDECREF(exception
);
1063 void PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1066 PyObject
*f
= PySys_GetObject("stderr");
1069 fprintf(stderr
, "lost sys.stderr\n");
1074 if (tb
&& tb
!= Py_None
)
1075 err
= PyTraceBack_Print(tb
, f
);
1077 PyObject_HasAttrString(value
, "print_file_and_line"))
1080 const char *filename
, *text
;
1082 if (!parse_syntax_error(value
, &message
, &filename
,
1083 &lineno
, &offset
, &text
))
1087 PyFile_WriteString(" File \"", f
);
1088 if (filename
== NULL
)
1089 PyFile_WriteString("<string>", f
);
1091 PyFile_WriteString(filename
, f
);
1092 PyFile_WriteString("\", line ", f
);
1093 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1094 PyFile_WriteString(buf
, f
);
1095 PyFile_WriteString("\n", f
);
1097 print_error_text(f
, offset
, text
);
1100 /* Can't be bothered to check all those
1101 PyFile_WriteString() calls */
1102 if (PyErr_Occurred())
1107 /* Don't do anything else */
1109 else if (PyExceptionClass_Check(exception
)) {
1110 char* className
= PyExceptionClass_Name(exception
);
1111 PyObject
* moduleName
=
1112 PyObject_GetAttrString(exception
, "__module__");
1114 if (moduleName
== NULL
)
1115 err
= PyFile_WriteString("<unknown>", f
);
1117 char* modstr
= PyString_AsString(moduleName
);
1118 Py_DECREF(moduleName
);
1119 if (modstr
&& strcmp(modstr
, "exceptions"))
1121 err
= PyFile_WriteString(modstr
, f
);
1122 err
+= PyFile_WriteString(".", f
);
1126 if (className
== NULL
)
1127 err
= PyFile_WriteString("<unknown>", f
);
1129 err
= PyFile_WriteString(className
, f
);
1133 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1134 if (err
== 0 && (value
!= Py_None
)) {
1135 PyObject
*s
= PyObject_Str(value
);
1136 /* only print colon if the str() of the
1137 object is not the empty string
1141 else if (!PyString_Check(s
) ||
1142 PyString_GET_SIZE(s
) != 0)
1143 err
= PyFile_WriteString(": ", f
);
1145 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1149 err
= PyFile_WriteString("\n", f
);
1152 /* If an error happened here, don't show it.
1153 XXX This is wrong, but too many callers rely on this behavior. */
1159 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
,
1160 PyObject
*locals
, PyCompilerFlags
*flags
)
1163 PyArena
*arena
= PyArena_New();
1164 mod_ty mod
= PyParser_ASTFromString(str
, "<string>", start
, flags
,
1166 ret
= run_err_mod(mod
, "<string>", globals
, locals
, flags
, arena
);
1167 PyArena_Free(arena
);
1172 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1173 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1176 PyArena
*arena
= PyArena_New();
1177 mod_ty mod
= PyParser_ASTFromFile(fp
, filename
, start
, 0, 0,
1178 flags
, NULL
, arena
);
1180 PyArena_Free(arena
);
1185 ret
= run_err_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1186 PyArena_Free(arena
);
1191 run_err_mod(mod_ty mod
, const char *filename
, PyObject
*globals
,
1192 PyObject
*locals
, PyCompilerFlags
*flags
, PyArena
*arena
)
1196 return run_mod(mod
, filename
, globals
, locals
, flags
, arena
);
1200 run_mod(mod_ty mod
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1201 PyCompilerFlags
*flags
, PyArena
*arena
)
1205 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1208 v
= PyEval_EvalCode(co
, globals
, locals
);
1214 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
,
1215 PyObject
*locals
, PyCompilerFlags
*flags
)
1220 long PyImport_GetMagicNumber(void);
1222 magic
= PyMarshal_ReadLongFromFile(fp
);
1223 if (magic
!= PyImport_GetMagicNumber()) {
1224 PyErr_SetString(PyExc_RuntimeError
,
1225 "Bad magic number in .pyc file");
1228 (void) PyMarshal_ReadLongFromFile(fp
);
1229 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1231 if (v
== NULL
|| !PyCode_Check(v
)) {
1233 PyErr_SetString(PyExc_RuntimeError
,
1234 "Bad code object in .pyc file");
1237 co
= (PyCodeObject
*)v
;
1238 v
= PyEval_EvalCode(co
, globals
, locals
);
1240 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1246 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1247 PyCompilerFlags
*flags
)
1250 PyArena
*arena
= PyArena_New();
1251 mod_ty mod
= PyParser_ASTFromString(str
, filename
, start
, flags
, arena
);
1253 PyArena_Free(arena
);
1256 if (flags
&& (flags
->cf_flags
& PyCF_ONLY_AST
)) {
1257 PyObject
*result
= PyAST_mod2obj(mod
);
1258 PyArena_Free(arena
);
1261 co
= PyAST_Compile(mod
, filename
, flags
, arena
);
1262 PyArena_Free(arena
);
1263 return (PyObject
*)co
;
1267 Py_SymtableString(const char *str
, const char *filename
, int start
)
1269 struct symtable
*st
;
1270 PyArena
*arena
= PyArena_New();
1271 mod_ty mod
= PyParser_ASTFromString(str
, filename
, start
, NULL
, arena
);
1273 PyArena_Free(arena
);
1276 st
= PySymtable_Build(mod
, filename
, 0);
1277 PyArena_Free(arena
);
1281 /* Preferred access to parser is through AST. */
1283 PyParser_ASTFromString(const char *s
, const char *filename
, int start
,
1284 PyCompilerFlags
*flags
, PyArena
*arena
)
1288 node
*n
= PyParser_ParseStringFlagsFilename(s
, filename
,
1289 &_PyParser_Grammar
, start
, &err
,
1290 PARSER_FLAGS(flags
));
1292 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1303 PyParser_ASTFromFile(FILE *fp
, const char *filename
, int start
, char *ps1
,
1304 char *ps2
, PyCompilerFlags
*flags
, int *errcode
,
1309 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1310 start
, ps1
, ps2
, &err
, PARSER_FLAGS(flags
));
1312 mod
= PyAST_FromNode(n
, flags
, filename
, arena
);
1319 *errcode
= err
.error
;
1324 /* Simplified interface to parsefile -- return node or set exception */
1327 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1330 node
*n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
1331 start
, NULL
, NULL
, &err
, flags
);
1338 /* Simplified interface to parsestring -- return node or set exception */
1341 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1344 node
*n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
,
1345 start
, &err
, flags
);
1352 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1353 int start
, int flags
)
1356 node
*n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1357 &_PyParser_Grammar
, start
, &err
, flags
);
1364 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1366 return PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
, 0);
1369 /* May want to move a more generalized form of this to parsetok.c or
1370 even parser modules. */
1373 PyParser_SetError(perrdetail
*err
)
1378 /* Set the error appropriate to the given input error code (see errcode.h) */
1381 err_input(perrdetail
*err
)
1383 PyObject
*v
, *w
, *errtype
;
1386 errtype
= PyExc_SyntaxError
;
1387 switch (err
->error
) {
1389 errtype
= PyExc_IndentationError
;
1390 if (err
->expected
== INDENT
)
1391 msg
= "expected an indented block";
1392 else if (err
->token
== INDENT
)
1393 msg
= "unexpected indent";
1394 else if (err
->token
== DEDENT
)
1395 msg
= "unexpected unindent";
1397 errtype
= PyExc_SyntaxError
;
1398 msg
= "invalid syntax";
1402 msg
= "invalid token";
1405 msg
= "EOF while scanning triple-quoted string";
1408 msg
= "EOL while scanning single-quoted string";
1411 if (!PyErr_Occurred())
1412 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1418 msg
= "unexpected EOF while parsing";
1421 errtype
= PyExc_TabError
;
1422 msg
= "inconsistent use of tabs and spaces in indentation";
1425 msg
= "expression too long";
1428 errtype
= PyExc_IndentationError
;
1429 msg
= "unindent does not match any outer indentation level";
1432 errtype
= PyExc_IndentationError
;
1433 msg
= "too many levels of indentation";
1436 PyObject
*type
, *value
, *tb
;
1437 PyErr_Fetch(&type
, &value
, &tb
);
1438 if (value
!= NULL
) {
1439 u
= PyObject_Str(value
);
1441 msg
= PyString_AsString(u
);
1445 msg
= "unknown decode error";
1452 msg
= "unexpected character after line continuation character";
1455 fprintf(stderr
, "error=%d\n", err
->error
);
1456 msg
= "unknown parsing error";
1459 v
= Py_BuildValue("(ziiz)", err
->filename
,
1460 err
->lineno
, err
->offset
, err
->text
);
1461 if (err
->text
!= NULL
) {
1462 PyMem_DEL(err
->text
);
1467 w
= Py_BuildValue("(sO)", msg
, v
);
1470 PyErr_SetObject(errtype
, w
);
1474 /* Print fatal error message and abort */
1477 Py_FatalError(const char *msg
)
1479 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1481 OutputDebugString("Fatal Python error: ");
1482 OutputDebugString(msg
);
1483 OutputDebugString("\n");
1487 #endif /* MS_WINDOWS */
1491 /* Clean up and exit */
1494 #include "pythread.h"
1497 #define NEXITFUNCS 32
1498 static void (*exitfuncs
[NEXITFUNCS
])(void);
1499 static int nexitfuncs
= 0;
1501 int Py_AtExit(void (*func
)(void))
1503 if (nexitfuncs
>= NEXITFUNCS
)
1505 exitfuncs
[nexitfuncs
++] = func
;
1510 call_sys_exitfunc(void)
1512 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1516 Py_INCREF(exitfunc
);
1517 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1518 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1520 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1521 PySys_WriteStderr("Error in sys.exitfunc:\n");
1525 Py_DECREF(exitfunc
);
1533 call_ll_exitfuncs(void)
1535 while (nexitfuncs
> 0)
1536 (*exitfuncs
[--nexitfuncs
])();
1554 PyOS_setsig(SIGPIPE
, SIG_IGN
);
1557 PyOS_setsig(SIGXFZ
, SIG_IGN
);
1560 PyOS_setsig(SIGXFSZ
, SIG_IGN
);
1562 PyOS_InitInterrupts(); /* May imply initsignal() */
1567 * The file descriptor fd is considered ``interactive'' if either
1568 * a) isatty(fd) is TRUE, or
1569 * b) the -i flag was given, and the filename associated with
1570 * the descriptor is NULL or "<stdin>" or "???".
1573 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1575 if (isatty((int)fileno(fp
)))
1577 if (!Py_InteractiveFlag
)
1579 return (filename
== NULL
) ||
1580 (strcmp(filename
, "<stdin>") == 0) ||
1581 (strcmp(filename
, "???") == 0);
1585 #if defined(USE_STACKCHECK)
1586 #if defined(WIN32) && defined(_MSC_VER)
1588 /* Stack checking for Microsoft C */
1594 * Return non-zero when we run out of memory on the stack; zero otherwise.
1597 PyOS_CheckStack(void)
1600 /* alloca throws a stack overflow exception if there's
1601 not enough space left on the stack */
1602 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1604 } __except (EXCEPTION_EXECUTE_HANDLER
) {
1605 /* just ignore all errors */
1610 #endif /* WIN32 && _MSC_VER */
1612 /* Alternate implementations can be added here... */
1614 #endif /* USE_STACKCHECK */
1617 /* Wrappers around sigaction() or signal(). */
1620 PyOS_getsig(int sig
)
1622 #ifdef HAVE_SIGACTION
1623 struct sigaction context
;
1624 if (sigaction(sig
, NULL
, &context
) == -1)
1626 return context
.sa_handler
;
1628 PyOS_sighandler_t handler
;
1629 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1630 #if defined(_MSC_VER) && _MSC_VER >= 1400
1632 /* Only these signals are valid */
1641 /* Don't call signal() with other values or it will assert */
1645 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1646 handler
= signal(sig
, SIG_IGN
);
1647 if (handler
!= SIG_ERR
)
1648 signal(sig
, handler
);
1654 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1656 #ifdef HAVE_SIGACTION
1657 struct sigaction context
, ocontext
;
1658 context
.sa_handler
= handler
;
1659 sigemptyset(&context
.sa_mask
);
1660 context
.sa_flags
= 0;
1661 if (sigaction(sig
, &context
, &ocontext
) == -1)
1663 return ocontext
.sa_handler
;
1665 PyOS_sighandler_t oldhandler
;
1666 oldhandler
= signal(sig
, handler
);
1667 #ifdef HAVE_SIGINTERRUPT
1668 siginterrupt(sig
, 1);
1674 /* Deprecated C API functions still provided for binary compatiblity */
1676 #undef PyParser_SimpleParseFile
1677 #undef PyParser_SimpleParseString
1680 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1682 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1686 PyParser_SimpleParseString(const char *str
, int start
)
1688 return PyParser_SimpleParseStringFlags(str
, start
, 0);