Updated to reflect change in logging.config to remove out-of-date comment in _install...
[python.git] / Python / pythonrun.c
blob785519bdc3600d7974132015fd8dec38afa7a2da
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"
21 #ifdef HAVE_SIGNAL_H
22 #include <signal.h>
23 #endif
25 #ifdef HAVE_LANGINFO_H
26 #include <locale.h>
27 #include <langinfo.h>
28 #endif
30 #ifdef MS_WINDOWS
31 #undef BYTE
32 #include "windows.h"
33 #endif
35 #ifndef Py_REF_DEBUG
36 #define PRINT_TOTAL_REFS()
37 #else /* Py_REF_DEBUG */
38 #define PRINT_TOTAL_REFS() fprintf(stderr, \
39 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
40 _Py_GetRefTotal())
41 #endif
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
47 extern char *Py_GetPath(void);
49 extern grammar _PyParser_Grammar; /* From graminit.c */
51 /* Forward */
52 static void initmain(void);
53 static void initsite(void);
54 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
55 PyCompilerFlags *, PyArena *);
56 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
57 PyCompilerFlags *);
58 static void err_input(perrdetail *);
59 static void initsigs(void);
60 static void call_sys_exitfunc(void);
61 static void call_ll_exitfuncs(void);
62 extern void _PyUnicode_Init(void);
63 extern void _PyUnicode_Fini(void);
65 #ifdef WITH_THREAD
66 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
67 extern void _PyGILState_Fini(void);
68 #endif /* WITH_THREAD */
70 int Py_DebugFlag; /* Needed by parser.c */
71 int Py_VerboseFlag; /* Needed by import.c */
72 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
73 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
74 int Py_NoSiteFlag; /* Suppress 'import site' */
75 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
76 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
77 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
78 int Py_FrozenFlag; /* Needed by getpath.c */
79 int Py_UnicodeFlag = 0; /* Needed by compile.c */
80 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
81 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
82 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
83 true divisions (which they will be in 2.3). */
84 int _Py_QnewFlag = 0;
85 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
87 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
88 since _warnings is builtin. This API should not be used. */
89 PyObject *
90 PyModule_GetWarningsModule(void)
92 return PyImport_ImportModule("warnings");
95 static int initialized = 0;
97 /* API to access the initialized flag -- useful for esoteric use */
99 int
100 Py_IsInitialized(void)
102 return initialized;
105 /* Global initializations. Can be undone by Py_Finalize(). Don't
106 call this twice without an intervening Py_Finalize() call. When
107 initializations fail, a fatal error is issued and the function does
108 not return. On return, the first thread and interpreter state have
109 been created.
111 Locking: you must hold the interpreter lock while calling this.
112 (If the lock has not yet been initialized, that's equivalent to
113 having the lock, but you cannot use multiple threads.)
117 static int
118 add_flag(int flag, const char *envs)
120 int env = atoi(envs);
121 if (flag < env)
122 flag = env;
123 if (flag < 1)
124 flag = 1;
125 return flag;
128 void
129 Py_InitializeEx(int install_sigs)
131 PyInterpreterState *interp;
132 PyThreadState *tstate;
133 PyObject *bimod, *sysmod;
134 char *p;
135 char *icodeset; /* On Windows, input codeset may theoretically
136 differ from output codeset. */
137 char *codeset = NULL;
138 char *errors = NULL;
139 int free_codeset = 0;
140 int overridden = 0;
141 PyObject *sys_stream, *sys_isatty;
142 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
143 char *saved_locale, *loc_codeset;
144 #endif
145 #ifdef MS_WINDOWS
146 char ibuf[128];
147 char buf[128];
148 #endif
149 extern void _Py_ReadyTypes(void);
151 if (initialized)
152 return;
153 initialized = 1;
155 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
156 Py_DebugFlag = add_flag(Py_DebugFlag, p);
157 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
158 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
159 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
160 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
161 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
162 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
164 interp = PyInterpreterState_New();
165 if (interp == NULL)
166 Py_FatalError("Py_Initialize: can't make first interpreter");
168 tstate = PyThreadState_New(interp);
169 if (tstate == NULL)
170 Py_FatalError("Py_Initialize: can't make first thread");
171 (void) PyThreadState_Swap(tstate);
173 _Py_ReadyTypes();
175 if (!_PyFrame_Init())
176 Py_FatalError("Py_Initialize: can't init frames");
178 if (!_PyInt_Init())
179 Py_FatalError("Py_Initialize: can't init ints");
181 if (!PyByteArray_Init())
182 Py_FatalError("Py_Initialize: can't init bytearray");
184 _PyFloat_Init();
186 interp->modules = PyDict_New();
187 if (interp->modules == NULL)
188 Py_FatalError("Py_Initialize: can't make modules dictionary");
189 interp->modules_reloading = PyDict_New();
190 if (interp->modules_reloading == NULL)
191 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
193 #ifdef Py_USING_UNICODE
194 /* Init Unicode implementation; relies on the codec registry */
195 _PyUnicode_Init();
196 #endif
198 bimod = _PyBuiltin_Init();
199 if (bimod == NULL)
200 Py_FatalError("Py_Initialize: can't initialize __builtin__");
201 interp->builtins = PyModule_GetDict(bimod);
202 if (interp->builtins == NULL)
203 Py_FatalError("Py_Initialize: can't initialize builtins dict");
204 Py_INCREF(interp->builtins);
206 sysmod = _PySys_Init();
207 if (sysmod == NULL)
208 Py_FatalError("Py_Initialize: can't initialize sys");
209 interp->sysdict = PyModule_GetDict(sysmod);
210 if (interp->sysdict == NULL)
211 Py_FatalError("Py_Initialize: can't initialize sys dict");
212 Py_INCREF(interp->sysdict);
213 _PyImport_FixupExtension("sys", "sys");
214 PySys_SetPath(Py_GetPath());
215 PyDict_SetItemString(interp->sysdict, "modules",
216 interp->modules);
218 _PyImport_Init();
220 /* initialize builtin exceptions */
221 _PyExc_Init();
222 _PyImport_FixupExtension("exceptions", "exceptions");
224 /* phase 2 of builtins */
225 _PyImport_FixupExtension("__builtin__", "__builtin__");
227 _PyImportHooks_Init();
229 if (install_sigs)
230 initsigs(); /* Signal handling stuff, including initintr() */
232 /* Initialize warnings. */
233 _PyWarnings_Init();
234 if (PySys_HasWarnOptions()) {
235 PyObject *warnings_module = PyImport_ImportModule("warnings");
236 if (!warnings_module)
237 PyErr_Clear();
238 Py_XDECREF(warnings_module);
241 initmain(); /* Module __main__ */
242 if (!Py_NoSiteFlag)
243 initsite(); /* Module site */
245 /* auto-thread-state API, if available */
246 #ifdef WITH_THREAD
247 _PyGILState_Init(interp, tstate);
248 #endif /* WITH_THREAD */
250 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
251 p = icodeset = codeset = strdup(p);
252 free_codeset = 1;
253 errors = strchr(p, ':');
254 if (errors) {
255 *errors = '\0';
256 errors++;
258 overridden = 1;
261 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
262 /* On Unix, set the file system encoding according to the
263 user's preference, if the CODESET names a well-known
264 Python codec, and Py_FileSystemDefaultEncoding isn't
265 initialized by other means. Also set the encoding of
266 stdin and stdout if these are terminals, unless overridden. */
268 if (!overridden || !Py_FileSystemDefaultEncoding) {
269 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
270 setlocale(LC_CTYPE, "");
271 loc_codeset = nl_langinfo(CODESET);
272 if (loc_codeset && *loc_codeset) {
273 PyObject *enc = PyCodec_Encoder(loc_codeset);
274 if (enc) {
275 loc_codeset = strdup(loc_codeset);
276 Py_DECREF(enc);
277 } else {
278 loc_codeset = NULL;
279 PyErr_Clear();
281 } else
282 loc_codeset = NULL;
283 setlocale(LC_CTYPE, saved_locale);
284 free(saved_locale);
286 if (!overridden) {
287 codeset = icodeset = loc_codeset;
288 free_codeset = 1;
291 /* Initialize Py_FileSystemDefaultEncoding from
292 locale even if PYTHONIOENCODING is set. */
293 if (!Py_FileSystemDefaultEncoding) {
294 Py_FileSystemDefaultEncoding = loc_codeset;
295 if (!overridden)
296 free_codeset = 0;
299 #endif
301 #ifdef MS_WINDOWS
302 if (!overridden) {
303 icodeset = ibuf;
304 codeset = buf;
305 sprintf(ibuf, "cp%d", GetConsoleCP());
306 sprintf(buf, "cp%d", GetConsoleOutputCP());
308 #endif
310 if (codeset) {
311 sys_stream = PySys_GetObject("stdin");
312 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
313 if (!sys_isatty)
314 PyErr_Clear();
315 if ((overridden ||
316 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
317 PyFile_Check(sys_stream)) {
318 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
319 Py_FatalError("Cannot set codeset of stdin");
321 Py_XDECREF(sys_isatty);
323 sys_stream = PySys_GetObject("stdout");
324 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
325 if (!sys_isatty)
326 PyErr_Clear();
327 if ((overridden ||
328 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
329 PyFile_Check(sys_stream)) {
330 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
331 Py_FatalError("Cannot set codeset of stdout");
333 Py_XDECREF(sys_isatty);
335 sys_stream = PySys_GetObject("stderr");
336 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
337 if (!sys_isatty)
338 PyErr_Clear();
339 if((overridden ||
340 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
341 PyFile_Check(sys_stream)) {
342 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
343 Py_FatalError("Cannot set codeset of stderr");
345 Py_XDECREF(sys_isatty);
347 if (free_codeset)
348 free(codeset);
352 void
353 Py_Initialize(void)
355 Py_InitializeEx(1);
359 #ifdef COUNT_ALLOCS
360 extern void dump_counts(FILE*);
361 #endif
363 /* Undo the effect of Py_Initialize().
365 Beware: if multiple interpreter and/or thread states exist, these
366 are not wiped out; only the current thread and interpreter state
367 are deleted. But since everything else is deleted, those other
368 interpreter and thread states should no longer be used.
370 (XXX We should do better, e.g. wipe out all interpreters and
371 threads.)
373 Locking: as above.
377 void
378 Py_Finalize(void)
380 PyInterpreterState *interp;
381 PyThreadState *tstate;
383 if (!initialized)
384 return;
386 /* The interpreter is still entirely intact at this point, and the
387 * exit funcs may be relying on that. In particular, if some thread
388 * or exit func is still waiting to do an import, the import machinery
389 * expects Py_IsInitialized() to return true. So don't say the
390 * interpreter is uninitialized until after the exit funcs have run.
391 * Note that Threading.py uses an exit func to do a join on all the
392 * threads created thru it, so this also protects pending imports in
393 * the threads created via Threading.
395 call_sys_exitfunc();
396 initialized = 0;
398 /* Get current thread state and interpreter pointer */
399 tstate = PyThreadState_GET();
400 interp = tstate->interp;
402 /* Disable signal handling */
403 PyOS_FiniInterrupts();
405 /* Clear type lookup cache */
406 PyType_ClearCache();
408 /* Collect garbage. This may call finalizers; it's nice to call these
409 * before all modules are destroyed.
410 * XXX If a __del__ or weakref callback is triggered here, and tries to
411 * XXX import a module, bad things can happen, because Python no
412 * XXX longer believes it's initialized.
413 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
414 * XXX is easy to provoke that way. I've also seen, e.g.,
415 * XXX Exception exceptions.ImportError: 'No module named sha'
416 * XXX in <function callback at 0x008F5718> ignored
417 * XXX but I'm unclear on exactly how that one happens. In any case,
418 * XXX I haven't seen a real-life report of either of these.
420 PyGC_Collect();
421 #ifdef COUNT_ALLOCS
422 /* With COUNT_ALLOCS, it helps to run GC multiple times:
423 each collection might release some types from the type
424 list, so they become garbage. */
425 while (PyGC_Collect() > 0)
426 /* nothing */;
427 #endif
429 /* Destroy all modules */
430 PyImport_Cleanup();
432 /* Collect final garbage. This disposes of cycles created by
433 * new-style class definitions, for example.
434 * XXX This is disabled because it caused too many problems. If
435 * XXX a __del__ or weakref callback triggers here, Python code has
436 * XXX a hard time running, because even the sys module has been
437 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
438 * XXX One symptom is a sequence of information-free messages
439 * XXX coming from threads (if a __del__ or callback is invoked,
440 * XXX other threads can execute too, and any exception they encounter
441 * XXX triggers a comedy of errors as subsystem after subsystem
442 * XXX fails to find what it *expects* to find in sys to help report
443 * XXX the exception and consequent unexpected failures). I've also
444 * XXX seen segfaults then, after adding print statements to the
445 * XXX Python code getting called.
447 #if 0
448 PyGC_Collect();
449 #endif
451 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
452 _PyImport_Fini();
454 /* Debugging stuff */
455 #ifdef COUNT_ALLOCS
456 dump_counts(stdout);
457 #endif
459 PRINT_TOTAL_REFS();
461 #ifdef Py_TRACE_REFS
462 /* Display all objects still alive -- this can invoke arbitrary
463 * __repr__ overrides, so requires a mostly-intact interpreter.
464 * Alas, a lot of stuff may still be alive now that will be cleaned
465 * up later.
467 if (Py_GETENV("PYTHONDUMPREFS"))
468 _Py_PrintReferences(stderr);
469 #endif /* Py_TRACE_REFS */
471 /* Clear interpreter state */
472 PyInterpreterState_Clear(interp);
474 /* Now we decref the exception classes. After this point nothing
475 can raise an exception. That's okay, because each Fini() method
476 below has been checked to make sure no exceptions are ever
477 raised.
480 _PyExc_Fini();
482 /* Cleanup auto-thread-state */
483 #ifdef WITH_THREAD
484 _PyGILState_Fini();
485 #endif /* WITH_THREAD */
487 /* Delete current thread */
488 PyThreadState_Swap(NULL);
489 PyInterpreterState_Delete(interp);
491 /* Sundry finalizers */
492 PyMethod_Fini();
493 PyFrame_Fini();
494 PyCFunction_Fini();
495 PyTuple_Fini();
496 PyList_Fini();
497 PySet_Fini();
498 PyString_Fini();
499 PyByteArray_Fini();
500 PyInt_Fini();
501 PyFloat_Fini();
502 PyDict_Fini();
504 #ifdef Py_USING_UNICODE
505 /* Cleanup Unicode implementation */
506 _PyUnicode_Fini();
507 #endif
509 /* XXX Still allocated:
510 - various static ad-hoc pointers to interned strings
511 - int and float free list blocks
512 - whatever various modules and libraries allocate
515 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
517 #ifdef Py_TRACE_REFS
518 /* Display addresses (& refcnts) of all objects still alive.
519 * An address can be used to find the repr of the object, printed
520 * above by _Py_PrintReferences.
522 if (Py_GETENV("PYTHONDUMPREFS"))
523 _Py_PrintReferenceAddresses(stderr);
524 #endif /* Py_TRACE_REFS */
525 #ifdef PYMALLOC_DEBUG
526 if (Py_GETENV("PYTHONMALLOCSTATS"))
527 _PyObject_DebugMallocStats();
528 #endif
530 call_ll_exitfuncs();
533 /* Create and initialize a new interpreter and thread, and return the
534 new thread. This requires that Py_Initialize() has been called
535 first.
537 Unsuccessful initialization yields a NULL pointer. Note that *no*
538 exception information is available even in this case -- the
539 exception information is held in the thread, and there is no
540 thread.
542 Locking: as above.
546 PyThreadState *
547 Py_NewInterpreter(void)
549 PyInterpreterState *interp;
550 PyThreadState *tstate, *save_tstate;
551 PyObject *bimod, *sysmod;
553 if (!initialized)
554 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
556 interp = PyInterpreterState_New();
557 if (interp == NULL)
558 return NULL;
560 tstate = PyThreadState_New(interp);
561 if (tstate == NULL) {
562 PyInterpreterState_Delete(interp);
563 return NULL;
566 save_tstate = PyThreadState_Swap(tstate);
568 /* XXX The following is lax in error checking */
570 interp->modules = PyDict_New();
571 interp->modules_reloading = PyDict_New();
573 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
574 if (bimod != NULL) {
575 interp->builtins = PyModule_GetDict(bimod);
576 if (interp->builtins == NULL)
577 goto handle_error;
578 Py_INCREF(interp->builtins);
580 sysmod = _PyImport_FindExtension("sys", "sys");
581 if (bimod != NULL && sysmod != NULL) {
582 interp->sysdict = PyModule_GetDict(sysmod);
583 if (interp->sysdict == NULL)
584 goto handle_error;
585 Py_INCREF(interp->sysdict);
586 PySys_SetPath(Py_GetPath());
587 PyDict_SetItemString(interp->sysdict, "modules",
588 interp->modules);
589 _PyImportHooks_Init();
590 initmain();
591 if (!Py_NoSiteFlag)
592 initsite();
595 if (!PyErr_Occurred())
596 return tstate;
598 handle_error:
599 /* Oops, it didn't work. Undo it all. */
601 PyErr_Print();
602 PyThreadState_Clear(tstate);
603 PyThreadState_Swap(save_tstate);
604 PyThreadState_Delete(tstate);
605 PyInterpreterState_Delete(interp);
607 return NULL;
610 /* Delete an interpreter and its last thread. This requires that the
611 given thread state is current, that the thread has no remaining
612 frames, and that it is its interpreter's only remaining thread.
613 It is a fatal error to violate these constraints.
615 (Py_Finalize() doesn't have these constraints -- it zaps
616 everything, regardless.)
618 Locking: as above.
622 void
623 Py_EndInterpreter(PyThreadState *tstate)
625 PyInterpreterState *interp = tstate->interp;
627 if (tstate != PyThreadState_GET())
628 Py_FatalError("Py_EndInterpreter: thread is not current");
629 if (tstate->frame != NULL)
630 Py_FatalError("Py_EndInterpreter: thread still has a frame");
631 if (tstate != interp->tstate_head || tstate->next != NULL)
632 Py_FatalError("Py_EndInterpreter: not the last thread");
634 PyImport_Cleanup();
635 PyInterpreterState_Clear(interp);
636 PyThreadState_Swap(NULL);
637 PyInterpreterState_Delete(interp);
640 static char *progname = "python";
642 void
643 Py_SetProgramName(char *pn)
645 if (pn && *pn)
646 progname = pn;
649 char *
650 Py_GetProgramName(void)
652 return progname;
655 static char *default_home = NULL;
657 void
658 Py_SetPythonHome(char *home)
660 default_home = home;
663 char *
664 Py_GetPythonHome(void)
666 char *home = default_home;
667 if (home == NULL && !Py_IgnoreEnvironmentFlag)
668 home = Py_GETENV("PYTHONHOME");
669 return home;
672 /* Create __main__ module */
674 static void
675 initmain(void)
677 PyObject *m, *d;
678 m = PyImport_AddModule("__main__");
679 if (m == NULL)
680 Py_FatalError("can't create __main__ module");
681 d = PyModule_GetDict(m);
682 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
683 PyObject *bimod = PyImport_ImportModule("__builtin__");
684 if (bimod == NULL ||
685 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
686 Py_FatalError("can't add __builtins__ to __main__");
687 Py_DECREF(bimod);
691 /* Import the site module (not into __main__ though) */
693 static void
694 initsite(void)
696 PyObject *m, *f;
697 m = PyImport_ImportModule("site");
698 if (m == NULL) {
699 f = PySys_GetObject("stderr");
700 if (Py_VerboseFlag) {
701 PyFile_WriteString(
702 "'import site' failed; traceback:\n", f);
703 PyErr_Print();
705 else {
706 PyFile_WriteString(
707 "'import site' failed; use -v for traceback\n", f);
708 PyErr_Clear();
711 else {
712 Py_DECREF(m);
716 /* Parse input from a file and execute it */
719 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
720 PyCompilerFlags *flags)
722 if (filename == NULL)
723 filename = "???";
724 if (Py_FdIsInteractive(fp, filename)) {
725 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
726 if (closeit)
727 fclose(fp);
728 return err;
730 else
731 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
735 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
737 PyObject *v;
738 int ret;
739 PyCompilerFlags local_flags;
741 if (flags == NULL) {
742 flags = &local_flags;
743 local_flags.cf_flags = 0;
745 v = PySys_GetObject("ps1");
746 if (v == NULL) {
747 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
748 Py_XDECREF(v);
750 v = PySys_GetObject("ps2");
751 if (v == NULL) {
752 PySys_SetObject("ps2", v = PyString_FromString("... "));
753 Py_XDECREF(v);
755 for (;;) {
756 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
757 PRINT_TOTAL_REFS();
758 if (ret == E_EOF)
759 return 0;
761 if (ret == E_NOMEM)
762 return -1;
767 #if 0
768 /* compute parser flags based on compiler flags */
769 #define PARSER_FLAGS(flags) \
770 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
771 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
772 #endif
773 #if 1
774 /* Keep an example of flags with future keyword support. */
775 #define PARSER_FLAGS(flags) \
776 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
777 PyPARSE_DONT_IMPLY_DEDENT : 0) \
778 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
779 PyPARSE_PRINT_IS_FUNCTION : 0) \
780 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
781 PyPARSE_UNICODE_LITERALS : 0) \
782 ) : 0)
783 #endif
786 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
788 PyObject *m, *d, *v, *w;
789 mod_ty mod;
790 PyArena *arena;
791 char *ps1 = "", *ps2 = "";
792 int errcode = 0;
794 v = PySys_GetObject("ps1");
795 if (v != NULL) {
796 v = PyObject_Str(v);
797 if (v == NULL)
798 PyErr_Clear();
799 else if (PyString_Check(v))
800 ps1 = PyString_AsString(v);
802 w = PySys_GetObject("ps2");
803 if (w != NULL) {
804 w = PyObject_Str(w);
805 if (w == NULL)
806 PyErr_Clear();
807 else if (PyString_Check(w))
808 ps2 = PyString_AsString(w);
810 arena = PyArena_New();
811 if (arena == NULL) {
812 Py_XDECREF(v);
813 Py_XDECREF(w);
814 return -1;
816 mod = PyParser_ASTFromFile(fp, filename,
817 Py_single_input, ps1, ps2,
818 flags, &errcode, arena);
819 Py_XDECREF(v);
820 Py_XDECREF(w);
821 if (mod == NULL) {
822 PyArena_Free(arena);
823 if (errcode == E_EOF) {
824 PyErr_Clear();
825 return E_EOF;
827 PyErr_Print();
828 return -1;
830 m = PyImport_AddModule("__main__");
831 if (m == NULL) {
832 PyArena_Free(arena);
833 return -1;
835 d = PyModule_GetDict(m);
836 v = run_mod(mod, filename, d, d, flags, arena);
837 PyArena_Free(arena);
838 if (v == NULL) {
839 PyErr_Print();
840 return -1;
842 Py_DECREF(v);
843 if (Py_FlushLine())
844 PyErr_Clear();
845 return 0;
848 /* Check whether a file maybe a pyc file: Look at the extension,
849 the file type, and, if we may close it, at the first few bytes. */
851 static int
852 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
854 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
855 return 1;
857 /* Only look into the file if we are allowed to close it, since
858 it then should also be seekable. */
859 if (closeit) {
860 /* Read only two bytes of the magic. If the file was opened in
861 text mode, the bytes 3 and 4 of the magic (\r\n) might not
862 be read as they are on disk. */
863 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
864 unsigned char buf[2];
865 /* Mess: In case of -x, the stream is NOT at its start now,
866 and ungetc() was used to push back the first newline,
867 which makes the current stream position formally undefined,
868 and a x-platform nightmare.
869 Unfortunately, we have no direct way to know whether -x
870 was specified. So we use a terrible hack: if the current
871 stream position is not 0, we assume -x was specified, and
872 give up. Bug 132850 on SourceForge spells out the
873 hopelessness of trying anything else (fseek and ftell
874 don't work predictably x-platform for text-mode files).
876 int ispyc = 0;
877 if (ftell(fp) == 0) {
878 if (fread(buf, 1, 2, fp) == 2 &&
879 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
880 ispyc = 1;
881 rewind(fp);
883 return ispyc;
885 return 0;
889 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
890 PyCompilerFlags *flags)
892 PyObject *m, *d, *v;
893 const char *ext;
894 int set_file_name = 0, ret;
896 m = PyImport_AddModule("__main__");
897 if (m == NULL)
898 return -1;
899 d = PyModule_GetDict(m);
900 if (PyDict_GetItemString(d, "__file__") == NULL) {
901 PyObject *f = PyString_FromString(filename);
902 if (f == NULL)
903 return -1;
904 if (PyDict_SetItemString(d, "__file__", f) < 0) {
905 Py_DECREF(f);
906 return -1;
908 set_file_name = 1;
909 Py_DECREF(f);
911 ext = filename + strlen(filename) - 4;
912 if (maybe_pyc_file(fp, filename, ext, closeit)) {
913 /* Try to run a pyc file. First, re-open in binary */
914 if (closeit)
915 fclose(fp);
916 if ((fp = fopen(filename, "rb")) == NULL) {
917 fprintf(stderr, "python: Can't reopen .pyc file\n");
918 ret = -1;
919 goto done;
921 /* Turn on optimization if a .pyo file is given */
922 if (strcmp(ext, ".pyo") == 0)
923 Py_OptimizeFlag = 1;
924 v = run_pyc_file(fp, filename, d, d, flags);
925 } else {
926 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
927 closeit, flags);
929 if (v == NULL) {
930 PyErr_Print();
931 ret = -1;
932 goto done;
934 Py_DECREF(v);
935 if (Py_FlushLine())
936 PyErr_Clear();
937 ret = 0;
938 done:
939 if (set_file_name && PyDict_DelItemString(d, "__file__"))
940 PyErr_Clear();
941 return ret;
945 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
947 PyObject *m, *d, *v;
948 m = PyImport_AddModule("__main__");
949 if (m == NULL)
950 return -1;
951 d = PyModule_GetDict(m);
952 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
953 if (v == NULL) {
954 PyErr_Print();
955 return -1;
957 Py_DECREF(v);
958 if (Py_FlushLine())
959 PyErr_Clear();
960 return 0;
963 static int
964 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
965 int *lineno, int *offset, const char **text)
967 long hold;
968 PyObject *v;
970 /* old style errors */
971 if (PyTuple_Check(err))
972 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
973 lineno, offset, text);
975 /* new style errors. `err' is an instance */
977 if (! (v = PyObject_GetAttrString(err, "msg")))
978 goto finally;
979 *message = v;
981 if (!(v = PyObject_GetAttrString(err, "filename")))
982 goto finally;
983 if (v == Py_None)
984 *filename = NULL;
985 else if (! (*filename = PyString_AsString(v)))
986 goto finally;
988 Py_DECREF(v);
989 if (!(v = PyObject_GetAttrString(err, "lineno")))
990 goto finally;
991 hold = PyInt_AsLong(v);
992 Py_DECREF(v);
993 v = NULL;
994 if (hold < 0 && PyErr_Occurred())
995 goto finally;
996 *lineno = (int)hold;
998 if (!(v = PyObject_GetAttrString(err, "offset")))
999 goto finally;
1000 if (v == Py_None) {
1001 *offset = -1;
1002 Py_DECREF(v);
1003 v = NULL;
1004 } else {
1005 hold = PyInt_AsLong(v);
1006 Py_DECREF(v);
1007 v = NULL;
1008 if (hold < 0 && PyErr_Occurred())
1009 goto finally;
1010 *offset = (int)hold;
1013 if (!(v = PyObject_GetAttrString(err, "text")))
1014 goto finally;
1015 if (v == Py_None)
1016 *text = NULL;
1017 else if (! (*text = PyString_AsString(v)))
1018 goto finally;
1019 Py_DECREF(v);
1020 return 1;
1022 finally:
1023 Py_XDECREF(v);
1024 return 0;
1027 void
1028 PyErr_Print(void)
1030 PyErr_PrintEx(1);
1033 static void
1034 print_error_text(PyObject *f, int offset, const char *text)
1036 char *nl;
1037 if (offset >= 0) {
1038 if (offset > 0 && offset == (int)strlen(text))
1039 offset--;
1040 for (;;) {
1041 nl = strchr(text, '\n');
1042 if (nl == NULL || nl-text >= offset)
1043 break;
1044 offset -= (int)(nl+1-text);
1045 text = nl+1;
1047 while (*text == ' ' || *text == '\t') {
1048 text++;
1049 offset--;
1052 PyFile_WriteString(" ", f);
1053 PyFile_WriteString(text, f);
1054 if (*text == '\0' || text[strlen(text)-1] != '\n')
1055 PyFile_WriteString("\n", f);
1056 if (offset == -1)
1057 return;
1058 PyFile_WriteString(" ", f);
1059 offset--;
1060 while (offset > 0) {
1061 PyFile_WriteString(" ", f);
1062 offset--;
1064 PyFile_WriteString("^\n", f);
1067 static void
1068 handle_system_exit(void)
1070 PyObject *exception, *value, *tb;
1071 int exitcode = 0;
1073 if (Py_InspectFlag)
1074 /* Don't exit if -i flag was given. This flag is set to 0
1075 * when entering interactive mode for inspecting. */
1076 return;
1078 PyErr_Fetch(&exception, &value, &tb);
1079 if (Py_FlushLine())
1080 PyErr_Clear();
1081 fflush(stdout);
1082 if (value == NULL || value == Py_None)
1083 goto done;
1084 if (PyExceptionInstance_Check(value)) {
1085 /* The error code should be in the `code' attribute. */
1086 PyObject *code = PyObject_GetAttrString(value, "code");
1087 if (code) {
1088 Py_DECREF(value);
1089 value = code;
1090 if (value == Py_None)
1091 goto done;
1093 /* If we failed to dig out the 'code' attribute,
1094 just let the else clause below print the error. */
1096 if (PyInt_Check(value))
1097 exitcode = (int)PyInt_AsLong(value);
1098 else {
1099 PyObject_Print(value, stderr, Py_PRINT_RAW);
1100 PySys_WriteStderr("\n");
1101 exitcode = 1;
1103 done:
1104 /* Restore and clear the exception info, in order to properly decref
1105 * the exception, value, and traceback. If we just exit instead,
1106 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1107 * some finalizers from running.
1109 PyErr_Restore(exception, value, tb);
1110 PyErr_Clear();
1111 Py_Exit(exitcode);
1112 /* NOTREACHED */
1115 void
1116 PyErr_PrintEx(int set_sys_last_vars)
1118 PyObject *exception, *v, *tb, *hook;
1120 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1121 handle_system_exit();
1123 PyErr_Fetch(&exception, &v, &tb);
1124 if (exception == NULL)
1125 return;
1126 PyErr_NormalizeException(&exception, &v, &tb);
1127 if (exception == NULL)
1128 return;
1129 /* Now we know v != NULL too */
1130 if (set_sys_last_vars) {
1131 PySys_SetObject("last_type", exception);
1132 PySys_SetObject("last_value", v);
1133 PySys_SetObject("last_traceback", tb);
1135 hook = PySys_GetObject("excepthook");
1136 if (hook) {
1137 PyObject *args = PyTuple_Pack(3,
1138 exception, v, tb ? tb : Py_None);
1139 PyObject *result = PyEval_CallObject(hook, args);
1140 if (result == NULL) {
1141 PyObject *exception2, *v2, *tb2;
1142 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1143 handle_system_exit();
1145 PyErr_Fetch(&exception2, &v2, &tb2);
1146 PyErr_NormalizeException(&exception2, &v2, &tb2);
1147 /* It should not be possible for exception2 or v2
1148 to be NULL. However PyErr_Display() can't
1149 tolerate NULLs, so just be safe. */
1150 if (exception2 == NULL) {
1151 exception2 = Py_None;
1152 Py_INCREF(exception2);
1154 if (v2 == NULL) {
1155 v2 = Py_None;
1156 Py_INCREF(v2);
1158 if (Py_FlushLine())
1159 PyErr_Clear();
1160 fflush(stdout);
1161 PySys_WriteStderr("Error in sys.excepthook:\n");
1162 PyErr_Display(exception2, v2, tb2);
1163 PySys_WriteStderr("\nOriginal exception was:\n");
1164 PyErr_Display(exception, v, tb);
1165 Py_DECREF(exception2);
1166 Py_DECREF(v2);
1167 Py_XDECREF(tb2);
1169 Py_XDECREF(result);
1170 Py_XDECREF(args);
1171 } else {
1172 PySys_WriteStderr("sys.excepthook is missing\n");
1173 PyErr_Display(exception, v, tb);
1175 Py_XDECREF(exception);
1176 Py_XDECREF(v);
1177 Py_XDECREF(tb);
1180 void
1181 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1183 int err = 0;
1184 PyObject *f = PySys_GetObject("stderr");
1185 Py_INCREF(value);
1186 if (f == NULL)
1187 fprintf(stderr, "lost sys.stderr\n");
1188 else {
1189 if (Py_FlushLine())
1190 PyErr_Clear();
1191 fflush(stdout);
1192 if (tb && tb != Py_None)
1193 err = PyTraceBack_Print(tb, f);
1194 if (err == 0 &&
1195 PyObject_HasAttrString(value, "print_file_and_line"))
1197 PyObject *message;
1198 const char *filename, *text;
1199 int lineno, offset;
1200 if (!parse_syntax_error(value, &message, &filename,
1201 &lineno, &offset, &text))
1202 PyErr_Clear();
1203 else {
1204 char buf[10];
1205 PyFile_WriteString(" File \"", f);
1206 if (filename == NULL)
1207 PyFile_WriteString("<string>", f);
1208 else
1209 PyFile_WriteString(filename, f);
1210 PyFile_WriteString("\", line ", f);
1211 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1212 PyFile_WriteString(buf, f);
1213 PyFile_WriteString("\n", f);
1214 if (text != NULL)
1215 print_error_text(f, offset, text);
1216 Py_DECREF(value);
1217 value = message;
1218 /* Can't be bothered to check all those
1219 PyFile_WriteString() calls */
1220 if (PyErr_Occurred())
1221 err = -1;
1224 if (err) {
1225 /* Don't do anything else */
1227 else if (PyExceptionClass_Check(exception)) {
1228 PyObject* moduleName;
1229 char* className = PyExceptionClass_Name(exception);
1230 if (className != NULL) {
1231 char *dot = strrchr(className, '.');
1232 if (dot != NULL)
1233 className = dot+1;
1236 moduleName = PyObject_GetAttrString(exception, "__module__");
1237 if (moduleName == NULL)
1238 err = PyFile_WriteString("<unknown>", f);
1239 else {
1240 char* modstr = PyString_AsString(moduleName);
1241 if (modstr && strcmp(modstr, "exceptions"))
1243 err = PyFile_WriteString(modstr, f);
1244 err += PyFile_WriteString(".", f);
1246 Py_DECREF(moduleName);
1248 if (err == 0) {
1249 if (className == NULL)
1250 err = PyFile_WriteString("<unknown>", f);
1251 else
1252 err = PyFile_WriteString(className, f);
1255 else
1256 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1257 if (err == 0 && (value != Py_None)) {
1258 PyObject *s = PyObject_Str(value);
1259 /* only print colon if the str() of the
1260 object is not the empty string
1262 if (s == NULL)
1263 err = -1;
1264 else if (!PyString_Check(s) ||
1265 PyString_GET_SIZE(s) != 0)
1266 err = PyFile_WriteString(": ", f);
1267 if (err == 0)
1268 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1269 Py_XDECREF(s);
1271 /* try to write a newline in any case */
1272 err += PyFile_WriteString("\n", f);
1274 Py_DECREF(value);
1275 /* If an error happened here, don't show it.
1276 XXX This is wrong, but too many callers rely on this behavior. */
1277 if (err != 0)
1278 PyErr_Clear();
1281 PyObject *
1282 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1283 PyObject *locals, PyCompilerFlags *flags)
1285 PyObject *ret = NULL;
1286 mod_ty mod;
1287 PyArena *arena = PyArena_New();
1288 if (arena == NULL)
1289 return NULL;
1291 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1292 if (mod != NULL)
1293 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1294 PyArena_Free(arena);
1295 return ret;
1298 PyObject *
1299 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1300 PyObject *locals, int closeit, PyCompilerFlags *flags)
1302 PyObject *ret;
1303 mod_ty mod;
1304 PyArena *arena = PyArena_New();
1305 if (arena == NULL)
1306 return NULL;
1308 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1309 flags, NULL, arena);
1310 if (closeit)
1311 fclose(fp);
1312 if (mod == NULL) {
1313 PyArena_Free(arena);
1314 return NULL;
1316 ret = run_mod(mod, filename, globals, locals, flags, arena);
1317 PyArena_Free(arena);
1318 return ret;
1321 static PyObject *
1322 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1323 PyCompilerFlags *flags, PyArena *arena)
1325 PyCodeObject *co;
1326 PyObject *v;
1327 co = PyAST_Compile(mod, filename, flags, arena);
1328 if (co == NULL)
1329 return NULL;
1330 v = PyEval_EvalCode(co, globals, locals);
1331 Py_DECREF(co);
1332 return v;
1335 static PyObject *
1336 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1337 PyObject *locals, PyCompilerFlags *flags)
1339 PyCodeObject *co;
1340 PyObject *v;
1341 long magic;
1342 long PyImport_GetMagicNumber(void);
1344 magic = PyMarshal_ReadLongFromFile(fp);
1345 if (magic != PyImport_GetMagicNumber()) {
1346 PyErr_SetString(PyExc_RuntimeError,
1347 "Bad magic number in .pyc file");
1348 return NULL;
1350 (void) PyMarshal_ReadLongFromFile(fp);
1351 v = PyMarshal_ReadLastObjectFromFile(fp);
1352 fclose(fp);
1353 if (v == NULL || !PyCode_Check(v)) {
1354 Py_XDECREF(v);
1355 PyErr_SetString(PyExc_RuntimeError,
1356 "Bad code object in .pyc file");
1357 return NULL;
1359 co = (PyCodeObject *)v;
1360 v = PyEval_EvalCode(co, globals, locals);
1361 if (v && flags)
1362 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1363 Py_DECREF(co);
1364 return v;
1367 PyObject *
1368 Py_CompileStringFlags(const char *str, const char *filename, int start,
1369 PyCompilerFlags *flags)
1371 PyCodeObject *co;
1372 mod_ty mod;
1373 PyArena *arena = PyArena_New();
1374 if (arena == NULL)
1375 return NULL;
1377 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1378 if (mod == NULL) {
1379 PyArena_Free(arena);
1380 return NULL;
1382 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1383 PyObject *result = PyAST_mod2obj(mod);
1384 PyArena_Free(arena);
1385 return result;
1387 co = PyAST_Compile(mod, filename, flags, arena);
1388 PyArena_Free(arena);
1389 return (PyObject *)co;
1392 struct symtable *
1393 Py_SymtableString(const char *str, const char *filename, int start)
1395 struct symtable *st;
1396 mod_ty mod;
1397 PyCompilerFlags flags;
1398 PyArena *arena = PyArena_New();
1399 if (arena == NULL)
1400 return NULL;
1402 flags.cf_flags = 0;
1404 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1405 if (mod == NULL) {
1406 PyArena_Free(arena);
1407 return NULL;
1409 st = PySymtable_Build(mod, filename, 0);
1410 PyArena_Free(arena);
1411 return st;
1414 /* Preferred access to parser is through AST. */
1415 mod_ty
1416 PyParser_ASTFromString(const char *s, const char *filename, int start,
1417 PyCompilerFlags *flags, PyArena *arena)
1419 mod_ty mod;
1420 perrdetail err;
1421 int iflags = PARSER_FLAGS(flags);
1423 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1424 &_PyParser_Grammar, start, &err,
1425 &iflags);
1426 if (n) {
1427 if (flags) {
1428 flags->cf_flags |= iflags & PyCF_MASK;
1430 mod = PyAST_FromNode(n, flags, filename, arena);
1431 PyNode_Free(n);
1432 return mod;
1434 else {
1435 err_input(&err);
1436 return NULL;
1440 mod_ty
1441 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1442 char *ps2, PyCompilerFlags *flags, int *errcode,
1443 PyArena *arena)
1445 mod_ty mod;
1446 perrdetail err;
1447 int iflags = PARSER_FLAGS(flags);
1449 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1450 start, ps1, ps2, &err, &iflags);
1451 if (n) {
1452 if (flags) {
1453 flags->cf_flags |= iflags & PyCF_MASK;
1455 mod = PyAST_FromNode(n, flags, filename, arena);
1456 PyNode_Free(n);
1457 return mod;
1459 else {
1460 err_input(&err);
1461 if (errcode)
1462 *errcode = err.error;
1463 return NULL;
1467 /* Simplified interface to parsefile -- return node or set exception */
1469 node *
1470 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1472 perrdetail err;
1473 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1474 start, NULL, NULL, &err, flags);
1475 if (n == NULL)
1476 err_input(&err);
1478 return n;
1481 /* Simplified interface to parsestring -- return node or set exception */
1483 node *
1484 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1486 perrdetail err;
1487 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1488 start, &err, flags);
1489 if (n == NULL)
1490 err_input(&err);
1491 return n;
1494 node *
1495 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1496 int start, int flags)
1498 perrdetail err;
1499 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1500 &_PyParser_Grammar, start, &err, flags);
1501 if (n == NULL)
1502 err_input(&err);
1503 return n;
1506 node *
1507 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1509 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1512 /* May want to move a more generalized form of this to parsetok.c or
1513 even parser modules. */
1515 void
1516 PyParser_SetError(perrdetail *err)
1518 err_input(err);
1521 /* Set the error appropriate to the given input error code (see errcode.h) */
1523 static void
1524 err_input(perrdetail *err)
1526 PyObject *v, *w, *errtype;
1527 PyObject* u = NULL;
1528 char *msg = NULL;
1529 errtype = PyExc_SyntaxError;
1530 switch (err->error) {
1531 case E_SYNTAX:
1532 errtype = PyExc_IndentationError;
1533 if (err->expected == INDENT)
1534 msg = "expected an indented block";
1535 else if (err->token == INDENT)
1536 msg = "unexpected indent";
1537 else if (err->token == DEDENT)
1538 msg = "unexpected unindent";
1539 else {
1540 errtype = PyExc_SyntaxError;
1541 msg = "invalid syntax";
1543 break;
1544 case E_TOKEN:
1545 msg = "invalid token";
1546 break;
1547 case E_EOFS:
1548 msg = "EOF while scanning triple-quoted string literal";
1549 break;
1550 case E_EOLS:
1551 msg = "EOL while scanning string literal";
1552 break;
1553 case E_INTR:
1554 if (!PyErr_Occurred())
1555 PyErr_SetNone(PyExc_KeyboardInterrupt);
1556 return;
1557 case E_NOMEM:
1558 PyErr_NoMemory();
1559 return;
1560 case E_EOF:
1561 msg = "unexpected EOF while parsing";
1562 break;
1563 case E_TABSPACE:
1564 errtype = PyExc_TabError;
1565 msg = "inconsistent use of tabs and spaces in indentation";
1566 break;
1567 case E_OVERFLOW:
1568 msg = "expression too long";
1569 break;
1570 case E_DEDENT:
1571 errtype = PyExc_IndentationError;
1572 msg = "unindent does not match any outer indentation level";
1573 break;
1574 case E_TOODEEP:
1575 errtype = PyExc_IndentationError;
1576 msg = "too many levels of indentation";
1577 break;
1578 case E_DECODE: {
1579 PyObject *type, *value, *tb;
1580 PyErr_Fetch(&type, &value, &tb);
1581 if (value != NULL) {
1582 u = PyObject_Str(value);
1583 if (u != NULL) {
1584 msg = PyString_AsString(u);
1587 if (msg == NULL)
1588 msg = "unknown decode error";
1589 Py_XDECREF(type);
1590 Py_XDECREF(value);
1591 Py_XDECREF(tb);
1592 break;
1594 case E_LINECONT:
1595 msg = "unexpected character after line continuation character";
1596 break;
1597 default:
1598 fprintf(stderr, "error=%d\n", err->error);
1599 msg = "unknown parsing error";
1600 break;
1602 v = Py_BuildValue("(ziiz)", err->filename,
1603 err->lineno, err->offset, err->text);
1604 if (err->text != NULL) {
1605 PyObject_FREE(err->text);
1606 err->text = NULL;
1608 w = NULL;
1609 if (v != NULL)
1610 w = Py_BuildValue("(sO)", msg, v);
1611 Py_XDECREF(u);
1612 Py_XDECREF(v);
1613 PyErr_SetObject(errtype, w);
1614 Py_XDECREF(w);
1617 /* Print fatal error message and abort */
1619 void
1620 Py_FatalError(const char *msg)
1622 fprintf(stderr, "Fatal Python error: %s\n", msg);
1623 #ifdef MS_WINDOWS
1624 OutputDebugString("Fatal Python error: ");
1625 OutputDebugString(msg);
1626 OutputDebugString("\n");
1627 #ifdef _DEBUG
1628 DebugBreak();
1629 #endif
1630 #endif /* MS_WINDOWS */
1631 abort();
1634 /* Clean up and exit */
1636 #ifdef WITH_THREAD
1637 #include "pythread.h"
1638 #endif
1640 #define NEXITFUNCS 32
1641 static void (*exitfuncs[NEXITFUNCS])(void);
1642 static int nexitfuncs = 0;
1644 int Py_AtExit(void (*func)(void))
1646 if (nexitfuncs >= NEXITFUNCS)
1647 return -1;
1648 exitfuncs[nexitfuncs++] = func;
1649 return 0;
1652 static void
1653 call_sys_exitfunc(void)
1655 PyObject *exitfunc = PySys_GetObject("exitfunc");
1657 if (exitfunc) {
1658 PyObject *res;
1659 Py_INCREF(exitfunc);
1660 PySys_SetObject("exitfunc", (PyObject *)NULL);
1661 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1662 if (res == NULL) {
1663 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1664 PySys_WriteStderr("Error in sys.exitfunc:\n");
1666 PyErr_Print();
1668 Py_DECREF(exitfunc);
1671 if (Py_FlushLine())
1672 PyErr_Clear();
1675 static void
1676 call_ll_exitfuncs(void)
1678 while (nexitfuncs > 0)
1679 (*exitfuncs[--nexitfuncs])();
1681 fflush(stdout);
1682 fflush(stderr);
1685 void
1686 Py_Exit(int sts)
1688 Py_Finalize();
1690 exit(sts);
1693 static void
1694 initsigs(void)
1696 #ifdef SIGPIPE
1697 PyOS_setsig(SIGPIPE, SIG_IGN);
1698 #endif
1699 #ifdef SIGXFZ
1700 PyOS_setsig(SIGXFZ, SIG_IGN);
1701 #endif
1702 #ifdef SIGXFSZ
1703 PyOS_setsig(SIGXFSZ, SIG_IGN);
1704 #endif
1705 PyOS_InitInterrupts(); /* May imply initsignal() */
1710 * The file descriptor fd is considered ``interactive'' if either
1711 * a) isatty(fd) is TRUE, or
1712 * b) the -i flag was given, and the filename associated with
1713 * the descriptor is NULL or "<stdin>" or "???".
1716 Py_FdIsInteractive(FILE *fp, const char *filename)
1718 if (isatty((int)fileno(fp)))
1719 return 1;
1720 if (!Py_InteractiveFlag)
1721 return 0;
1722 return (filename == NULL) ||
1723 (strcmp(filename, "<stdin>") == 0) ||
1724 (strcmp(filename, "???") == 0);
1728 #if defined(USE_STACKCHECK)
1729 #if defined(WIN32) && defined(_MSC_VER)
1731 /* Stack checking for Microsoft C */
1733 #include <malloc.h>
1734 #include <excpt.h>
1737 * Return non-zero when we run out of memory on the stack; zero otherwise.
1740 PyOS_CheckStack(void)
1742 __try {
1743 /* alloca throws a stack overflow exception if there's
1744 not enough space left on the stack */
1745 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1746 return 0;
1747 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1748 EXCEPTION_EXECUTE_HANDLER :
1749 EXCEPTION_CONTINUE_SEARCH) {
1750 int errcode = _resetstkoflw();
1751 if (errcode)
1753 Py_FatalError("Could not reset the stack!");
1756 return 1;
1759 #endif /* WIN32 && _MSC_VER */
1761 /* Alternate implementations can be added here... */
1763 #endif /* USE_STACKCHECK */
1766 /* Wrappers around sigaction() or signal(). */
1768 PyOS_sighandler_t
1769 PyOS_getsig(int sig)
1771 #ifdef HAVE_SIGACTION
1772 struct sigaction context;
1773 if (sigaction(sig, NULL, &context) == -1)
1774 return SIG_ERR;
1775 return context.sa_handler;
1776 #else
1777 PyOS_sighandler_t handler;
1778 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1779 #if defined(_MSC_VER) && _MSC_VER >= 1400
1780 switch (sig) {
1781 /* Only these signals are valid */
1782 case SIGINT:
1783 case SIGILL:
1784 case SIGFPE:
1785 case SIGSEGV:
1786 case SIGTERM:
1787 case SIGBREAK:
1788 case SIGABRT:
1789 break;
1790 /* Don't call signal() with other values or it will assert */
1791 default:
1792 return SIG_ERR;
1794 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1795 handler = signal(sig, SIG_IGN);
1796 if (handler != SIG_ERR)
1797 signal(sig, handler);
1798 return handler;
1799 #endif
1802 PyOS_sighandler_t
1803 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1805 #ifdef HAVE_SIGACTION
1806 struct sigaction context, ocontext;
1807 context.sa_handler = handler;
1808 sigemptyset(&context.sa_mask);
1809 context.sa_flags = 0;
1810 if (sigaction(sig, &context, &ocontext) == -1)
1811 return SIG_ERR;
1812 return ocontext.sa_handler;
1813 #else
1814 PyOS_sighandler_t oldhandler;
1815 oldhandler = signal(sig, handler);
1816 #ifdef HAVE_SIGINTERRUPT
1817 siginterrupt(sig, 1);
1818 #endif
1819 return oldhandler;
1820 #endif
1823 /* Deprecated C API functions still provided for binary compatiblity */
1825 #undef PyParser_SimpleParseFile
1826 PyAPI_FUNC(node *)
1827 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1829 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1832 #undef PyParser_SimpleParseString
1833 PyAPI_FUNC(node *)
1834 PyParser_SimpleParseString(const char *str, int start)
1836 return PyParser_SimpleParseStringFlags(str, start, 0);
1839 #undef PyRun_AnyFile
1840 PyAPI_FUNC(int)
1841 PyRun_AnyFile(FILE *fp, const char *name)
1843 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1846 #undef PyRun_AnyFileEx
1847 PyAPI_FUNC(int)
1848 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1850 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1853 #undef PyRun_AnyFileFlags
1854 PyAPI_FUNC(int)
1855 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1857 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1860 #undef PyRun_File
1861 PyAPI_FUNC(PyObject *)
1862 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1864 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1867 #undef PyRun_FileEx
1868 PyAPI_FUNC(PyObject *)
1869 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1871 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1874 #undef PyRun_FileFlags
1875 PyAPI_FUNC(PyObject *)
1876 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1877 PyCompilerFlags *flags)
1879 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1882 #undef PyRun_SimpleFile
1883 PyAPI_FUNC(int)
1884 PyRun_SimpleFile(FILE *f, const char *p)
1886 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1889 #undef PyRun_SimpleFileEx
1890 PyAPI_FUNC(int)
1891 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1893 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1897 #undef PyRun_String
1898 PyAPI_FUNC(PyObject *)
1899 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1901 return PyRun_StringFlags(str, s, g, l, NULL);
1904 #undef PyRun_SimpleString
1905 PyAPI_FUNC(int)
1906 PyRun_SimpleString(const char *s)
1908 return PyRun_SimpleStringFlags(s, NULL);
1911 #undef Py_CompileString
1912 PyAPI_FUNC(PyObject *)
1913 Py_CompileString(const char *str, const char *p, int s)
1915 return Py_CompileStringFlags(str, p, s, NULL);
1918 #undef PyRun_InteractiveOne
1919 PyAPI_FUNC(int)
1920 PyRun_InteractiveOne(FILE *f, const char *p)
1922 return PyRun_InteractiveOneFlags(f, p, NULL);
1925 #undef PyRun_InteractiveLoop
1926 PyAPI_FUNC(int)
1927 PyRun_InteractiveLoop(FILE *f, const char *p)
1929 return PyRun_InteractiveLoopFlags(f, p, NULL);
1932 #ifdef __cplusplus
1934 #endif