2 /* Python interpreter top-level routines, including init/exit */
20 #ifdef HAVE_LANGINFO_H
33 extern char *Py_GetPath(void);
35 extern grammar _PyParser_Grammar
; /* From graminit.c */
38 static void initmain(void);
39 static void initsite(void);
40 static PyObject
*run_err_node(node
*, const char *, PyObject
*, PyObject
*,
42 static PyObject
*run_node(node
*, const char *, PyObject
*, PyObject
*,
44 static PyObject
*run_pyc_file(FILE *, const char *, PyObject
*, PyObject
*,
46 static void err_input(perrdetail
*);
47 static void initsigs(void);
48 static void call_sys_exitfunc(void);
49 static void call_ll_exitfuncs(void);
50 extern void _PyUnicode_Init(void);
51 extern void _PyUnicode_Fini(void);
54 extern void _PyGILState_Init(PyInterpreterState
*, PyThreadState
*);
55 extern void _PyGILState_Fini(void);
56 #endif /* WITH_THREAD */
58 int Py_DebugFlag
; /* Needed by parser.c */
59 int Py_VerboseFlag
; /* Needed by import.c */
60 int Py_InteractiveFlag
; /* Needed by Py_FdIsInteractive() below */
61 int Py_NoSiteFlag
; /* Suppress 'import site' */
62 int Py_UseClassExceptionsFlag
= 1; /* Needed by bltinmodule.c: deprecated */
63 int Py_FrozenFlag
; /* Needed by getpath.c */
64 int Py_UnicodeFlag
= 0; /* Needed by compile.c */
65 int Py_IgnoreEnvironmentFlag
; /* e.g. PYTHONPATH, PYTHONHOME */
66 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
67 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
68 true divisions (which they will be in 2.3). */
71 /* Reference to 'warnings' module, to avoid importing it
72 on the fly when the import lock may be held. See 683658/771097
74 static PyObject
*warnings_module
= NULL
;
76 /* Returns a borrowed reference to the 'warnings' module, or NULL.
77 If the module is returned, it is guaranteed to have been obtained
78 without acquiring the import lock
80 PyObject
*PyModule_GetWarningsModule(void)
82 PyObject
*typ
, *val
, *tb
;
83 PyObject
*all_modules
;
84 /* If we managed to get the module at init time, just use it */
86 return warnings_module
;
87 /* If it wasn't available at init time, it may be available
88 now in sys.modules (common scenario is frozen apps: import
89 at init time fails, but the frozen init code sets up sys.path
90 correctly, then does an implicit import of warnings for us
92 /* Save and restore any exceptions */
93 PyErr_Fetch(&typ
, &val
, &tb
);
95 all_modules
= PySys_GetObject("modules");
97 warnings_module
= PyDict_GetItemString(all_modules
, "warnings");
98 /* We keep a ref in the global */
99 Py_XINCREF(warnings_module
);
101 PyErr_Restore(typ
, val
, tb
);
102 return warnings_module
;
105 static int initialized
= 0;
107 /* API to access the initialized flag -- useful for esoteric use */
110 Py_IsInitialized(void)
115 /* Global initializations. Can be undone by Py_Finalize(). Don't
116 call this twice without an intervening Py_Finalize() call. When
117 initializations fail, a fatal error is issued and the function does
118 not return. On return, the first thread and interpreter state have
121 Locking: you must hold the interpreter lock while calling this.
122 (If the lock has not yet been initialized, that's equivalent to
123 having the lock, but you cannot use multiple threads.)
128 add_flag(int flag
, const char *envs
)
130 int env
= atoi(envs
);
141 PyInterpreterState
*interp
;
142 PyThreadState
*tstate
;
143 PyObject
*bimod
, *sysmod
;
145 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
148 PyObject
*sys_stream
, *sys_isatty
;
150 extern void _Py_ReadyTypes(void);
156 if ((p
= Py_GETENV("PYTHONDEBUG")) && *p
!= '\0')
157 Py_DebugFlag
= add_flag(Py_DebugFlag
, p
);
158 if ((p
= Py_GETENV("PYTHONVERBOSE")) && *p
!= '\0')
159 Py_VerboseFlag
= add_flag(Py_VerboseFlag
, p
);
160 if ((p
= Py_GETENV("PYTHONOPTIMIZE")) && *p
!= '\0')
161 Py_OptimizeFlag
= add_flag(Py_OptimizeFlag
, p
);
163 interp
= PyInterpreterState_New();
165 Py_FatalError("Py_Initialize: can't make first interpreter");
167 tstate
= PyThreadState_New(interp
);
169 Py_FatalError("Py_Initialize: can't make first thread");
170 (void) PyThreadState_Swap(tstate
);
174 if (!_PyFrame_Init())
175 Py_FatalError("Py_Initialize: can't init frames");
178 Py_FatalError("Py_Initialize: can't init ints");
180 interp
->modules
= PyDict_New();
181 if (interp
->modules
== NULL
)
182 Py_FatalError("Py_Initialize: can't make modules dictionary");
184 #ifdef Py_USING_UNICODE
185 /* Init Unicode implementation; relies on the codec registry */
189 bimod
= _PyBuiltin_Init();
191 Py_FatalError("Py_Initialize: can't initialize __builtin__");
192 interp
->builtins
= PyModule_GetDict(bimod
);
193 Py_INCREF(interp
->builtins
);
195 sysmod
= _PySys_Init();
197 Py_FatalError("Py_Initialize: can't initialize sys");
198 interp
->sysdict
= PyModule_GetDict(sysmod
);
199 Py_INCREF(interp
->sysdict
);
200 _PyImport_FixupExtension("sys", "sys");
201 PySys_SetPath(Py_GetPath());
202 PyDict_SetItemString(interp
->sysdict
, "modules",
207 /* initialize builtin exceptions */
209 _PyImport_FixupExtension("exceptions", "exceptions");
211 /* phase 2 of builtins */
212 _PyImport_FixupExtension("__builtin__", "__builtin__");
214 _PyImportHooks_Init();
216 initsigs(); /* Signal handling stuff, including initintr() */
218 initmain(); /* Module __main__ */
220 initsite(); /* Module site */
222 /* auto-thread-state API, if available */
224 _PyGILState_Init(interp
, tstate
);
225 #endif /* WITH_THREAD */
227 warnings_module
= PyImport_ImportModule("warnings");
228 if (!warnings_module
)
231 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
232 /* On Unix, set the file system encoding according to the
233 user's preference, if the CODESET names a well-known
234 Python codec, and Py_FileSystemDefaultEncoding isn't
235 initialized by other means. Also set the encoding of
236 stdin and stdout if these are terminals. */
238 saved_locale
= strdup(setlocale(LC_CTYPE
, NULL
));
239 setlocale(LC_CTYPE
, "");
240 codeset
= nl_langinfo(CODESET
);
241 if (codeset
&& *codeset
) {
242 PyObject
*enc
= PyCodec_Encoder(codeset
);
244 codeset
= strdup(codeset
);
252 setlocale(LC_CTYPE
, saved_locale
);
256 sys_stream
= PySys_GetObject("stdin");
257 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
260 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
261 if (!PyFile_SetEncoding(sys_stream
, codeset
))
262 Py_FatalError("Cannot set codeset of stdin");
264 Py_XDECREF(sys_isatty
);
266 sys_stream
= PySys_GetObject("stdout");
267 sys_isatty
= PyObject_CallMethod(sys_stream
, "isatty", "");
270 if(sys_isatty
&& PyObject_IsTrue(sys_isatty
)) {
271 if (!PyFile_SetEncoding(sys_stream
, codeset
))
272 Py_FatalError("Cannot set codeset of stdout");
274 Py_XDECREF(sys_isatty
);
276 if (!Py_FileSystemDefaultEncoding
)
277 Py_FileSystemDefaultEncoding
= codeset
;
285 extern void dump_counts(void);
288 /* Undo the effect of Py_Initialize().
290 Beware: if multiple interpreter and/or thread states exist, these
291 are not wiped out; only the current thread and interpreter state
292 are deleted. But since everything else is deleted, those other
293 interpreter and thread states should no longer be used.
295 (XXX We should do better, e.g. wipe out all interpreters and
305 PyInterpreterState
*interp
;
306 PyThreadState
*tstate
;
311 /* The interpreter is still entirely intact at this point, and the
312 * exit funcs may be relying on that. In particular, if some thread
313 * or exit func is still waiting to do an import, the import machinery
314 * expects Py_IsInitialized() to return true. So don't say the
315 * interpreter is uninitialized until after the exit funcs have run.
316 * Note that Threading.py uses an exit func to do a join on all the
317 * threads created thru it, so this also protects pending imports in
318 * the threads created via Threading.
323 /* Get current thread state and interpreter pointer */
324 tstate
= PyThreadState_Get();
325 interp
= tstate
->interp
;
327 /* Disable signal handling */
328 PyOS_FiniInterrupts();
330 /* drop module references we saved */
331 Py_XDECREF(warnings_module
);
332 warnings_module
= NULL
;
334 /* Collect garbage. This may call finalizers; it's nice to call these
335 * before all modules are destroyed.
336 * XXX If a __del__ or weakref callback is triggered here, and tries to
337 * XXX import a module, bad things can happen, because Python no
338 * XXX longer believes it's initialized.
339 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
340 * XXX is easy to provoke that way. I've also seen, e.g.,
341 * XXX Exception exceptions.ImportError: 'No module named sha'
342 * XXX in <function callback at 0x008F5718> ignored
343 * XXX but I'm unclear on exactly how that one happens. In any case,
344 * XXX I haven't seen a real-life report of either of these.
348 /* Destroy all modules */
351 /* Collect final garbage. This disposes of cycles created by
352 * new-style class definitions, for example.
353 * XXX This is disabled because it caused too many problems. If
354 * XXX a __del__ or weakref callback triggers here, Python code has
355 * XXX a hard time running, because even the sys module has been
356 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
357 * XXX One symptom is a sequence of information-free messages
358 * XXX coming from threads (if a __del__ or callback is invoked,
359 * XXX other threads can execute too, and any exception they encounter
360 * XXX triggers a comedy of errors as subsystem after subsystem
361 * XXX fails to find what it *expects* to find in sys to help report
362 * XXX the exception and consequent unexpected failures). I've also
363 * XXX seen segfaults then, after adding print statements to the
364 * XXX Python code getting called.
370 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
373 /* Debugging stuff */
379 fprintf(stderr
, "[%ld refs]\n", _Py_RefTotal
);
383 /* Display all objects still alive -- this can invoke arbitrary
384 * __repr__ overrides, so requires a mostly-intact interpreter.
385 * Alas, a lot of stuff may still be alive now that will be cleaned
388 if (Py_GETENV("PYTHONDUMPREFS"))
389 _Py_PrintReferences(stderr
);
390 #endif /* Py_TRACE_REFS */
392 /* Now we decref the exception classes. After this point nothing
393 can raise an exception. That's okay, because each Fini() method
394 below has been checked to make sure no exceptions are ever
399 /* Cleanup auto-thread-state */
402 #endif /* WITH_THREAD */
404 /* Clear interpreter state */
405 PyInterpreterState_Clear(interp
);
407 /* Delete current thread */
408 PyThreadState_Swap(NULL
);
409 PyInterpreterState_Delete(interp
);
411 /* Sundry finalizers */
420 #ifdef Py_USING_UNICODE
421 /* Cleanup Unicode implementation */
425 /* XXX Still allocated:
426 - various static ad-hoc pointers to interned strings
427 - int and float free list blocks
428 - whatever various modules and libraries allocate
431 PyGrammar_RemoveAccelerators(&_PyParser_Grammar
);
434 /* Display addresses (& refcnts) of all objects still alive.
435 * An address can be used to find the repr of the object, printed
436 * above by _Py_PrintReferences.
438 if (Py_GETENV("PYTHONDUMPREFS"))
439 _Py_PrintReferenceAddresses(stderr
);
440 #endif /* Py_TRACE_REFS */
441 #ifdef PYMALLOC_DEBUG
442 if (Py_GETENV("PYTHONMALLOCSTATS"))
443 _PyObject_DebugMallocStats();
449 /* Create and initialize a new interpreter and thread, and return the
450 new thread. This requires that Py_Initialize() has been called
453 Unsuccessful initialization yields a NULL pointer. Note that *no*
454 exception information is available even in this case -- the
455 exception information is held in the thread, and there is no
463 Py_NewInterpreter(void)
465 PyInterpreterState
*interp
;
466 PyThreadState
*tstate
, *save_tstate
;
467 PyObject
*bimod
, *sysmod
;
470 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
472 interp
= PyInterpreterState_New();
476 tstate
= PyThreadState_New(interp
);
477 if (tstate
== NULL
) {
478 PyInterpreterState_Delete(interp
);
482 save_tstate
= PyThreadState_Swap(tstate
);
484 /* XXX The following is lax in error checking */
486 interp
->modules
= PyDict_New();
488 bimod
= _PyImport_FindExtension("__builtin__", "__builtin__");
490 interp
->builtins
= PyModule_GetDict(bimod
);
491 Py_INCREF(interp
->builtins
);
493 sysmod
= _PyImport_FindExtension("sys", "sys");
494 if (bimod
!= NULL
&& sysmod
!= NULL
) {
495 interp
->sysdict
= PyModule_GetDict(sysmod
);
496 Py_INCREF(interp
->sysdict
);
497 PySys_SetPath(Py_GetPath());
498 PyDict_SetItemString(interp
->sysdict
, "modules",
500 _PyImportHooks_Init();
506 if (!PyErr_Occurred())
509 /* Oops, it didn't work. Undo it all. */
512 PyThreadState_Clear(tstate
);
513 PyThreadState_Swap(save_tstate
);
514 PyThreadState_Delete(tstate
);
515 PyInterpreterState_Delete(interp
);
520 /* Delete an interpreter and its last thread. This requires that the
521 given thread state is current, that the thread has no remaining
522 frames, and that it is its interpreter's only remaining thread.
523 It is a fatal error to violate these constraints.
525 (Py_Finalize() doesn't have these constraints -- it zaps
526 everything, regardless.)
533 Py_EndInterpreter(PyThreadState
*tstate
)
535 PyInterpreterState
*interp
= tstate
->interp
;
537 if (tstate
!= PyThreadState_Get())
538 Py_FatalError("Py_EndInterpreter: thread is not current");
539 if (tstate
->frame
!= NULL
)
540 Py_FatalError("Py_EndInterpreter: thread still has a frame");
541 if (tstate
!= interp
->tstate_head
|| tstate
->next
!= NULL
)
542 Py_FatalError("Py_EndInterpreter: not the last thread");
545 PyInterpreterState_Clear(interp
);
546 PyThreadState_Swap(NULL
);
547 PyInterpreterState_Delete(interp
);
550 static char *progname
= "python";
553 Py_SetProgramName(char *pn
)
560 Py_GetProgramName(void)
565 static char *default_home
= NULL
;
568 Py_SetPythonHome(char *home
)
574 Py_GetPythonHome(void)
576 char *home
= default_home
;
577 if (home
== NULL
&& !Py_IgnoreEnvironmentFlag
)
578 home
= Py_GETENV("PYTHONHOME");
582 /* Create __main__ module */
588 m
= PyImport_AddModule("__main__");
590 Py_FatalError("can't create __main__ module");
591 d
= PyModule_GetDict(m
);
592 if (PyDict_GetItemString(d
, "__builtins__") == NULL
) {
593 PyObject
*bimod
= PyImport_ImportModule("__builtin__");
595 PyDict_SetItemString(d
, "__builtins__", bimod
) != 0)
596 Py_FatalError("can't add __builtins__ to __main__");
601 /* Import the site module (not into __main__ though) */
607 m
= PyImport_ImportModule("site");
609 f
= PySys_GetObject("stderr");
610 if (Py_VerboseFlag
) {
612 "'import site' failed; traceback:\n", f
);
617 "'import site' failed; use -v for traceback\n", f
);
626 /* Parse input from a file and execute it */
629 PyRun_AnyFile(FILE *fp
, const char *filename
)
631 return PyRun_AnyFileExFlags(fp
, filename
, 0, NULL
);
635 PyRun_AnyFileFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
637 return PyRun_AnyFileExFlags(fp
, filename
, 0, flags
);
641 PyRun_AnyFileEx(FILE *fp
, const char *filename
, int closeit
)
643 return PyRun_AnyFileExFlags(fp
, filename
, closeit
, NULL
);
647 PyRun_AnyFileExFlags(FILE *fp
, const char *filename
, int closeit
,
648 PyCompilerFlags
*flags
)
650 if (filename
== NULL
)
652 if (Py_FdIsInteractive(fp
, filename
)) {
653 int err
= PyRun_InteractiveLoopFlags(fp
, filename
, flags
);
659 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, flags
);
663 PyRun_InteractiveLoop(FILE *fp
, const char *filename
)
665 return PyRun_InteractiveLoopFlags(fp
, filename
, NULL
);
669 PyRun_InteractiveLoopFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
673 PyCompilerFlags local_flags
;
676 flags
= &local_flags
;
677 local_flags
.cf_flags
= 0;
679 v
= PySys_GetObject("ps1");
681 PySys_SetObject("ps1", v
= PyString_FromString(">>> "));
684 v
= PySys_GetObject("ps2");
686 PySys_SetObject("ps2", v
= PyString_FromString("... "));
690 ret
= PyRun_InteractiveOneFlags(fp
, filename
, flags
);
692 fprintf(stderr
, "[%ld refs]\n", _Py_RefTotal
);
704 PyRun_InteractiveOne(FILE *fp
, const char *filename
)
706 return PyRun_InteractiveOneFlags(fp
, filename
, NULL
);
709 /* compute parser flags based on compiler flags */
710 #define PARSER_FLAGS(flags) \
711 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
712 PyPARSE_DONT_IMPLY_DEDENT : 0)
715 PyRun_InteractiveOneFlags(FILE *fp
, const char *filename
, PyCompilerFlags
*flags
)
717 PyObject
*m
, *d
, *v
, *w
;
720 char *ps1
= "", *ps2
= "";
722 v
= PySys_GetObject("ps1");
727 else if (PyString_Check(v
))
728 ps1
= PyString_AsString(v
);
730 w
= PySys_GetObject("ps2");
735 else if (PyString_Check(w
))
736 ps2
= PyString_AsString(w
);
738 n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
,
739 Py_single_input
, ps1
, ps2
, &err
,
740 PARSER_FLAGS(flags
));
744 if (err
.error
== E_EOF
) {
753 m
= PyImport_AddModule("__main__");
756 d
= PyModule_GetDict(m
);
757 v
= run_node(n
, filename
, d
, d
, flags
);
769 PyRun_SimpleFile(FILE *fp
, const char *filename
)
771 return PyRun_SimpleFileEx(fp
, filename
, 0);
774 /* Check whether a file maybe a pyc file: Look at the extension,
775 the file type, and, if we may close it, at the first few bytes. */
778 maybe_pyc_file(FILE *fp
, const char* filename
, const char* ext
, int closeit
)
780 if (strcmp(ext
, ".pyc") == 0 || strcmp(ext
, ".pyo") == 0)
784 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
785 if (PyMac_getfiletype((char *)filename
) == 'PYC '
786 || PyMac_getfiletype((char *)filename
) == 'APPL')
788 #endif /* macintosh */
790 /* Only look into the file if we are allowed to close it, since
791 it then should also be seekable. */
793 /* Read only two bytes of the magic. If the file was opened in
794 text mode, the bytes 3 and 4 of the magic (\r\n) might not
795 be read as they are on disk. */
796 unsigned int halfmagic
= PyImport_GetMagicNumber() & 0xFFFF;
797 unsigned char buf
[2];
798 /* Mess: In case of -x, the stream is NOT at its start now,
799 and ungetc() was used to push back the first newline,
800 which makes the current stream position formally undefined,
801 and a x-platform nightmare.
802 Unfortunately, we have no direct way to know whether -x
803 was specified. So we use a terrible hack: if the current
804 stream position is not 0, we assume -x was specified, and
805 give up. Bug 132850 on SourceForge spells out the
806 hopelessness of trying anything else (fseek and ftell
807 don't work predictably x-platform for text-mode files).
810 if (ftell(fp
) == 0) {
811 if (fread(buf
, 1, 2, fp
) == 2 &&
812 ((unsigned int)buf
[1]<<8 | buf
[0]) == halfmagic
)
822 PyRun_SimpleFileEx(FILE *fp
, const char *filename
, int closeit
)
824 return PyRun_SimpleFileExFlags(fp
, filename
, closeit
, NULL
);
828 PyRun_SimpleFileExFlags(FILE *fp
, const char *filename
, int closeit
,
829 PyCompilerFlags
*flags
)
834 m
= PyImport_AddModule("__main__");
837 d
= PyModule_GetDict(m
);
838 if (PyDict_GetItemString(d
, "__file__") == NULL
) {
839 PyObject
*f
= PyString_FromString(filename
);
842 if (PyDict_SetItemString(d
, "__file__", f
) < 0) {
848 ext
= filename
+ strlen(filename
) - 4;
849 if (maybe_pyc_file(fp
, filename
, ext
, closeit
)) {
850 /* Try to run a pyc file. First, re-open in binary */
853 if ((fp
= fopen(filename
, "rb")) == NULL
) {
854 fprintf(stderr
, "python: Can't reopen .pyc file\n");
857 /* Turn on optimization if a .pyo file is given */
858 if (strcmp(ext
, ".pyo") == 0)
860 v
= run_pyc_file(fp
, filename
, d
, d
, flags
);
862 v
= PyRun_FileExFlags(fp
, filename
, Py_file_input
, d
, d
,
876 PyRun_SimpleString(const char *command
)
878 return PyRun_SimpleStringFlags(command
, NULL
);
882 PyRun_SimpleStringFlags(const char *command
, PyCompilerFlags
*flags
)
885 m
= PyImport_AddModule("__main__");
888 d
= PyModule_GetDict(m
);
889 v
= PyRun_StringFlags(command
, Py_file_input
, d
, d
, flags
);
901 parse_syntax_error(PyObject
*err
, PyObject
**message
, const char **filename
,
902 int *lineno
, int *offset
, const char **text
)
907 /* old style errors */
908 if (PyTuple_Check(err
))
909 return PyArg_ParseTuple(err
, "O(ziiz)", message
, filename
,
910 lineno
, offset
, text
);
912 /* new style errors. `err' is an instance */
914 if (! (v
= PyObject_GetAttrString(err
, "msg")))
918 if (!(v
= PyObject_GetAttrString(err
, "filename")))
922 else if (! (*filename
= PyString_AsString(v
)))
926 if (!(v
= PyObject_GetAttrString(err
, "lineno")))
928 hold
= PyInt_AsLong(v
);
931 if (hold
< 0 && PyErr_Occurred())
935 if (!(v
= PyObject_GetAttrString(err
, "offset")))
942 hold
= PyInt_AsLong(v
);
945 if (hold
< 0 && PyErr_Occurred())
950 if (!(v
= PyObject_GetAttrString(err
, "text")))
954 else if (! (*text
= PyString_AsString(v
)))
971 print_error_text(PyObject
*f
, int offset
, const char *text
)
975 if (offset
> 0 && offset
== (int)strlen(text
))
978 nl
= strchr(text
, '\n');
979 if (nl
== NULL
|| nl
-text
>= offset
)
981 offset
-= (nl
+1-text
);
984 while (*text
== ' ' || *text
== '\t') {
989 PyFile_WriteString(" ", f
);
990 PyFile_WriteString(text
, f
);
991 if (*text
== '\0' || text
[strlen(text
)-1] != '\n')
992 PyFile_WriteString("\n", f
);
995 PyFile_WriteString(" ", f
);
998 PyFile_WriteString(" ", f
);
1001 PyFile_WriteString("^\n", f
);
1005 handle_system_exit(void)
1007 PyObject
*exception
, *value
, *tb
;
1010 PyErr_Fetch(&exception
, &value
, &tb
);
1014 if (value
== NULL
|| value
== Py_None
)
1016 if (PyInstance_Check(value
)) {
1017 /* The error code should be in the `code' attribute. */
1018 PyObject
*code
= PyObject_GetAttrString(value
, "code");
1022 if (value
== Py_None
)
1025 /* If we failed to dig out the 'code' attribute,
1026 just let the else clause below print the error. */
1028 if (PyInt_Check(value
))
1029 exitcode
= (int)PyInt_AsLong(value
);
1031 PyObject_Print(value
, stderr
, Py_PRINT_RAW
);
1032 PySys_WriteStderr("\n");
1036 /* Restore and clear the exception info, in order to properly decref
1037 * the exception, value, and traceback. If we just exit instead,
1038 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1039 * some finalizers from running.
1041 PyErr_Restore(exception
, value
, tb
);
1048 PyErr_PrintEx(int set_sys_last_vars
)
1050 PyObject
*exception
, *v
, *tb
, *hook
;
1052 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1053 handle_system_exit();
1055 PyErr_Fetch(&exception
, &v
, &tb
);
1056 PyErr_NormalizeException(&exception
, &v
, &tb
);
1057 if (exception
== NULL
)
1059 if (set_sys_last_vars
) {
1060 PySys_SetObject("last_type", exception
);
1061 PySys_SetObject("last_value", v
);
1062 PySys_SetObject("last_traceback", tb
);
1064 hook
= PySys_GetObject("excepthook");
1066 PyObject
*args
= Py_BuildValue("(OOO)",
1067 exception
, v
? v
: Py_None
, tb
? tb
: Py_None
);
1068 PyObject
*result
= PyEval_CallObject(hook
, args
);
1069 if (result
== NULL
) {
1070 PyObject
*exception2
, *v2
, *tb2
;
1071 if (PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1072 handle_system_exit();
1074 PyErr_Fetch(&exception2
, &v2
, &tb2
);
1075 PyErr_NormalizeException(&exception2
, &v2
, &tb2
);
1079 PySys_WriteStderr("Error in sys.excepthook:\n");
1080 PyErr_Display(exception2
, v2
, tb2
);
1081 PySys_WriteStderr("\nOriginal exception was:\n");
1082 PyErr_Display(exception
, v
, tb
);
1083 Py_XDECREF(exception2
);
1090 PySys_WriteStderr("sys.excepthook is missing\n");
1091 PyErr_Display(exception
, v
, tb
);
1093 Py_XDECREF(exception
);
1098 void PyErr_Display(PyObject
*exception
, PyObject
*value
, PyObject
*tb
)
1101 PyObject
*f
= PySys_GetObject("stderr");
1104 fprintf(stderr
, "lost sys.stderr\n");
1109 if (tb
&& tb
!= Py_None
)
1110 err
= PyTraceBack_Print(tb
, f
);
1112 PyObject_HasAttrString(value
, "print_file_and_line"))
1115 const char *filename
, *text
;
1117 if (!parse_syntax_error(value
, &message
, &filename
,
1118 &lineno
, &offset
, &text
))
1122 PyFile_WriteString(" File \"", f
);
1123 if (filename
== NULL
)
1124 PyFile_WriteString("<string>", f
);
1126 PyFile_WriteString(filename
, f
);
1127 PyFile_WriteString("\", line ", f
);
1128 PyOS_snprintf(buf
, sizeof(buf
), "%d", lineno
);
1129 PyFile_WriteString(buf
, f
);
1130 PyFile_WriteString("\n", f
);
1132 print_error_text(f
, offset
, text
);
1135 /* Can't be bothered to check all those
1136 PyFile_WriteString() calls */
1137 if (PyErr_Occurred())
1142 /* Don't do anything else */
1144 else if (PyClass_Check(exception
)) {
1145 PyClassObject
* exc
= (PyClassObject
*)exception
;
1146 PyObject
* className
= exc
->cl_name
;
1147 PyObject
* moduleName
=
1148 PyDict_GetItemString(exc
->cl_dict
, "__module__");
1150 if (moduleName
== NULL
)
1151 err
= PyFile_WriteString("<unknown>", f
);
1153 char* modstr
= PyString_AsString(moduleName
);
1154 if (modstr
&& strcmp(modstr
, "exceptions"))
1156 err
= PyFile_WriteString(modstr
, f
);
1157 err
+= PyFile_WriteString(".", f
);
1161 if (className
== NULL
)
1162 err
= PyFile_WriteString("<unknown>", f
);
1164 err
= PyFile_WriteObject(className
, f
,
1169 err
= PyFile_WriteObject(exception
, f
, Py_PRINT_RAW
);
1171 if (value
!= Py_None
) {
1172 PyObject
*s
= PyObject_Str(value
);
1173 /* only print colon if the str() of the
1174 object is not the empty string
1178 else if (!PyString_Check(s
) ||
1179 PyString_GET_SIZE(s
) != 0)
1180 err
= PyFile_WriteString(": ", f
);
1182 err
= PyFile_WriteObject(s
, f
, Py_PRINT_RAW
);
1187 err
= PyFile_WriteString("\n", f
);
1190 /* If an error happened here, don't show it.
1191 XXX This is wrong, but too many callers rely on this behavior. */
1197 PyRun_String(const char *str
, int start
, PyObject
*globals
, PyObject
*locals
)
1199 return run_err_node(PyParser_SimpleParseString(str
, start
),
1200 "<string>", globals
, locals
, NULL
);
1204 PyRun_File(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1207 return PyRun_FileEx(fp
, filename
, start
, globals
, locals
, 0);
1211 PyRun_FileEx(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1212 PyObject
*locals
, int closeit
)
1214 node
*n
= PyParser_SimpleParseFile(fp
, filename
, start
);
1217 return run_err_node(n
, filename
, globals
, locals
, NULL
);
1221 PyRun_StringFlags(const char *str
, int start
, PyObject
*globals
, PyObject
*locals
,
1222 PyCompilerFlags
*flags
)
1224 return run_err_node(PyParser_SimpleParseStringFlags(
1225 str
, start
, PARSER_FLAGS(flags
)),
1226 "<string>", globals
, locals
, flags
);
1230 PyRun_FileFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1231 PyObject
*locals
, PyCompilerFlags
*flags
)
1233 return PyRun_FileExFlags(fp
, filename
, start
, globals
, locals
, 0,
1238 PyRun_FileExFlags(FILE *fp
, const char *filename
, int start
, PyObject
*globals
,
1239 PyObject
*locals
, int closeit
, PyCompilerFlags
*flags
)
1241 node
*n
= PyParser_SimpleParseFileFlags(fp
, filename
, start
,
1242 PARSER_FLAGS(flags
));
1245 return run_err_node(n
, filename
, globals
, locals
, flags
);
1249 run_err_node(node
*n
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1250 PyCompilerFlags
*flags
)
1254 return run_node(n
, filename
, globals
, locals
, flags
);
1258 run_node(node
*n
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1259 PyCompilerFlags
*flags
)
1263 co
= PyNode_CompileFlags(n
, filename
, flags
);
1267 v
= PyEval_EvalCode(co
, globals
, locals
);
1273 run_pyc_file(FILE *fp
, const char *filename
, PyObject
*globals
, PyObject
*locals
,
1274 PyCompilerFlags
*flags
)
1279 long PyImport_GetMagicNumber(void);
1281 magic
= PyMarshal_ReadLongFromFile(fp
);
1282 if (magic
!= PyImport_GetMagicNumber()) {
1283 PyErr_SetString(PyExc_RuntimeError
,
1284 "Bad magic number in .pyc file");
1287 (void) PyMarshal_ReadLongFromFile(fp
);
1288 v
= PyMarshal_ReadLastObjectFromFile(fp
);
1290 if (v
== NULL
|| !PyCode_Check(v
)) {
1292 PyErr_SetString(PyExc_RuntimeError
,
1293 "Bad code object in .pyc file");
1296 co
= (PyCodeObject
*)v
;
1297 v
= PyEval_EvalCode(co
, globals
, locals
);
1299 flags
->cf_flags
|= (co
->co_flags
& PyCF_MASK
);
1305 Py_CompileString(const char *str
, const char *filename
, int start
)
1307 return Py_CompileStringFlags(str
, filename
, start
, NULL
);
1311 Py_CompileStringFlags(const char *str
, const char *filename
, int start
,
1312 PyCompilerFlags
*flags
)
1317 n
= PyParser_SimpleParseStringFlagsFilename(str
, filename
, start
,
1318 PARSER_FLAGS(flags
));
1321 co
= PyNode_CompileFlags(n
, filename
, flags
);
1323 return (PyObject
*)co
;
1327 Py_SymtableString(const char *str
, const char *filename
, int start
)
1330 struct symtable
*st
;
1331 n
= PyParser_SimpleParseStringFlagsFilename(str
, filename
,
1335 st
= PyNode_CompileSymtable(n
, filename
);
1340 /* Simplified interface to parsefile -- return node or set exception */
1343 PyParser_SimpleParseFileFlags(FILE *fp
, const char *filename
, int start
, int flags
)
1347 n
= PyParser_ParseFileFlags(fp
, filename
, &_PyParser_Grammar
, start
,
1348 (char *)0, (char *)0, &err
, flags
);
1355 PyParser_SimpleParseFile(FILE *fp
, const char *filename
, int start
)
1357 return PyParser_SimpleParseFileFlags(fp
, filename
, start
, 0);
1360 /* Simplified interface to parsestring -- return node or set exception */
1363 PyParser_SimpleParseStringFlags(const char *str
, int start
, int flags
)
1367 n
= PyParser_ParseStringFlags(str
, &_PyParser_Grammar
, start
, &err
,
1375 PyParser_SimpleParseString(const char *str
, int start
)
1377 return PyParser_SimpleParseStringFlags(str
, start
, 0);
1381 PyParser_SimpleParseStringFlagsFilename(const char *str
, const char *filename
,
1382 int start
, int flags
)
1387 n
= PyParser_ParseStringFlagsFilename(str
, filename
,
1389 start
, &err
, flags
);
1396 PyParser_SimpleParseStringFilename(const char *str
, const char *filename
, int start
)
1398 return PyParser_SimpleParseStringFlagsFilename(str
, filename
,
1402 /* May want to move a more generalized form of this to parsetok.c or
1403 even parser modules. */
1406 PyParser_SetError(perrdetail
*err
)
1411 /* Set the error appropriate to the given input error code (see errcode.h) */
1414 err_input(perrdetail
*err
)
1416 PyObject
*v
, *w
, *errtype
;
1419 errtype
= PyExc_SyntaxError
;
1420 v
= Py_BuildValue("(ziiz)", err
->filename
,
1421 err
->lineno
, err
->offset
, err
->text
);
1422 if (err
->text
!= NULL
) {
1423 PyMem_DEL(err
->text
);
1426 switch (err
->error
) {
1428 errtype
= PyExc_IndentationError
;
1429 if (err
->expected
== INDENT
)
1430 msg
= "expected an indented block";
1431 else if (err
->token
== INDENT
)
1432 msg
= "unexpected indent";
1433 else if (err
->token
== DEDENT
)
1434 msg
= "unexpected unindent";
1436 errtype
= PyExc_SyntaxError
;
1437 msg
= "invalid syntax";
1441 msg
= "invalid token";
1444 msg
= "EOF while scanning triple-quoted string";
1447 msg
= "EOL while scanning single-quoted string";
1450 PyErr_SetNone(PyExc_KeyboardInterrupt
);
1458 msg
= "unexpected EOF while parsing";
1461 errtype
= PyExc_TabError
;
1462 msg
= "inconsistent use of tabs and spaces in indentation";
1465 msg
= "expression too long";
1468 errtype
= PyExc_IndentationError
;
1469 msg
= "unindent does not match any outer indentation level";
1472 errtype
= PyExc_IndentationError
;
1473 msg
= "too many levels of indentation";
1475 case E_DECODE
: { /* XXX */
1476 PyThreadState
* tstate
= PyThreadState_Get();
1477 PyObject
* value
= tstate
->curexc_value
;
1478 if (value
!= NULL
) {
1479 u
= PyObject_Repr(value
);
1481 msg
= PyString_AsString(u
);
1487 fprintf(stderr
, "error=%d\n", err
->error
);
1488 msg
= "unknown parsing error";
1491 w
= Py_BuildValue("(sO)", msg
, v
);
1494 PyErr_SetObject(errtype
, w
);
1498 /* Print fatal error message and abort */
1501 Py_FatalError(const char *msg
)
1503 fprintf(stderr
, "Fatal Python error: %s\n", msg
);
1505 OutputDebugString("Fatal Python error: ");
1506 OutputDebugString(msg
);
1507 OutputDebugString("\n");
1511 #endif /* MS_WINDOWS */
1515 /* Clean up and exit */
1518 #include "pythread.h"
1519 int _PyThread_Started
= 0; /* Set by threadmodule.c and maybe others */
1522 #define NEXITFUNCS 32
1523 static void (*exitfuncs
[NEXITFUNCS
])(void);
1524 static int nexitfuncs
= 0;
1526 int Py_AtExit(void (*func
)(void))
1528 if (nexitfuncs
>= NEXITFUNCS
)
1530 exitfuncs
[nexitfuncs
++] = func
;
1535 call_sys_exitfunc(void)
1537 PyObject
*exitfunc
= PySys_GetObject("exitfunc");
1541 Py_INCREF(exitfunc
);
1542 PySys_SetObject("exitfunc", (PyObject
*)NULL
);
1543 res
= PyEval_CallObject(exitfunc
, (PyObject
*)NULL
);
1545 if (!PyErr_ExceptionMatches(PyExc_SystemExit
)) {
1546 PySys_WriteStderr("Error in sys.exitfunc:\n");
1550 Py_DECREF(exitfunc
);
1558 call_ll_exitfuncs(void)
1560 while (nexitfuncs
> 0)
1561 (*exitfuncs
[--nexitfuncs
])();
1582 #ifdef HAVE_SIGNAL_H
1584 signal(SIGPIPE
, SIG_IGN
);
1587 signal(SIGXFZ
, SIG_IGN
);
1590 signal(SIGXFSZ
, SIG_IGN
);
1592 #endif /* HAVE_SIGNAL_H */
1593 PyOS_InitInterrupts(); /* May imply initsignal() */
1598 /* Check for file descriptor connected to interactive device.
1599 Pretend that stdin is always interactive, other files never. */
1604 return fd
== fileno(stdin
);
1610 * The file descriptor fd is considered ``interactive'' if either
1611 * a) isatty(fd) is TRUE, or
1612 * b) the -i flag was given, and the filename associated with
1613 * the descriptor is NULL or "<stdin>" or "???".
1616 Py_FdIsInteractive(FILE *fp
, const char *filename
)
1618 if (isatty((int)fileno(fp
)))
1620 if (!Py_InteractiveFlag
)
1622 return (filename
== NULL
) ||
1623 (strcmp(filename
, "<stdin>") == 0) ||
1624 (strcmp(filename
, "???") == 0);
1628 #if defined(USE_STACKCHECK)
1629 #if defined(WIN32) && defined(_MSC_VER)
1631 /* Stack checking for Microsoft C */
1637 * Return non-zero when we run out of memory on the stack; zero otherwise.
1640 PyOS_CheckStack(void)
1643 /* alloca throws a stack overflow exception if there's
1644 not enough space left on the stack */
1645 alloca(PYOS_STACK_MARGIN
* sizeof(void*));
1647 } __except (EXCEPTION_EXECUTE_HANDLER
) {
1648 /* just ignore all errors */
1653 #endif /* WIN32 && _MSC_VER */
1655 /* Alternate implementations can be added here... */
1657 #endif /* USE_STACKCHECK */
1660 /* Wrappers around sigaction() or signal(). */
1663 PyOS_getsig(int sig
)
1665 #ifdef HAVE_SIGACTION
1666 struct sigaction context
;
1667 /* Initialize context.sa_handler to SIG_ERR which makes about as
1668 * much sense as anything else. It should get overwritten if
1669 * sigaction actually succeeds and otherwise we avoid an
1670 * uninitialized memory read.
1672 context
.sa_handler
= SIG_ERR
;
1673 sigaction(sig
, NULL
, &context
);
1674 return context
.sa_handler
;
1676 PyOS_sighandler_t handler
;
1677 handler
= signal(sig
, SIG_IGN
);
1678 signal(sig
, handler
);
1684 PyOS_setsig(int sig
, PyOS_sighandler_t handler
)
1686 #ifdef HAVE_SIGACTION
1687 struct sigaction context
;
1688 PyOS_sighandler_t oldhandler
;
1689 /* Initialize context.sa_handler to SIG_ERR which makes about as
1690 * much sense as anything else. It should get overwritten if
1691 * sigaction actually succeeds and otherwise we avoid an
1692 * uninitialized memory read.
1694 context
.sa_handler
= SIG_ERR
;
1695 sigaction(sig
, NULL
, &context
);
1696 oldhandler
= context
.sa_handler
;
1697 context
.sa_handler
= handler
;
1698 sigaction(sig
, &context
, NULL
);
1701 return signal(sig
, handler
);