Issue #4969: The mimetypes module now reads the MIME database from
[python.git] / Python / pythonrun.c
blob4f8417a31f3f94d237cf5c0192f3bf787336b71d
2 /* Python interpreter top-level routines, including init/exit */
4 #include "Python.h"
6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */
8 #include "grammar.h"
9 #include "node.h"
10 #include "token.h"
11 #include "parsetok.h"
12 #include "errcode.h"
13 #include "code.h"
14 #include "compile.h"
15 #include "symtable.h"
16 #include "pyarena.h"
17 #include "ast.h"
18 #include "eval.h"
19 #include "marshal.h"
20 #include "abstract.h"
22 #ifdef HAVE_SIGNAL_H
23 #include <signal.h>
24 #endif
26 #ifdef MS_WINDOWS
27 #include "malloc.h" /* for alloca */
28 #endif
30 #ifdef HAVE_LANGINFO_H
31 #include <locale.h>
32 #include <langinfo.h>
33 #endif
35 #ifdef MS_WINDOWS
36 #undef BYTE
37 #include "windows.h"
38 #endif
40 #ifndef Py_REF_DEBUG
41 #define PRINT_TOTAL_REFS()
42 #else /* Py_REF_DEBUG */
43 #define PRINT_TOTAL_REFS() fprintf(stderr, \
44 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
45 _Py_GetRefTotal())
46 #endif
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
52 extern char *Py_GetPath(void);
54 extern grammar _PyParser_Grammar; /* From graminit.c */
56 /* Forward */
57 static void initmain(void);
58 static void initsite(void);
59 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
60 PyCompilerFlags *, PyArena *);
61 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
62 PyCompilerFlags *);
63 static void err_input(perrdetail *);
64 static void initsigs(void);
65 static void wait_for_thread_shutdown(void);
66 static void call_sys_exitfunc(void);
67 static void call_ll_exitfuncs(void);
68 extern void _PyUnicode_Init(void);
69 extern void _PyUnicode_Fini(void);
71 #ifdef WITH_THREAD
72 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
73 extern void _PyGILState_Fini(void);
74 #endif /* WITH_THREAD */
76 int Py_DebugFlag; /* Needed by parser.c */
77 int Py_VerboseFlag; /* Needed by import.c */
78 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
79 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
80 int Py_NoSiteFlag; /* Suppress 'import site' */
81 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
82 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
83 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
84 int Py_FrozenFlag; /* Needed by getpath.c */
85 int Py_UnicodeFlag = 0; /* Needed by compile.c */
86 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
87 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
88 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
89 true divisions (which they will be in 2.3). */
90 int _Py_QnewFlag = 0;
91 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
93 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
94 since _warnings is builtin. This API should not be used. */
95 PyObject *
96 PyModule_GetWarningsModule(void)
98 return PyImport_ImportModule("warnings");
101 static int initialized = 0;
103 /* API to access the initialized flag -- useful for esoteric use */
106 Py_IsInitialized(void)
108 return initialized;
111 /* Global initializations. Can be undone by Py_Finalize(). Don't
112 call this twice without an intervening Py_Finalize() call. When
113 initializations fail, a fatal error is issued and the function does
114 not return. On return, the first thread and interpreter state have
115 been created.
117 Locking: you must hold the interpreter lock while calling this.
118 (If the lock has not yet been initialized, that's equivalent to
119 having the lock, but you cannot use multiple threads.)
123 static int
124 add_flag(int flag, const char *envs)
126 int env = atoi(envs);
127 if (flag < env)
128 flag = env;
129 if (flag < 1)
130 flag = 1;
131 return flag;
134 void
135 Py_InitializeEx(int install_sigs)
137 PyInterpreterState *interp;
138 PyThreadState *tstate;
139 PyObject *bimod, *sysmod;
140 char *p;
141 char *icodeset = NULL; /* On Windows, input codeset may theoretically
142 differ from output codeset. */
143 char *codeset = NULL;
144 char *errors = NULL;
145 int free_codeset = 0;
146 int overridden = 0;
147 PyObject *sys_stream, *sys_isatty;
148 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
149 char *saved_locale, *loc_codeset;
150 #endif
151 #ifdef MS_WINDOWS
152 char ibuf[128];
153 char buf[128];
154 #endif
155 extern void _Py_ReadyTypes(void);
157 if (initialized)
158 return;
159 initialized = 1;
161 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
162 Py_DebugFlag = add_flag(Py_DebugFlag, p);
163 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
164 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
165 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
166 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
167 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
168 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
170 interp = PyInterpreterState_New();
171 if (interp == NULL)
172 Py_FatalError("Py_Initialize: can't make first interpreter");
174 tstate = PyThreadState_New(interp);
175 if (tstate == NULL)
176 Py_FatalError("Py_Initialize: can't make first thread");
177 (void) PyThreadState_Swap(tstate);
179 _Py_ReadyTypes();
181 if (!_PyFrame_Init())
182 Py_FatalError("Py_Initialize: can't init frames");
184 if (!_PyInt_Init())
185 Py_FatalError("Py_Initialize: can't init ints");
187 if (!_PyLong_Init())
188 Py_FatalError("Py_Initialize: can't init longs");
190 if (!PyByteArray_Init())
191 Py_FatalError("Py_Initialize: can't init bytearray");
193 _PyFloat_Init();
195 interp->modules = PyDict_New();
196 if (interp->modules == NULL)
197 Py_FatalError("Py_Initialize: can't make modules dictionary");
198 interp->modules_reloading = PyDict_New();
199 if (interp->modules_reloading == NULL)
200 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
202 #ifdef Py_USING_UNICODE
203 /* Init Unicode implementation; relies on the codec registry */
204 _PyUnicode_Init();
205 #endif
207 bimod = _PyBuiltin_Init();
208 if (bimod == NULL)
209 Py_FatalError("Py_Initialize: can't initialize __builtin__");
210 interp->builtins = PyModule_GetDict(bimod);
211 if (interp->builtins == NULL)
212 Py_FatalError("Py_Initialize: can't initialize builtins dict");
213 Py_INCREF(interp->builtins);
215 sysmod = _PySys_Init();
216 if (sysmod == NULL)
217 Py_FatalError("Py_Initialize: can't initialize sys");
218 interp->sysdict = PyModule_GetDict(sysmod);
219 if (interp->sysdict == NULL)
220 Py_FatalError("Py_Initialize: can't initialize sys dict");
221 Py_INCREF(interp->sysdict);
222 _PyImport_FixupExtension("sys", "sys");
223 PySys_SetPath(Py_GetPath());
224 PyDict_SetItemString(interp->sysdict, "modules",
225 interp->modules);
227 _PyImport_Init();
229 /* initialize builtin exceptions */
230 _PyExc_Init();
231 _PyImport_FixupExtension("exceptions", "exceptions");
233 /* phase 2 of builtins */
234 _PyImport_FixupExtension("__builtin__", "__builtin__");
236 _PyImportHooks_Init();
238 if (install_sigs)
239 initsigs(); /* Signal handling stuff, including initintr() */
241 /* Initialize warnings. */
242 _PyWarnings_Init();
243 if (PySys_HasWarnOptions()) {
244 PyObject *warnings_module = PyImport_ImportModule("warnings");
245 if (!warnings_module)
246 PyErr_Clear();
247 Py_XDECREF(warnings_module);
250 initmain(); /* Module __main__ */
251 if (!Py_NoSiteFlag)
252 initsite(); /* Module site */
254 /* auto-thread-state API, if available */
255 #ifdef WITH_THREAD
256 _PyGILState_Init(interp, tstate);
257 #endif /* WITH_THREAD */
259 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
260 p = icodeset = codeset = strdup(p);
261 free_codeset = 1;
262 errors = strchr(p, ':');
263 if (errors) {
264 *errors = '\0';
265 errors++;
267 overridden = 1;
270 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
271 /* On Unix, set the file system encoding according to the
272 user's preference, if the CODESET names a well-known
273 Python codec, and Py_FileSystemDefaultEncoding isn't
274 initialized by other means. Also set the encoding of
275 stdin and stdout if these are terminals, unless overridden. */
277 if (!overridden || !Py_FileSystemDefaultEncoding) {
278 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
279 setlocale(LC_CTYPE, "");
280 loc_codeset = nl_langinfo(CODESET);
281 if (loc_codeset && *loc_codeset) {
282 PyObject *enc = PyCodec_Encoder(loc_codeset);
283 if (enc) {
284 loc_codeset = strdup(loc_codeset);
285 Py_DECREF(enc);
286 } else {
287 loc_codeset = NULL;
288 PyErr_Clear();
290 } else
291 loc_codeset = NULL;
292 setlocale(LC_CTYPE, saved_locale);
293 free(saved_locale);
295 if (!overridden) {
296 codeset = icodeset = loc_codeset;
297 free_codeset = 1;
300 /* Initialize Py_FileSystemDefaultEncoding from
301 locale even if PYTHONIOENCODING is set. */
302 if (!Py_FileSystemDefaultEncoding) {
303 Py_FileSystemDefaultEncoding = loc_codeset;
304 if (!overridden)
305 free_codeset = 0;
308 #endif
310 #ifdef MS_WINDOWS
311 if (!overridden) {
312 icodeset = ibuf;
313 codeset = buf;
314 sprintf(ibuf, "cp%d", GetConsoleCP());
315 sprintf(buf, "cp%d", GetConsoleOutputCP());
317 #endif
319 if (codeset) {
320 sys_stream = PySys_GetObject("stdin");
321 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
322 if (!sys_isatty)
323 PyErr_Clear();
324 if ((overridden ||
325 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
326 PyFile_Check(sys_stream)) {
327 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
328 Py_FatalError("Cannot set codeset of stdin");
330 Py_XDECREF(sys_isatty);
332 sys_stream = PySys_GetObject("stdout");
333 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
334 if (!sys_isatty)
335 PyErr_Clear();
336 if ((overridden ||
337 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
338 PyFile_Check(sys_stream)) {
339 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
340 Py_FatalError("Cannot set codeset of stdout");
342 Py_XDECREF(sys_isatty);
344 sys_stream = PySys_GetObject("stderr");
345 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
346 if (!sys_isatty)
347 PyErr_Clear();
348 if((overridden ||
349 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
350 PyFile_Check(sys_stream)) {
351 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
352 Py_FatalError("Cannot set codeset of stderr");
354 Py_XDECREF(sys_isatty);
356 if (free_codeset)
357 free(codeset);
361 void
362 Py_Initialize(void)
364 Py_InitializeEx(1);
368 #ifdef COUNT_ALLOCS
369 extern void dump_counts(FILE*);
370 #endif
372 /* Undo the effect of Py_Initialize().
374 Beware: if multiple interpreter and/or thread states exist, these
375 are not wiped out; only the current thread and interpreter state
376 are deleted. But since everything else is deleted, those other
377 interpreter and thread states should no longer be used.
379 (XXX We should do better, e.g. wipe out all interpreters and
380 threads.)
382 Locking: as above.
386 void
387 Py_Finalize(void)
389 PyInterpreterState *interp;
390 PyThreadState *tstate;
392 if (!initialized)
393 return;
395 wait_for_thread_shutdown();
397 /* The interpreter is still entirely intact at this point, and the
398 * exit funcs may be relying on that. In particular, if some thread
399 * or exit func is still waiting to do an import, the import machinery
400 * expects Py_IsInitialized() to return true. So don't say the
401 * interpreter is uninitialized until after the exit funcs have run.
402 * Note that Threading.py uses an exit func to do a join on all the
403 * threads created thru it, so this also protects pending imports in
404 * the threads created via Threading.
406 call_sys_exitfunc();
407 initialized = 0;
409 /* Get current thread state and interpreter pointer */
410 tstate = PyThreadState_GET();
411 interp = tstate->interp;
413 /* Disable signal handling */
414 PyOS_FiniInterrupts();
416 /* Clear type lookup cache */
417 PyType_ClearCache();
419 /* Collect garbage. This may call finalizers; it's nice to call these
420 * before all modules are destroyed.
421 * XXX If a __del__ or weakref callback is triggered here, and tries to
422 * XXX import a module, bad things can happen, because Python no
423 * XXX longer believes it's initialized.
424 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
425 * XXX is easy to provoke that way. I've also seen, e.g.,
426 * XXX Exception exceptions.ImportError: 'No module named sha'
427 * XXX in <function callback at 0x008F5718> ignored
428 * XXX but I'm unclear on exactly how that one happens. In any case,
429 * XXX I haven't seen a real-life report of either of these.
431 PyGC_Collect();
432 #ifdef COUNT_ALLOCS
433 /* With COUNT_ALLOCS, it helps to run GC multiple times:
434 each collection might release some types from the type
435 list, so they become garbage. */
436 while (PyGC_Collect() > 0)
437 /* nothing */;
438 #endif
440 /* Destroy all modules */
441 PyImport_Cleanup();
443 /* Collect final garbage. This disposes of cycles created by
444 * new-style class definitions, for example.
445 * XXX This is disabled because it caused too many problems. If
446 * XXX a __del__ or weakref callback triggers here, Python code has
447 * XXX a hard time running, because even the sys module has been
448 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
449 * XXX One symptom is a sequence of information-free messages
450 * XXX coming from threads (if a __del__ or callback is invoked,
451 * XXX other threads can execute too, and any exception they encounter
452 * XXX triggers a comedy of errors as subsystem after subsystem
453 * XXX fails to find what it *expects* to find in sys to help report
454 * XXX the exception and consequent unexpected failures). I've also
455 * XXX seen segfaults then, after adding print statements to the
456 * XXX Python code getting called.
458 #if 0
459 PyGC_Collect();
460 #endif
462 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
463 _PyImport_Fini();
465 /* Debugging stuff */
466 #ifdef COUNT_ALLOCS
467 dump_counts(stdout);
468 #endif
470 PRINT_TOTAL_REFS();
472 #ifdef Py_TRACE_REFS
473 /* Display all objects still alive -- this can invoke arbitrary
474 * __repr__ overrides, so requires a mostly-intact interpreter.
475 * Alas, a lot of stuff may still be alive now that will be cleaned
476 * up later.
478 if (Py_GETENV("PYTHONDUMPREFS"))
479 _Py_PrintReferences(stderr);
480 #endif /* Py_TRACE_REFS */
482 /* Clear interpreter state */
483 PyInterpreterState_Clear(interp);
485 /* Now we decref the exception classes. After this point nothing
486 can raise an exception. That's okay, because each Fini() method
487 below has been checked to make sure no exceptions are ever
488 raised.
491 _PyExc_Fini();
493 /* Cleanup auto-thread-state */
494 #ifdef WITH_THREAD
495 _PyGILState_Fini();
496 #endif /* WITH_THREAD */
498 /* Delete current thread */
499 PyThreadState_Swap(NULL);
500 PyInterpreterState_Delete(interp);
502 /* Sundry finalizers */
503 PyMethod_Fini();
504 PyFrame_Fini();
505 PyCFunction_Fini();
506 PyTuple_Fini();
507 PyList_Fini();
508 PySet_Fini();
509 PyString_Fini();
510 PyByteArray_Fini();
511 PyInt_Fini();
512 PyFloat_Fini();
513 PyDict_Fini();
515 #ifdef Py_USING_UNICODE
516 /* Cleanup Unicode implementation */
517 _PyUnicode_Fini();
518 #endif
520 /* XXX Still allocated:
521 - various static ad-hoc pointers to interned strings
522 - int and float free list blocks
523 - whatever various modules and libraries allocate
526 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
528 #ifdef Py_TRACE_REFS
529 /* Display addresses (& refcnts) of all objects still alive.
530 * An address can be used to find the repr of the object, printed
531 * above by _Py_PrintReferences.
533 if (Py_GETENV("PYTHONDUMPREFS"))
534 _Py_PrintReferenceAddresses(stderr);
535 #endif /* Py_TRACE_REFS */
536 #ifdef PYMALLOC_DEBUG
537 if (Py_GETENV("PYTHONMALLOCSTATS"))
538 _PyObject_DebugMallocStats();
539 #endif
541 call_ll_exitfuncs();
544 /* Create and initialize a new interpreter and thread, and return the
545 new thread. This requires that Py_Initialize() has been called
546 first.
548 Unsuccessful initialization yields a NULL pointer. Note that *no*
549 exception information is available even in this case -- the
550 exception information is held in the thread, and there is no
551 thread.
553 Locking: as above.
557 PyThreadState *
558 Py_NewInterpreter(void)
560 PyInterpreterState *interp;
561 PyThreadState *tstate, *save_tstate;
562 PyObject *bimod, *sysmod;
564 if (!initialized)
565 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
567 interp = PyInterpreterState_New();
568 if (interp == NULL)
569 return NULL;
571 tstate = PyThreadState_New(interp);
572 if (tstate == NULL) {
573 PyInterpreterState_Delete(interp);
574 return NULL;
577 save_tstate = PyThreadState_Swap(tstate);
579 /* XXX The following is lax in error checking */
581 interp->modules = PyDict_New();
582 interp->modules_reloading = PyDict_New();
584 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
585 if (bimod != NULL) {
586 interp->builtins = PyModule_GetDict(bimod);
587 if (interp->builtins == NULL)
588 goto handle_error;
589 Py_INCREF(interp->builtins);
591 sysmod = _PyImport_FindExtension("sys", "sys");
592 if (bimod != NULL && sysmod != NULL) {
593 interp->sysdict = PyModule_GetDict(sysmod);
594 if (interp->sysdict == NULL)
595 goto handle_error;
596 Py_INCREF(interp->sysdict);
597 PySys_SetPath(Py_GetPath());
598 PyDict_SetItemString(interp->sysdict, "modules",
599 interp->modules);
600 _PyImportHooks_Init();
601 initmain();
602 if (!Py_NoSiteFlag)
603 initsite();
606 if (!PyErr_Occurred())
607 return tstate;
609 handle_error:
610 /* Oops, it didn't work. Undo it all. */
612 PyErr_Print();
613 PyThreadState_Clear(tstate);
614 PyThreadState_Swap(save_tstate);
615 PyThreadState_Delete(tstate);
616 PyInterpreterState_Delete(interp);
618 return NULL;
621 /* Delete an interpreter and its last thread. This requires that the
622 given thread state is current, that the thread has no remaining
623 frames, and that it is its interpreter's only remaining thread.
624 It is a fatal error to violate these constraints.
626 (Py_Finalize() doesn't have these constraints -- it zaps
627 everything, regardless.)
629 Locking: as above.
633 void
634 Py_EndInterpreter(PyThreadState *tstate)
636 PyInterpreterState *interp = tstate->interp;
638 if (tstate != PyThreadState_GET())
639 Py_FatalError("Py_EndInterpreter: thread is not current");
640 if (tstate->frame != NULL)
641 Py_FatalError("Py_EndInterpreter: thread still has a frame");
642 if (tstate != interp->tstate_head || tstate->next != NULL)
643 Py_FatalError("Py_EndInterpreter: not the last thread");
645 PyImport_Cleanup();
646 PyInterpreterState_Clear(interp);
647 PyThreadState_Swap(NULL);
648 PyInterpreterState_Delete(interp);
651 static char *progname = "python";
653 void
654 Py_SetProgramName(char *pn)
656 if (pn && *pn)
657 progname = pn;
660 char *
661 Py_GetProgramName(void)
663 return progname;
666 static char *default_home = NULL;
668 void
669 Py_SetPythonHome(char *home)
671 default_home = home;
674 char *
675 Py_GetPythonHome(void)
677 char *home = default_home;
678 if (home == NULL && !Py_IgnoreEnvironmentFlag)
679 home = Py_GETENV("PYTHONHOME");
680 return home;
683 /* Create __main__ module */
685 static void
686 initmain(void)
688 PyObject *m, *d;
689 m = PyImport_AddModule("__main__");
690 if (m == NULL)
691 Py_FatalError("can't create __main__ module");
692 d = PyModule_GetDict(m);
693 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
694 PyObject *bimod = PyImport_ImportModule("__builtin__");
695 if (bimod == NULL ||
696 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
697 Py_FatalError("can't add __builtins__ to __main__");
698 Py_DECREF(bimod);
702 /* Import the site module (not into __main__ though) */
704 static void
705 initsite(void)
707 PyObject *m, *f;
708 m = PyImport_ImportModule("site");
709 if (m == NULL) {
710 f = PySys_GetObject("stderr");
711 if (Py_VerboseFlag) {
712 PyFile_WriteString(
713 "'import site' failed; traceback:\n", f);
714 PyErr_Print();
716 else {
717 PyFile_WriteString(
718 "'import site' failed; use -v for traceback\n", f);
719 PyErr_Clear();
722 else {
723 Py_DECREF(m);
727 /* Parse input from a file and execute it */
730 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
731 PyCompilerFlags *flags)
733 if (filename == NULL)
734 filename = "???";
735 if (Py_FdIsInteractive(fp, filename)) {
736 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
737 if (closeit)
738 fclose(fp);
739 return err;
741 else
742 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
746 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
748 PyObject *v;
749 int ret;
750 PyCompilerFlags local_flags;
752 if (flags == NULL) {
753 flags = &local_flags;
754 local_flags.cf_flags = 0;
756 v = PySys_GetObject("ps1");
757 if (v == NULL) {
758 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
759 Py_XDECREF(v);
761 v = PySys_GetObject("ps2");
762 if (v == NULL) {
763 PySys_SetObject("ps2", v = PyString_FromString("... "));
764 Py_XDECREF(v);
766 for (;;) {
767 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
768 PRINT_TOTAL_REFS();
769 if (ret == E_EOF)
770 return 0;
772 if (ret == E_NOMEM)
773 return -1;
778 #if 0
779 /* compute parser flags based on compiler flags */
780 #define PARSER_FLAGS(flags) \
781 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
782 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
783 #endif
784 #if 1
785 /* Keep an example of flags with future keyword support. */
786 #define PARSER_FLAGS(flags) \
787 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
788 PyPARSE_DONT_IMPLY_DEDENT : 0) \
789 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
790 PyPARSE_PRINT_IS_FUNCTION : 0) \
791 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
792 PyPARSE_UNICODE_LITERALS : 0) \
793 ) : 0)
794 #endif
797 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
799 PyObject *m, *d, *v, *w;
800 mod_ty mod;
801 PyArena *arena;
802 char *ps1 = "", *ps2 = "";
803 int errcode = 0;
805 v = PySys_GetObject("ps1");
806 if (v != NULL) {
807 v = PyObject_Str(v);
808 if (v == NULL)
809 PyErr_Clear();
810 else if (PyString_Check(v))
811 ps1 = PyString_AsString(v);
813 w = PySys_GetObject("ps2");
814 if (w != NULL) {
815 w = PyObject_Str(w);
816 if (w == NULL)
817 PyErr_Clear();
818 else if (PyString_Check(w))
819 ps2 = PyString_AsString(w);
821 arena = PyArena_New();
822 if (arena == NULL) {
823 Py_XDECREF(v);
824 Py_XDECREF(w);
825 return -1;
827 mod = PyParser_ASTFromFile(fp, filename,
828 Py_single_input, ps1, ps2,
829 flags, &errcode, arena);
830 Py_XDECREF(v);
831 Py_XDECREF(w);
832 if (mod == NULL) {
833 PyArena_Free(arena);
834 if (errcode == E_EOF) {
835 PyErr_Clear();
836 return E_EOF;
838 PyErr_Print();
839 return -1;
841 m = PyImport_AddModule("__main__");
842 if (m == NULL) {
843 PyArena_Free(arena);
844 return -1;
846 d = PyModule_GetDict(m);
847 v = run_mod(mod, filename, d, d, flags, arena);
848 PyArena_Free(arena);
849 if (v == NULL) {
850 PyErr_Print();
851 return -1;
853 Py_DECREF(v);
854 if (Py_FlushLine())
855 PyErr_Clear();
856 return 0;
859 /* Check whether a file maybe a pyc file: Look at the extension,
860 the file type, and, if we may close it, at the first few bytes. */
862 static int
863 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
865 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
866 return 1;
868 /* Only look into the file if we are allowed to close it, since
869 it then should also be seekable. */
870 if (closeit) {
871 /* Read only two bytes of the magic. If the file was opened in
872 text mode, the bytes 3 and 4 of the magic (\r\n) might not
873 be read as they are on disk. */
874 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
875 unsigned char buf[2];
876 /* Mess: In case of -x, the stream is NOT at its start now,
877 and ungetc() was used to push back the first newline,
878 which makes the current stream position formally undefined,
879 and a x-platform nightmare.
880 Unfortunately, we have no direct way to know whether -x
881 was specified. So we use a terrible hack: if the current
882 stream position is not 0, we assume -x was specified, and
883 give up. Bug 132850 on SourceForge spells out the
884 hopelessness of trying anything else (fseek and ftell
885 don't work predictably x-platform for text-mode files).
887 int ispyc = 0;
888 if (ftell(fp) == 0) {
889 if (fread(buf, 1, 2, fp) == 2 &&
890 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
891 ispyc = 1;
892 rewind(fp);
894 return ispyc;
896 return 0;
900 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
901 PyCompilerFlags *flags)
903 PyObject *m, *d, *v;
904 const char *ext;
905 int set_file_name = 0, ret, len;
907 m = PyImport_AddModule("__main__");
908 if (m == NULL)
909 return -1;
910 d = PyModule_GetDict(m);
911 if (PyDict_GetItemString(d, "__file__") == NULL) {
912 PyObject *f = PyString_FromString(filename);
913 if (f == NULL)
914 return -1;
915 if (PyDict_SetItemString(d, "__file__", f) < 0) {
916 Py_DECREF(f);
917 return -1;
919 set_file_name = 1;
920 Py_DECREF(f);
922 len = strlen(filename);
923 ext = filename + len - (len > 4 ? 4 : 0);
924 if (maybe_pyc_file(fp, filename, ext, closeit)) {
925 /* Try to run a pyc file. First, re-open in binary */
926 if (closeit)
927 fclose(fp);
928 if ((fp = fopen(filename, "rb")) == NULL) {
929 fprintf(stderr, "python: Can't reopen .pyc file\n");
930 ret = -1;
931 goto done;
933 /* Turn on optimization if a .pyo file is given */
934 if (strcmp(ext, ".pyo") == 0)
935 Py_OptimizeFlag = 1;
936 v = run_pyc_file(fp, filename, d, d, flags);
937 } else {
938 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
939 closeit, flags);
941 if (v == NULL) {
942 PyErr_Print();
943 ret = -1;
944 goto done;
946 Py_DECREF(v);
947 if (Py_FlushLine())
948 PyErr_Clear();
949 ret = 0;
950 done:
951 if (set_file_name && PyDict_DelItemString(d, "__file__"))
952 PyErr_Clear();
953 return ret;
957 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
959 PyObject *m, *d, *v;
960 m = PyImport_AddModule("__main__");
961 if (m == NULL)
962 return -1;
963 d = PyModule_GetDict(m);
964 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
965 if (v == NULL) {
966 PyErr_Print();
967 return -1;
969 Py_DECREF(v);
970 if (Py_FlushLine())
971 PyErr_Clear();
972 return 0;
975 static int
976 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
977 int *lineno, int *offset, const char **text)
979 long hold;
980 PyObject *v;
982 /* old style errors */
983 if (PyTuple_Check(err))
984 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
985 lineno, offset, text);
987 /* new style errors. `err' is an instance */
989 if (! (v = PyObject_GetAttrString(err, "msg")))
990 goto finally;
991 *message = v;
993 if (!(v = PyObject_GetAttrString(err, "filename")))
994 goto finally;
995 if (v == Py_None)
996 *filename = NULL;
997 else if (! (*filename = PyString_AsString(v)))
998 goto finally;
1000 Py_DECREF(v);
1001 if (!(v = PyObject_GetAttrString(err, "lineno")))
1002 goto finally;
1003 hold = PyInt_AsLong(v);
1004 Py_DECREF(v);
1005 v = NULL;
1006 if (hold < 0 && PyErr_Occurred())
1007 goto finally;
1008 *lineno = (int)hold;
1010 if (!(v = PyObject_GetAttrString(err, "offset")))
1011 goto finally;
1012 if (v == Py_None) {
1013 *offset = -1;
1014 Py_DECREF(v);
1015 v = NULL;
1016 } else {
1017 hold = PyInt_AsLong(v);
1018 Py_DECREF(v);
1019 v = NULL;
1020 if (hold < 0 && PyErr_Occurred())
1021 goto finally;
1022 *offset = (int)hold;
1025 if (!(v = PyObject_GetAttrString(err, "text")))
1026 goto finally;
1027 if (v == Py_None)
1028 *text = NULL;
1029 else if (! (*text = PyString_AsString(v)))
1030 goto finally;
1031 Py_DECREF(v);
1032 return 1;
1034 finally:
1035 Py_XDECREF(v);
1036 return 0;
1039 void
1040 PyErr_Print(void)
1042 PyErr_PrintEx(1);
1045 static void
1046 print_error_text(PyObject *f, int offset, const char *text)
1048 char *nl;
1049 if (offset >= 0) {
1050 if (offset > 0 && offset == (int)strlen(text))
1051 offset--;
1052 for (;;) {
1053 nl = strchr(text, '\n');
1054 if (nl == NULL || nl-text >= offset)
1055 break;
1056 offset -= (int)(nl+1-text);
1057 text = nl+1;
1059 while (*text == ' ' || *text == '\t') {
1060 text++;
1061 offset--;
1064 PyFile_WriteString(" ", f);
1065 PyFile_WriteString(text, f);
1066 if (*text == '\0' || text[strlen(text)-1] != '\n')
1067 PyFile_WriteString("\n", f);
1068 if (offset == -1)
1069 return;
1070 PyFile_WriteString(" ", f);
1071 offset--;
1072 while (offset > 0) {
1073 PyFile_WriteString(" ", f);
1074 offset--;
1076 PyFile_WriteString("^\n", f);
1079 static void
1080 handle_system_exit(void)
1082 PyObject *exception, *value, *tb;
1083 int exitcode = 0;
1085 if (Py_InspectFlag)
1086 /* Don't exit if -i flag was given. This flag is set to 0
1087 * when entering interactive mode for inspecting. */
1088 return;
1090 PyErr_Fetch(&exception, &value, &tb);
1091 if (Py_FlushLine())
1092 PyErr_Clear();
1093 fflush(stdout);
1094 if (value == NULL || value == Py_None)
1095 goto done;
1096 if (PyExceptionInstance_Check(value)) {
1097 /* The error code should be in the `code' attribute. */
1098 PyObject *code = PyObject_GetAttrString(value, "code");
1099 if (code) {
1100 Py_DECREF(value);
1101 value = code;
1102 if (value == Py_None)
1103 goto done;
1105 /* If we failed to dig out the 'code' attribute,
1106 just let the else clause below print the error. */
1108 if (PyInt_Check(value))
1109 exitcode = (int)PyInt_AsLong(value);
1110 else {
1111 PyObject_Print(value, stderr, Py_PRINT_RAW);
1112 PySys_WriteStderr("\n");
1113 exitcode = 1;
1115 done:
1116 /* Restore and clear the exception info, in order to properly decref
1117 * the exception, value, and traceback. If we just exit instead,
1118 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1119 * some finalizers from running.
1121 PyErr_Restore(exception, value, tb);
1122 PyErr_Clear();
1123 Py_Exit(exitcode);
1124 /* NOTREACHED */
1127 void
1128 PyErr_PrintEx(int set_sys_last_vars)
1130 PyObject *exception, *v, *tb, *hook;
1132 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1133 handle_system_exit();
1135 PyErr_Fetch(&exception, &v, &tb);
1136 if (exception == NULL)
1137 return;
1138 PyErr_NormalizeException(&exception, &v, &tb);
1139 if (exception == NULL)
1140 return;
1141 /* Now we know v != NULL too */
1142 if (set_sys_last_vars) {
1143 PySys_SetObject("last_type", exception);
1144 PySys_SetObject("last_value", v);
1145 PySys_SetObject("last_traceback", tb);
1147 hook = PySys_GetObject("excepthook");
1148 if (hook) {
1149 PyObject *args = PyTuple_Pack(3,
1150 exception, v, tb ? tb : Py_None);
1151 PyObject *result = PyEval_CallObject(hook, args);
1152 if (result == NULL) {
1153 PyObject *exception2, *v2, *tb2;
1154 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1155 handle_system_exit();
1157 PyErr_Fetch(&exception2, &v2, &tb2);
1158 PyErr_NormalizeException(&exception2, &v2, &tb2);
1159 /* It should not be possible for exception2 or v2
1160 to be NULL. However PyErr_Display() can't
1161 tolerate NULLs, so just be safe. */
1162 if (exception2 == NULL) {
1163 exception2 = Py_None;
1164 Py_INCREF(exception2);
1166 if (v2 == NULL) {
1167 v2 = Py_None;
1168 Py_INCREF(v2);
1170 if (Py_FlushLine())
1171 PyErr_Clear();
1172 fflush(stdout);
1173 PySys_WriteStderr("Error in sys.excepthook:\n");
1174 PyErr_Display(exception2, v2, tb2);
1175 PySys_WriteStderr("\nOriginal exception was:\n");
1176 PyErr_Display(exception, v, tb);
1177 Py_DECREF(exception2);
1178 Py_DECREF(v2);
1179 Py_XDECREF(tb2);
1181 Py_XDECREF(result);
1182 Py_XDECREF(args);
1183 } else {
1184 PySys_WriteStderr("sys.excepthook is missing\n");
1185 PyErr_Display(exception, v, tb);
1187 Py_XDECREF(exception);
1188 Py_XDECREF(v);
1189 Py_XDECREF(tb);
1192 void
1193 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1195 int err = 0;
1196 PyObject *f = PySys_GetObject("stderr");
1197 Py_INCREF(value);
1198 if (f == NULL)
1199 fprintf(stderr, "lost sys.stderr\n");
1200 else {
1201 if (Py_FlushLine())
1202 PyErr_Clear();
1203 fflush(stdout);
1204 if (tb && tb != Py_None)
1205 err = PyTraceBack_Print(tb, f);
1206 if (err == 0 &&
1207 PyObject_HasAttrString(value, "print_file_and_line"))
1209 PyObject *message;
1210 const char *filename, *text;
1211 int lineno, offset;
1212 if (!parse_syntax_error(value, &message, &filename,
1213 &lineno, &offset, &text))
1214 PyErr_Clear();
1215 else {
1216 char buf[10];
1217 PyFile_WriteString(" File \"", f);
1218 if (filename == NULL)
1219 PyFile_WriteString("<string>", f);
1220 else
1221 PyFile_WriteString(filename, f);
1222 PyFile_WriteString("\", line ", f);
1223 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1224 PyFile_WriteString(buf, f);
1225 PyFile_WriteString("\n", f);
1226 if (text != NULL)
1227 print_error_text(f, offset, text);
1228 Py_DECREF(value);
1229 value = message;
1230 /* Can't be bothered to check all those
1231 PyFile_WriteString() calls */
1232 if (PyErr_Occurred())
1233 err = -1;
1236 if (err) {
1237 /* Don't do anything else */
1239 else if (PyExceptionClass_Check(exception)) {
1240 PyObject* moduleName;
1241 char* className = PyExceptionClass_Name(exception);
1242 if (className != NULL) {
1243 char *dot = strrchr(className, '.');
1244 if (dot != NULL)
1245 className = dot+1;
1248 moduleName = PyObject_GetAttrString(exception, "__module__");
1249 if (moduleName == NULL)
1250 err = PyFile_WriteString("<unknown>", f);
1251 else {
1252 char* modstr = PyString_AsString(moduleName);
1253 if (modstr && strcmp(modstr, "exceptions"))
1255 err = PyFile_WriteString(modstr, f);
1256 err += PyFile_WriteString(".", f);
1258 Py_DECREF(moduleName);
1260 if (err == 0) {
1261 if (className == NULL)
1262 err = PyFile_WriteString("<unknown>", f);
1263 else
1264 err = PyFile_WriteString(className, f);
1267 else
1268 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1269 if (err == 0 && (value != Py_None)) {
1270 PyObject *s = PyObject_Str(value);
1271 /* only print colon if the str() of the
1272 object is not the empty string
1274 if (s == NULL)
1275 err = -1;
1276 else if (!PyString_Check(s) ||
1277 PyString_GET_SIZE(s) != 0)
1278 err = PyFile_WriteString(": ", f);
1279 if (err == 0)
1280 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1281 Py_XDECREF(s);
1283 /* try to write a newline in any case */
1284 err += PyFile_WriteString("\n", f);
1286 Py_DECREF(value);
1287 /* If an error happened here, don't show it.
1288 XXX This is wrong, but too many callers rely on this behavior. */
1289 if (err != 0)
1290 PyErr_Clear();
1293 PyObject *
1294 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1295 PyObject *locals, PyCompilerFlags *flags)
1297 PyObject *ret = NULL;
1298 mod_ty mod;
1299 PyArena *arena = PyArena_New();
1300 if (arena == NULL)
1301 return NULL;
1303 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1304 if (mod != NULL)
1305 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1306 PyArena_Free(arena);
1307 return ret;
1310 PyObject *
1311 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1312 PyObject *locals, int closeit, PyCompilerFlags *flags)
1314 PyObject *ret;
1315 mod_ty mod;
1316 PyArena *arena = PyArena_New();
1317 if (arena == NULL)
1318 return NULL;
1320 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1321 flags, NULL, arena);
1322 if (closeit)
1323 fclose(fp);
1324 if (mod == NULL) {
1325 PyArena_Free(arena);
1326 return NULL;
1328 ret = run_mod(mod, filename, globals, locals, flags, arena);
1329 PyArena_Free(arena);
1330 return ret;
1333 static PyObject *
1334 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1335 PyCompilerFlags *flags, PyArena *arena)
1337 PyCodeObject *co;
1338 PyObject *v;
1339 co = PyAST_Compile(mod, filename, flags, arena);
1340 if (co == NULL)
1341 return NULL;
1342 v = PyEval_EvalCode(co, globals, locals);
1343 Py_DECREF(co);
1344 return v;
1347 static PyObject *
1348 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1349 PyObject *locals, PyCompilerFlags *flags)
1351 PyCodeObject *co;
1352 PyObject *v;
1353 long magic;
1354 long PyImport_GetMagicNumber(void);
1356 magic = PyMarshal_ReadLongFromFile(fp);
1357 if (magic != PyImport_GetMagicNumber()) {
1358 PyErr_SetString(PyExc_RuntimeError,
1359 "Bad magic number in .pyc file");
1360 return NULL;
1362 (void) PyMarshal_ReadLongFromFile(fp);
1363 v = PyMarshal_ReadLastObjectFromFile(fp);
1364 fclose(fp);
1365 if (v == NULL || !PyCode_Check(v)) {
1366 Py_XDECREF(v);
1367 PyErr_SetString(PyExc_RuntimeError,
1368 "Bad code object in .pyc file");
1369 return NULL;
1371 co = (PyCodeObject *)v;
1372 v = PyEval_EvalCode(co, globals, locals);
1373 if (v && flags)
1374 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1375 Py_DECREF(co);
1376 return v;
1379 PyObject *
1380 Py_CompileStringFlags(const char *str, const char *filename, int start,
1381 PyCompilerFlags *flags)
1383 PyCodeObject *co;
1384 mod_ty mod;
1385 PyArena *arena = PyArena_New();
1386 if (arena == NULL)
1387 return NULL;
1389 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1390 if (mod == NULL) {
1391 PyArena_Free(arena);
1392 return NULL;
1394 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1395 PyObject *result = PyAST_mod2obj(mod);
1396 PyArena_Free(arena);
1397 return result;
1399 co = PyAST_Compile(mod, filename, flags, arena);
1400 PyArena_Free(arena);
1401 return (PyObject *)co;
1404 struct symtable *
1405 Py_SymtableString(const char *str, const char *filename, int start)
1407 struct symtable *st;
1408 mod_ty mod;
1409 PyCompilerFlags flags;
1410 PyArena *arena = PyArena_New();
1411 if (arena == NULL)
1412 return NULL;
1414 flags.cf_flags = 0;
1416 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1417 if (mod == NULL) {
1418 PyArena_Free(arena);
1419 return NULL;
1421 st = PySymtable_Build(mod, filename, 0);
1422 PyArena_Free(arena);
1423 return st;
1426 /* Preferred access to parser is through AST. */
1427 mod_ty
1428 PyParser_ASTFromString(const char *s, const char *filename, int start,
1429 PyCompilerFlags *flags, PyArena *arena)
1431 mod_ty mod;
1432 PyCompilerFlags localflags;
1433 perrdetail err;
1434 int iflags = PARSER_FLAGS(flags);
1436 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1437 &_PyParser_Grammar, start, &err,
1438 &iflags);
1439 if (flags == NULL) {
1440 localflags.cf_flags = 0;
1441 flags = &localflags;
1443 if (n) {
1444 flags->cf_flags |= iflags & PyCF_MASK;
1445 mod = PyAST_FromNode(n, flags, filename, arena);
1446 PyNode_Free(n);
1447 return mod;
1449 else {
1450 err_input(&err);
1451 return NULL;
1455 mod_ty
1456 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1457 char *ps2, PyCompilerFlags *flags, int *errcode,
1458 PyArena *arena)
1460 mod_ty mod;
1461 PyCompilerFlags localflags;
1462 perrdetail err;
1463 int iflags = PARSER_FLAGS(flags);
1465 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1466 start, ps1, ps2, &err, &iflags);
1467 if (flags == NULL) {
1468 localflags.cf_flags = 0;
1469 flags = &localflags;
1471 if (n) {
1472 flags->cf_flags |= iflags & PyCF_MASK;
1473 mod = PyAST_FromNode(n, flags, filename, arena);
1474 PyNode_Free(n);
1475 return mod;
1477 else {
1478 err_input(&err);
1479 if (errcode)
1480 *errcode = err.error;
1481 return NULL;
1485 /* Simplified interface to parsefile -- return node or set exception */
1487 node *
1488 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1490 perrdetail err;
1491 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1492 start, NULL, NULL, &err, flags);
1493 if (n == NULL)
1494 err_input(&err);
1496 return n;
1499 /* Simplified interface to parsestring -- return node or set exception */
1501 node *
1502 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1504 perrdetail err;
1505 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1506 start, &err, flags);
1507 if (n == NULL)
1508 err_input(&err);
1509 return n;
1512 node *
1513 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1514 int start, int flags)
1516 perrdetail err;
1517 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1518 &_PyParser_Grammar, start, &err, flags);
1519 if (n == NULL)
1520 err_input(&err);
1521 return n;
1524 node *
1525 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1527 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1530 /* May want to move a more generalized form of this to parsetok.c or
1531 even parser modules. */
1533 void
1534 PyParser_SetError(perrdetail *err)
1536 err_input(err);
1539 /* Set the error appropriate to the given input error code (see errcode.h) */
1541 static void
1542 err_input(perrdetail *err)
1544 PyObject *v, *w, *errtype;
1545 PyObject* u = NULL;
1546 char *msg = NULL;
1547 errtype = PyExc_SyntaxError;
1548 switch (err->error) {
1549 case E_SYNTAX:
1550 errtype = PyExc_IndentationError;
1551 if (err->expected == INDENT)
1552 msg = "expected an indented block";
1553 else if (err->token == INDENT)
1554 msg = "unexpected indent";
1555 else if (err->token == DEDENT)
1556 msg = "unexpected unindent";
1557 else {
1558 errtype = PyExc_SyntaxError;
1559 msg = "invalid syntax";
1561 break;
1562 case E_TOKEN:
1563 msg = "invalid token";
1564 break;
1565 case E_EOFS:
1566 msg = "EOF while scanning triple-quoted string literal";
1567 break;
1568 case E_EOLS:
1569 msg = "EOL while scanning string literal";
1570 break;
1571 case E_INTR:
1572 if (!PyErr_Occurred())
1573 PyErr_SetNone(PyExc_KeyboardInterrupt);
1574 goto cleanup;
1575 case E_NOMEM:
1576 PyErr_NoMemory();
1577 goto cleanup;
1578 case E_EOF:
1579 msg = "unexpected EOF while parsing";
1580 break;
1581 case E_TABSPACE:
1582 errtype = PyExc_TabError;
1583 msg = "inconsistent use of tabs and spaces in indentation";
1584 break;
1585 case E_OVERFLOW:
1586 msg = "expression too long";
1587 break;
1588 case E_DEDENT:
1589 errtype = PyExc_IndentationError;
1590 msg = "unindent does not match any outer indentation level";
1591 break;
1592 case E_TOODEEP:
1593 errtype = PyExc_IndentationError;
1594 msg = "too many levels of indentation";
1595 break;
1596 case E_DECODE: {
1597 PyObject *type, *value, *tb;
1598 PyErr_Fetch(&type, &value, &tb);
1599 if (value != NULL) {
1600 u = PyObject_Str(value);
1601 if (u != NULL) {
1602 msg = PyString_AsString(u);
1605 if (msg == NULL)
1606 msg = "unknown decode error";
1607 Py_XDECREF(type);
1608 Py_XDECREF(value);
1609 Py_XDECREF(tb);
1610 break;
1612 case E_LINECONT:
1613 msg = "unexpected character after line continuation character";
1614 break;
1615 default:
1616 fprintf(stderr, "error=%d\n", err->error);
1617 msg = "unknown parsing error";
1618 break;
1620 v = Py_BuildValue("(ziiz)", err->filename,
1621 err->lineno, err->offset, err->text);
1622 w = NULL;
1623 if (v != NULL)
1624 w = Py_BuildValue("(sO)", msg, v);
1625 Py_XDECREF(u);
1626 Py_XDECREF(v);
1627 PyErr_SetObject(errtype, w);
1628 Py_XDECREF(w);
1629 cleanup:
1630 if (err->text != NULL) {
1631 PyObject_FREE(err->text);
1632 err->text = NULL;
1636 /* Print fatal error message and abort */
1638 void
1639 Py_FatalError(const char *msg)
1641 fprintf(stderr, "Fatal Python error: %s\n", msg);
1642 fflush(stderr); /* it helps in Windows debug build */
1644 #ifdef MS_WINDOWS
1646 size_t len = strlen(msg);
1647 WCHAR* buffer;
1648 size_t i;
1650 /* Convert the message to wchar_t. This uses a simple one-to-one
1651 conversion, assuming that the this error message actually uses ASCII
1652 only. If this ceases to be true, we will have to convert. */
1653 buffer = alloca( (len+1) * (sizeof *buffer));
1654 for( i=0; i<=len; ++i)
1655 buffer[i] = msg[i];
1656 OutputDebugStringW(L"Fatal Python error: ");
1657 OutputDebugStringW(buffer);
1658 OutputDebugStringW(L"\n");
1660 #ifdef _DEBUG
1661 DebugBreak();
1662 #endif
1663 #endif /* MS_WINDOWS */
1664 abort();
1667 /* Clean up and exit */
1669 #ifdef WITH_THREAD
1670 #include "pythread.h"
1671 #endif
1673 /* Wait until threading._shutdown completes, provided
1674 the threading module was imported in the first place.
1675 The shutdown routine will wait until all non-daemon
1676 "threading" threads have completed. */
1677 static void
1678 wait_for_thread_shutdown(void)
1680 #ifdef WITH_THREAD
1681 PyObject *result;
1682 PyThreadState *tstate = PyThreadState_GET();
1683 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1684 "threading");
1685 if (threading == NULL) {
1686 /* threading not imported */
1687 PyErr_Clear();
1688 return;
1690 result = PyObject_CallMethod(threading, "_shutdown", "");
1691 if (result == NULL)
1692 PyErr_WriteUnraisable(threading);
1693 else
1694 Py_DECREF(result);
1695 Py_DECREF(threading);
1696 #endif
1699 #define NEXITFUNCS 32
1700 static void (*exitfuncs[NEXITFUNCS])(void);
1701 static int nexitfuncs = 0;
1703 int Py_AtExit(void (*func)(void))
1705 if (nexitfuncs >= NEXITFUNCS)
1706 return -1;
1707 exitfuncs[nexitfuncs++] = func;
1708 return 0;
1711 static void
1712 call_sys_exitfunc(void)
1714 PyObject *exitfunc = PySys_GetObject("exitfunc");
1716 if (exitfunc) {
1717 PyObject *res;
1718 Py_INCREF(exitfunc);
1719 PySys_SetObject("exitfunc", (PyObject *)NULL);
1720 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1721 if (res == NULL) {
1722 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1723 PySys_WriteStderr("Error in sys.exitfunc:\n");
1725 PyErr_Print();
1727 Py_DECREF(exitfunc);
1730 if (Py_FlushLine())
1731 PyErr_Clear();
1734 static void
1735 call_ll_exitfuncs(void)
1737 while (nexitfuncs > 0)
1738 (*exitfuncs[--nexitfuncs])();
1740 fflush(stdout);
1741 fflush(stderr);
1744 void
1745 Py_Exit(int sts)
1747 Py_Finalize();
1749 exit(sts);
1752 static void
1753 initsigs(void)
1755 #ifdef SIGPIPE
1756 PyOS_setsig(SIGPIPE, SIG_IGN);
1757 #endif
1758 #ifdef SIGXFZ
1759 PyOS_setsig(SIGXFZ, SIG_IGN);
1760 #endif
1761 #ifdef SIGXFSZ
1762 PyOS_setsig(SIGXFSZ, SIG_IGN);
1763 #endif
1764 PyOS_InitInterrupts(); /* May imply initsignal() */
1769 * The file descriptor fd is considered ``interactive'' if either
1770 * a) isatty(fd) is TRUE, or
1771 * b) the -i flag was given, and the filename associated with
1772 * the descriptor is NULL or "<stdin>" or "???".
1775 Py_FdIsInteractive(FILE *fp, const char *filename)
1777 if (isatty((int)fileno(fp)))
1778 return 1;
1779 if (!Py_InteractiveFlag)
1780 return 0;
1781 return (filename == NULL) ||
1782 (strcmp(filename, "<stdin>") == 0) ||
1783 (strcmp(filename, "???") == 0);
1787 #if defined(USE_STACKCHECK)
1788 #if defined(WIN32) && defined(_MSC_VER)
1790 /* Stack checking for Microsoft C */
1792 #include <malloc.h>
1793 #include <excpt.h>
1796 * Return non-zero when we run out of memory on the stack; zero otherwise.
1799 PyOS_CheckStack(void)
1801 __try {
1802 /* alloca throws a stack overflow exception if there's
1803 not enough space left on the stack */
1804 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1805 return 0;
1806 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1807 EXCEPTION_EXECUTE_HANDLER :
1808 EXCEPTION_CONTINUE_SEARCH) {
1809 int errcode = _resetstkoflw();
1810 if (errcode == 0)
1812 Py_FatalError("Could not reset the stack!");
1815 return 1;
1818 #endif /* WIN32 && _MSC_VER */
1820 /* Alternate implementations can be added here... */
1822 #endif /* USE_STACKCHECK */
1825 /* Wrappers around sigaction() or signal(). */
1827 PyOS_sighandler_t
1828 PyOS_getsig(int sig)
1830 #ifdef HAVE_SIGACTION
1831 struct sigaction context;
1832 if (sigaction(sig, NULL, &context) == -1)
1833 return SIG_ERR;
1834 return context.sa_handler;
1835 #else
1836 PyOS_sighandler_t handler;
1837 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1838 #if defined(_MSC_VER) && _MSC_VER >= 1400
1839 switch (sig) {
1840 /* Only these signals are valid */
1841 case SIGINT:
1842 case SIGILL:
1843 case SIGFPE:
1844 case SIGSEGV:
1845 case SIGTERM:
1846 case SIGBREAK:
1847 case SIGABRT:
1848 break;
1849 /* Don't call signal() with other values or it will assert */
1850 default:
1851 return SIG_ERR;
1853 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1854 handler = signal(sig, SIG_IGN);
1855 if (handler != SIG_ERR)
1856 signal(sig, handler);
1857 return handler;
1858 #endif
1861 PyOS_sighandler_t
1862 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1864 #ifdef HAVE_SIGACTION
1865 struct sigaction context, ocontext;
1866 context.sa_handler = handler;
1867 sigemptyset(&context.sa_mask);
1868 context.sa_flags = 0;
1869 if (sigaction(sig, &context, &ocontext) == -1)
1870 return SIG_ERR;
1871 return ocontext.sa_handler;
1872 #else
1873 PyOS_sighandler_t oldhandler;
1874 oldhandler = signal(sig, handler);
1875 #ifdef HAVE_SIGINTERRUPT
1876 siginterrupt(sig, 1);
1877 #endif
1878 return oldhandler;
1879 #endif
1882 /* Deprecated C API functions still provided for binary compatiblity */
1884 #undef PyParser_SimpleParseFile
1885 PyAPI_FUNC(node *)
1886 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1888 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1891 #undef PyParser_SimpleParseString
1892 PyAPI_FUNC(node *)
1893 PyParser_SimpleParseString(const char *str, int start)
1895 return PyParser_SimpleParseStringFlags(str, start, 0);
1898 #undef PyRun_AnyFile
1899 PyAPI_FUNC(int)
1900 PyRun_AnyFile(FILE *fp, const char *name)
1902 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1905 #undef PyRun_AnyFileEx
1906 PyAPI_FUNC(int)
1907 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1909 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1912 #undef PyRun_AnyFileFlags
1913 PyAPI_FUNC(int)
1914 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1916 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1919 #undef PyRun_File
1920 PyAPI_FUNC(PyObject *)
1921 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1923 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1926 #undef PyRun_FileEx
1927 PyAPI_FUNC(PyObject *)
1928 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1930 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1933 #undef PyRun_FileFlags
1934 PyAPI_FUNC(PyObject *)
1935 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1936 PyCompilerFlags *flags)
1938 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1941 #undef PyRun_SimpleFile
1942 PyAPI_FUNC(int)
1943 PyRun_SimpleFile(FILE *f, const char *p)
1945 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1948 #undef PyRun_SimpleFileEx
1949 PyAPI_FUNC(int)
1950 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1952 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1956 #undef PyRun_String
1957 PyAPI_FUNC(PyObject *)
1958 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1960 return PyRun_StringFlags(str, s, g, l, NULL);
1963 #undef PyRun_SimpleString
1964 PyAPI_FUNC(int)
1965 PyRun_SimpleString(const char *s)
1967 return PyRun_SimpleStringFlags(s, NULL);
1970 #undef Py_CompileString
1971 PyAPI_FUNC(PyObject *)
1972 Py_CompileString(const char *str, const char *p, int s)
1974 return Py_CompileStringFlags(str, p, s, NULL);
1977 #undef PyRun_InteractiveOne
1978 PyAPI_FUNC(int)
1979 PyRun_InteractiveOne(FILE *f, const char *p)
1981 return PyRun_InteractiveOneFlags(f, p, NULL);
1984 #undef PyRun_InteractiveLoop
1985 PyAPI_FUNC(int)
1986 PyRun_InteractiveLoop(FILE *f, const char *p)
1988 return PyRun_InteractiveLoopFlags(f, p, NULL);
1991 #ifdef __cplusplus
1993 #endif