Remove incorrect usage of :const: in documentation.
[python.git] / Python / pythonrun.c
blobf5465c5edeea64076e2bd1a727666c48d38e7323
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_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
76 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
77 int Py_FrozenFlag; /* Needed by getpath.c */
78 int Py_UnicodeFlag = 0; /* Needed by compile.c */
79 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
80 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
81 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
82 true divisions (which they will be in 2.3). */
83 int _Py_QnewFlag = 0;
85 /* Reference to 'warnings' module, to avoid importing it
86 on the fly when the import lock may be held. See 683658/771097
88 static PyObject *warnings_module = NULL;
90 /* Returns a borrowed reference to the 'warnings' module, or NULL.
91 If the module is returned, it is guaranteed to have been obtained
92 without acquiring the import lock
94 PyObject *PyModule_GetWarningsModule(void)
96 PyObject *typ, *val, *tb;
97 PyObject *all_modules;
98 /* If we managed to get the module at init time, just use it */
99 if (warnings_module)
100 return warnings_module;
101 /* If it wasn't available at init time, it may be available
102 now in sys.modules (common scenario is frozen apps: import
103 at init time fails, but the frozen init code sets up sys.path
104 correctly, then does an implicit import of warnings for us
106 /* Save and restore any exceptions */
107 PyErr_Fetch(&typ, &val, &tb);
109 all_modules = PySys_GetObject("modules");
110 if (all_modules) {
111 warnings_module = PyDict_GetItemString(all_modules, "warnings");
112 /* We keep a ref in the global */
113 Py_XINCREF(warnings_module);
115 PyErr_Restore(typ, val, tb);
116 return warnings_module;
119 static int initialized = 0;
121 /* API to access the initialized flag -- useful for esoteric use */
124 Py_IsInitialized(void)
126 return initialized;
129 /* Global initializations. Can be undone by Py_Finalize(). Don't
130 call this twice without an intervening Py_Finalize() call. When
131 initializations fail, a fatal error is issued and the function does
132 not return. On return, the first thread and interpreter state have
133 been created.
135 Locking: you must hold the interpreter lock while calling this.
136 (If the lock has not yet been initialized, that's equivalent to
137 having the lock, but you cannot use multiple threads.)
141 static int
142 add_flag(int flag, const char *envs)
144 int env = atoi(envs);
145 if (flag < env)
146 flag = env;
147 if (flag < 1)
148 flag = 1;
149 return flag;
152 void
153 Py_InitializeEx(int install_sigs)
155 PyInterpreterState *interp;
156 PyThreadState *tstate;
157 PyObject *bimod, *sysmod;
158 char *p;
159 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
160 char *codeset;
161 char *saved_locale;
162 PyObject *sys_stream, *sys_isatty;
163 #endif
164 extern void _Py_ReadyTypes(void);
166 if (initialized)
167 return;
168 initialized = 1;
170 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
171 Py_DebugFlag = add_flag(Py_DebugFlag, p);
172 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
173 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
174 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
175 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
176 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
177 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
179 interp = PyInterpreterState_New();
180 if (interp == NULL)
181 Py_FatalError("Py_Initialize: can't make first interpreter");
183 tstate = PyThreadState_New(interp);
184 if (tstate == NULL)
185 Py_FatalError("Py_Initialize: can't make first thread");
186 (void) PyThreadState_Swap(tstate);
188 _Py_ReadyTypes();
190 if (!_PyFrame_Init())
191 Py_FatalError("Py_Initialize: can't init frames");
193 if (!_PyInt_Init())
194 Py_FatalError("Py_Initialize: can't init ints");
196 _PyFloat_Init();
198 interp->modules = PyDict_New();
199 if (interp->modules == NULL)
200 Py_FatalError("Py_Initialize: can't make modules dictionary");
201 interp->modules_reloading = PyDict_New();
202 if (interp->modules_reloading == NULL)
203 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
205 #ifdef Py_USING_UNICODE
206 /* Init Unicode implementation; relies on the codec registry */
207 _PyUnicode_Init();
208 #endif
210 bimod = _PyBuiltin_Init();
211 if (bimod == NULL)
212 Py_FatalError("Py_Initialize: can't initialize __builtin__");
213 interp->builtins = PyModule_GetDict(bimod);
214 if (interp->builtins == NULL)
215 Py_FatalError("Py_Initialize: can't initialize builtins dict");
216 Py_INCREF(interp->builtins);
218 sysmod = _PySys_Init();
219 if (sysmod == NULL)
220 Py_FatalError("Py_Initialize: can't initialize sys");
221 interp->sysdict = PyModule_GetDict(sysmod);
222 if (interp->sysdict == NULL)
223 Py_FatalError("Py_Initialize: can't initialize sys dict");
224 Py_INCREF(interp->sysdict);
225 _PyImport_FixupExtension("sys", "sys");
226 PySys_SetPath(Py_GetPath());
227 PyDict_SetItemString(interp->sysdict, "modules",
228 interp->modules);
230 _PyImport_Init();
232 /* initialize builtin exceptions */
233 _PyExc_Init();
234 _PyImport_FixupExtension("exceptions", "exceptions");
236 /* phase 2 of builtins */
237 _PyImport_FixupExtension("__builtin__", "__builtin__");
239 _PyImportHooks_Init();
241 if (install_sigs)
242 initsigs(); /* Signal handling stuff, including initintr() */
244 initmain(); /* Module __main__ */
245 if (!Py_NoSiteFlag)
246 initsite(); /* Module site */
248 /* auto-thread-state API, if available */
249 #ifdef WITH_THREAD
250 _PyGILState_Init(interp, tstate);
251 #endif /* WITH_THREAD */
253 warnings_module = PyImport_ImportModule("warnings");
254 if (!warnings_module)
255 PyErr_Clear();
257 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
258 /* On Unix, set the file system encoding according to the
259 user's preference, if the CODESET names a well-known
260 Python codec, and Py_FileSystemDefaultEncoding isn't
261 initialized by other means. Also set the encoding of
262 stdin and stdout if these are terminals. */
264 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
265 setlocale(LC_CTYPE, "");
266 codeset = nl_langinfo(CODESET);
267 if (codeset && *codeset) {
268 PyObject *enc = PyCodec_Encoder(codeset);
269 if (enc) {
270 codeset = strdup(codeset);
271 Py_DECREF(enc);
272 } else {
273 codeset = NULL;
274 PyErr_Clear();
276 } else
277 codeset = NULL;
278 setlocale(LC_CTYPE, saved_locale);
279 free(saved_locale);
281 if (codeset) {
282 sys_stream = PySys_GetObject("stdin");
283 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
284 if (!sys_isatty)
285 PyErr_Clear();
286 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
287 PyFile_Check(sys_stream)) {
288 if (!PyFile_SetEncoding(sys_stream, codeset))
289 Py_FatalError("Cannot set codeset of stdin");
291 Py_XDECREF(sys_isatty);
293 sys_stream = PySys_GetObject("stdout");
294 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
295 if (!sys_isatty)
296 PyErr_Clear();
297 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
298 PyFile_Check(sys_stream)) {
299 if (!PyFile_SetEncoding(sys_stream, codeset))
300 Py_FatalError("Cannot set codeset of stdout");
302 Py_XDECREF(sys_isatty);
304 sys_stream = PySys_GetObject("stderr");
305 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
306 if (!sys_isatty)
307 PyErr_Clear();
308 if(sys_isatty && PyObject_IsTrue(sys_isatty) &&
309 PyFile_Check(sys_stream)) {
310 if (!PyFile_SetEncoding(sys_stream, codeset))
311 Py_FatalError("Cannot set codeset of stderr");
313 Py_XDECREF(sys_isatty);
315 if (!Py_FileSystemDefaultEncoding)
316 Py_FileSystemDefaultEncoding = codeset;
317 else
318 free(codeset);
320 #endif
323 void
324 Py_Initialize(void)
326 Py_InitializeEx(1);
330 #ifdef COUNT_ALLOCS
331 extern void dump_counts(FILE*);
332 #endif
334 /* Undo the effect of Py_Initialize().
336 Beware: if multiple interpreter and/or thread states exist, these
337 are not wiped out; only the current thread and interpreter state
338 are deleted. But since everything else is deleted, those other
339 interpreter and thread states should no longer be used.
341 (XXX We should do better, e.g. wipe out all interpreters and
342 threads.)
344 Locking: as above.
348 void
349 Py_Finalize(void)
351 PyInterpreterState *interp;
352 PyThreadState *tstate;
354 if (!initialized)
355 return;
357 /* The interpreter is still entirely intact at this point, and the
358 * exit funcs may be relying on that. In particular, if some thread
359 * or exit func is still waiting to do an import, the import machinery
360 * expects Py_IsInitialized() to return true. So don't say the
361 * interpreter is uninitialized until after the exit funcs have run.
362 * Note that Threading.py uses an exit func to do a join on all the
363 * threads created thru it, so this also protects pending imports in
364 * the threads created via Threading.
366 call_sys_exitfunc();
367 initialized = 0;
369 /* Get current thread state and interpreter pointer */
370 tstate = PyThreadState_GET();
371 interp = tstate->interp;
373 /* Disable signal handling */
374 PyOS_FiniInterrupts();
376 /* drop module references we saved */
377 Py_XDECREF(warnings_module);
378 warnings_module = NULL;
380 /* Clear type lookup cache */
381 PyType_ClearCache();
383 /* Collect garbage. This may call finalizers; it's nice to call these
384 * before all modules are destroyed.
385 * XXX If a __del__ or weakref callback is triggered here, and tries to
386 * XXX import a module, bad things can happen, because Python no
387 * XXX longer believes it's initialized.
388 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
389 * XXX is easy to provoke that way. I've also seen, e.g.,
390 * XXX Exception exceptions.ImportError: 'No module named sha'
391 * XXX in <function callback at 0x008F5718> ignored
392 * XXX but I'm unclear on exactly how that one happens. In any case,
393 * XXX I haven't seen a real-life report of either of these.
395 PyGC_Collect();
396 #ifdef COUNT_ALLOCS
397 /* With COUNT_ALLOCS, it helps to run GC multiple times:
398 each collection might release some types from the type
399 list, so they become garbage. */
400 while (PyGC_Collect() > 0)
401 /* nothing */;
402 #endif
404 /* Destroy all modules */
405 PyImport_Cleanup();
407 /* Collect final garbage. This disposes of cycles created by
408 * new-style class definitions, for example.
409 * XXX This is disabled because it caused too many problems. If
410 * XXX a __del__ or weakref callback triggers here, Python code has
411 * XXX a hard time running, because even the sys module has been
412 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
413 * XXX One symptom is a sequence of information-free messages
414 * XXX coming from threads (if a __del__ or callback is invoked,
415 * XXX other threads can execute too, and any exception they encounter
416 * XXX triggers a comedy of errors as subsystem after subsystem
417 * XXX fails to find what it *expects* to find in sys to help report
418 * XXX the exception and consequent unexpected failures). I've also
419 * XXX seen segfaults then, after adding print statements to the
420 * XXX Python code getting called.
422 #if 0
423 PyGC_Collect();
424 #endif
426 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
427 _PyImport_Fini();
429 /* Debugging stuff */
430 #ifdef COUNT_ALLOCS
431 dump_counts(stdout);
432 #endif
434 PRINT_TOTAL_REFS();
436 #ifdef Py_TRACE_REFS
437 /* Display all objects still alive -- this can invoke arbitrary
438 * __repr__ overrides, so requires a mostly-intact interpreter.
439 * Alas, a lot of stuff may still be alive now that will be cleaned
440 * up later.
442 if (Py_GETENV("PYTHONDUMPREFS"))
443 _Py_PrintReferences(stderr);
444 #endif /* Py_TRACE_REFS */
446 /* Clear interpreter state */
447 PyInterpreterState_Clear(interp);
449 /* Now we decref the exception classes. After this point nothing
450 can raise an exception. That's okay, because each Fini() method
451 below has been checked to make sure no exceptions are ever
452 raised.
455 _PyExc_Fini();
457 /* Cleanup auto-thread-state */
458 #ifdef WITH_THREAD
459 _PyGILState_Fini();
460 #endif /* WITH_THREAD */
462 /* Delete current thread */
463 PyThreadState_Swap(NULL);
464 PyInterpreterState_Delete(interp);
466 /* Sundry finalizers */
467 PyMethod_Fini();
468 PyFrame_Fini();
469 PyCFunction_Fini();
470 PyTuple_Fini();
471 PyList_Fini();
472 PySet_Fini();
473 PyString_Fini();
474 PyInt_Fini();
475 PyFloat_Fini();
477 #ifdef Py_USING_UNICODE
478 /* Cleanup Unicode implementation */
479 _PyUnicode_Fini();
480 #endif
482 /* XXX Still allocated:
483 - various static ad-hoc pointers to interned strings
484 - int and float free list blocks
485 - whatever various modules and libraries allocate
488 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
490 #ifdef Py_TRACE_REFS
491 /* Display addresses (& refcnts) of all objects still alive.
492 * An address can be used to find the repr of the object, printed
493 * above by _Py_PrintReferences.
495 if (Py_GETENV("PYTHONDUMPREFS"))
496 _Py_PrintReferenceAddresses(stderr);
497 #endif /* Py_TRACE_REFS */
498 #ifdef PYMALLOC_DEBUG
499 if (Py_GETENV("PYTHONMALLOCSTATS"))
500 _PyObject_DebugMallocStats();
501 #endif
503 call_ll_exitfuncs();
506 /* Create and initialize a new interpreter and thread, and return the
507 new thread. This requires that Py_Initialize() has been called
508 first.
510 Unsuccessful initialization yields a NULL pointer. Note that *no*
511 exception information is available even in this case -- the
512 exception information is held in the thread, and there is no
513 thread.
515 Locking: as above.
519 PyThreadState *
520 Py_NewInterpreter(void)
522 PyInterpreterState *interp;
523 PyThreadState *tstate, *save_tstate;
524 PyObject *bimod, *sysmod;
526 if (!initialized)
527 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
529 interp = PyInterpreterState_New();
530 if (interp == NULL)
531 return NULL;
533 tstate = PyThreadState_New(interp);
534 if (tstate == NULL) {
535 PyInterpreterState_Delete(interp);
536 return NULL;
539 save_tstate = PyThreadState_Swap(tstate);
541 /* XXX The following is lax in error checking */
543 interp->modules = PyDict_New();
544 interp->modules_reloading = PyDict_New();
546 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
547 if (bimod != NULL) {
548 interp->builtins = PyModule_GetDict(bimod);
549 if (interp->builtins == NULL)
550 goto handle_error;
551 Py_INCREF(interp->builtins);
553 sysmod = _PyImport_FindExtension("sys", "sys");
554 if (bimod != NULL && sysmod != NULL) {
555 interp->sysdict = PyModule_GetDict(sysmod);
556 if (interp->sysdict == NULL)
557 goto handle_error;
558 Py_INCREF(interp->sysdict);
559 PySys_SetPath(Py_GetPath());
560 PyDict_SetItemString(interp->sysdict, "modules",
561 interp->modules);
562 _PyImportHooks_Init();
563 initmain();
564 if (!Py_NoSiteFlag)
565 initsite();
568 if (!PyErr_Occurred())
569 return tstate;
571 handle_error:
572 /* Oops, it didn't work. Undo it all. */
574 PyErr_Print();
575 PyThreadState_Clear(tstate);
576 PyThreadState_Swap(save_tstate);
577 PyThreadState_Delete(tstate);
578 PyInterpreterState_Delete(interp);
580 return NULL;
583 /* Delete an interpreter and its last thread. This requires that the
584 given thread state is current, that the thread has no remaining
585 frames, and that it is its interpreter's only remaining thread.
586 It is a fatal error to violate these constraints.
588 (Py_Finalize() doesn't have these constraints -- it zaps
589 everything, regardless.)
591 Locking: as above.
595 void
596 Py_EndInterpreter(PyThreadState *tstate)
598 PyInterpreterState *interp = tstate->interp;
600 if (tstate != PyThreadState_GET())
601 Py_FatalError("Py_EndInterpreter: thread is not current");
602 if (tstate->frame != NULL)
603 Py_FatalError("Py_EndInterpreter: thread still has a frame");
604 if (tstate != interp->tstate_head || tstate->next != NULL)
605 Py_FatalError("Py_EndInterpreter: not the last thread");
607 PyImport_Cleanup();
608 PyInterpreterState_Clear(interp);
609 PyThreadState_Swap(NULL);
610 PyInterpreterState_Delete(interp);
613 static char *progname = "python";
615 void
616 Py_SetProgramName(char *pn)
618 if (pn && *pn)
619 progname = pn;
622 char *
623 Py_GetProgramName(void)
625 return progname;
628 static char *default_home = NULL;
630 void
631 Py_SetPythonHome(char *home)
633 default_home = home;
636 char *
637 Py_GetPythonHome(void)
639 char *home = default_home;
640 if (home == NULL && !Py_IgnoreEnvironmentFlag)
641 home = Py_GETENV("PYTHONHOME");
642 return home;
645 /* Create __main__ module */
647 static void
648 initmain(void)
650 PyObject *m, *d;
651 m = PyImport_AddModule("__main__");
652 if (m == NULL)
653 Py_FatalError("can't create __main__ module");
654 d = PyModule_GetDict(m);
655 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
656 PyObject *bimod = PyImport_ImportModule("__builtin__");
657 if (bimod == NULL ||
658 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
659 Py_FatalError("can't add __builtins__ to __main__");
660 Py_DECREF(bimod);
664 /* Import the site module (not into __main__ though) */
666 static void
667 initsite(void)
669 PyObject *m, *f;
670 m = PyImport_ImportModule("site");
671 if (m == NULL) {
672 f = PySys_GetObject("stderr");
673 if (Py_VerboseFlag) {
674 PyFile_WriteString(
675 "'import site' failed; traceback:\n", f);
676 PyErr_Print();
678 else {
679 PyFile_WriteString(
680 "'import site' failed; use -v for traceback\n", f);
681 PyErr_Clear();
684 else {
685 Py_DECREF(m);
689 /* Parse input from a file and execute it */
692 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
693 PyCompilerFlags *flags)
695 if (filename == NULL)
696 filename = "???";
697 if (Py_FdIsInteractive(fp, filename)) {
698 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
699 if (closeit)
700 fclose(fp);
701 return err;
703 else
704 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
708 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
710 PyObject *v;
711 int ret;
712 PyCompilerFlags local_flags;
714 if (flags == NULL) {
715 flags = &local_flags;
716 local_flags.cf_flags = 0;
718 v = PySys_GetObject("ps1");
719 if (v == NULL) {
720 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
721 Py_XDECREF(v);
723 v = PySys_GetObject("ps2");
724 if (v == NULL) {
725 PySys_SetObject("ps2", v = PyString_FromString("... "));
726 Py_XDECREF(v);
728 for (;;) {
729 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
730 PRINT_TOTAL_REFS();
731 if (ret == E_EOF)
732 return 0;
734 if (ret == E_NOMEM)
735 return -1;
740 /* compute parser flags based on compiler flags */
741 #define PARSER_FLAGS(flags) \
742 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
743 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
745 #if 0
746 /* Keep an example of flags with future keyword support. */
747 #define PARSER_FLAGS(flags) \
748 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
749 PyPARSE_DONT_IMPLY_DEDENT : 0) \
750 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
751 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
752 #endif
755 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
757 PyObject *m, *d, *v, *w;
758 mod_ty mod;
759 PyArena *arena;
760 char *ps1 = "", *ps2 = "";
761 int errcode = 0;
763 v = PySys_GetObject("ps1");
764 if (v != NULL) {
765 v = PyObject_Str(v);
766 if (v == NULL)
767 PyErr_Clear();
768 else if (PyString_Check(v))
769 ps1 = PyString_AsString(v);
771 w = PySys_GetObject("ps2");
772 if (w != NULL) {
773 w = PyObject_Str(w);
774 if (w == NULL)
775 PyErr_Clear();
776 else if (PyString_Check(w))
777 ps2 = PyString_AsString(w);
779 arena = PyArena_New();
780 if (arena == NULL) {
781 Py_XDECREF(v);
782 Py_XDECREF(w);
783 return -1;
785 mod = PyParser_ASTFromFile(fp, filename,
786 Py_single_input, ps1, ps2,
787 flags, &errcode, arena);
788 Py_XDECREF(v);
789 Py_XDECREF(w);
790 if (mod == NULL) {
791 PyArena_Free(arena);
792 if (errcode == E_EOF) {
793 PyErr_Clear();
794 return E_EOF;
796 PyErr_Print();
797 return -1;
799 m = PyImport_AddModule("__main__");
800 if (m == NULL) {
801 PyArena_Free(arena);
802 return -1;
804 d = PyModule_GetDict(m);
805 v = run_mod(mod, filename, d, d, flags, arena);
806 PyArena_Free(arena);
807 if (v == NULL) {
808 PyErr_Print();
809 return -1;
811 Py_DECREF(v);
812 if (Py_FlushLine())
813 PyErr_Clear();
814 return 0;
817 /* Check whether a file maybe a pyc file: Look at the extension,
818 the file type, and, if we may close it, at the first few bytes. */
820 static int
821 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
823 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
824 return 1;
826 /* Only look into the file if we are allowed to close it, since
827 it then should also be seekable. */
828 if (closeit) {
829 /* Read only two bytes of the magic. If the file was opened in
830 text mode, the bytes 3 and 4 of the magic (\r\n) might not
831 be read as they are on disk. */
832 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
833 unsigned char buf[2];
834 /* Mess: In case of -x, the stream is NOT at its start now,
835 and ungetc() was used to push back the first newline,
836 which makes the current stream position formally undefined,
837 and a x-platform nightmare.
838 Unfortunately, we have no direct way to know whether -x
839 was specified. So we use a terrible hack: if the current
840 stream position is not 0, we assume -x was specified, and
841 give up. Bug 132850 on SourceForge spells out the
842 hopelessness of trying anything else (fseek and ftell
843 don't work predictably x-platform for text-mode files).
845 int ispyc = 0;
846 if (ftell(fp) == 0) {
847 if (fread(buf, 1, 2, fp) == 2 &&
848 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
849 ispyc = 1;
850 rewind(fp);
852 return ispyc;
854 return 0;
858 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
859 PyCompilerFlags *flags)
861 PyObject *m, *d, *v;
862 const char *ext;
863 int set_file_name = 0, ret;
865 m = PyImport_AddModule("__main__");
866 if (m == NULL)
867 return -1;
868 d = PyModule_GetDict(m);
869 if (PyDict_GetItemString(d, "__file__") == NULL) {
870 PyObject *f = PyString_FromString(filename);
871 if (f == NULL)
872 return -1;
873 if (PyDict_SetItemString(d, "__file__", f) < 0) {
874 Py_DECREF(f);
875 return -1;
877 set_file_name = 1;
878 Py_DECREF(f);
880 ext = filename + strlen(filename) - 4;
881 if (maybe_pyc_file(fp, filename, ext, closeit)) {
882 /* Try to run a pyc file. First, re-open in binary */
883 if (closeit)
884 fclose(fp);
885 if ((fp = fopen(filename, "rb")) == NULL) {
886 fprintf(stderr, "python: Can't reopen .pyc file\n");
887 ret = -1;
888 goto done;
890 /* Turn on optimization if a .pyo file is given */
891 if (strcmp(ext, ".pyo") == 0)
892 Py_OptimizeFlag = 1;
893 v = run_pyc_file(fp, filename, d, d, flags);
894 } else {
895 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
896 closeit, flags);
898 if (v == NULL) {
899 PyErr_Print();
900 ret = -1;
901 goto done;
903 Py_DECREF(v);
904 if (Py_FlushLine())
905 PyErr_Clear();
906 ret = 0;
907 done:
908 if (set_file_name && PyDict_DelItemString(d, "__file__"))
909 PyErr_Clear();
910 return ret;
914 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
916 PyObject *m, *d, *v;
917 m = PyImport_AddModule("__main__");
918 if (m == NULL)
919 return -1;
920 d = PyModule_GetDict(m);
921 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
922 if (v == NULL) {
923 PyErr_Print();
924 return -1;
926 Py_DECREF(v);
927 if (Py_FlushLine())
928 PyErr_Clear();
929 return 0;
932 static int
933 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
934 int *lineno, int *offset, const char **text)
936 long hold;
937 PyObject *v;
939 /* old style errors */
940 if (PyTuple_Check(err))
941 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
942 lineno, offset, text);
944 /* new style errors. `err' is an instance */
946 if (! (v = PyObject_GetAttrString(err, "msg")))
947 goto finally;
948 *message = v;
950 if (!(v = PyObject_GetAttrString(err, "filename")))
951 goto finally;
952 if (v == Py_None)
953 *filename = NULL;
954 else if (! (*filename = PyString_AsString(v)))
955 goto finally;
957 Py_DECREF(v);
958 if (!(v = PyObject_GetAttrString(err, "lineno")))
959 goto finally;
960 hold = PyInt_AsLong(v);
961 Py_DECREF(v);
962 v = NULL;
963 if (hold < 0 && PyErr_Occurred())
964 goto finally;
965 *lineno = (int)hold;
967 if (!(v = PyObject_GetAttrString(err, "offset")))
968 goto finally;
969 if (v == Py_None) {
970 *offset = -1;
971 Py_DECREF(v);
972 v = NULL;
973 } else {
974 hold = PyInt_AsLong(v);
975 Py_DECREF(v);
976 v = NULL;
977 if (hold < 0 && PyErr_Occurred())
978 goto finally;
979 *offset = (int)hold;
982 if (!(v = PyObject_GetAttrString(err, "text")))
983 goto finally;
984 if (v == Py_None)
985 *text = NULL;
986 else if (! (*text = PyString_AsString(v)))
987 goto finally;
988 Py_DECREF(v);
989 return 1;
991 finally:
992 Py_XDECREF(v);
993 return 0;
996 void
997 PyErr_Print(void)
999 PyErr_PrintEx(1);
1002 static void
1003 print_error_text(PyObject *f, int offset, const char *text)
1005 char *nl;
1006 if (offset >= 0) {
1007 if (offset > 0 && offset == (int)strlen(text))
1008 offset--;
1009 for (;;) {
1010 nl = strchr(text, '\n');
1011 if (nl == NULL || nl-text >= offset)
1012 break;
1013 offset -= (int)(nl+1-text);
1014 text = nl+1;
1016 while (*text == ' ' || *text == '\t') {
1017 text++;
1018 offset--;
1021 PyFile_WriteString(" ", f);
1022 PyFile_WriteString(text, f);
1023 if (*text == '\0' || text[strlen(text)-1] != '\n')
1024 PyFile_WriteString("\n", f);
1025 if (offset == -1)
1026 return;
1027 PyFile_WriteString(" ", f);
1028 offset--;
1029 while (offset > 0) {
1030 PyFile_WriteString(" ", f);
1031 offset--;
1033 PyFile_WriteString("^\n", f);
1036 static void
1037 handle_system_exit(void)
1039 PyObject *exception, *value, *tb;
1040 int exitcode = 0;
1042 if (Py_InspectFlag)
1043 /* Don't exit if -i flag was given. This flag is set to 0
1044 * when entering interactive mode for inspecting. */
1045 return;
1047 PyErr_Fetch(&exception, &value, &tb);
1048 if (Py_FlushLine())
1049 PyErr_Clear();
1050 fflush(stdout);
1051 if (value == NULL || value == Py_None)
1052 goto done;
1053 if (PyExceptionInstance_Check(value)) {
1054 /* The error code should be in the `code' attribute. */
1055 PyObject *code = PyObject_GetAttrString(value, "code");
1056 if (code) {
1057 Py_DECREF(value);
1058 value = code;
1059 if (value == Py_None)
1060 goto done;
1062 /* If we failed to dig out the 'code' attribute,
1063 just let the else clause below print the error. */
1065 if (PyInt_Check(value))
1066 exitcode = (int)PyInt_AsLong(value);
1067 else {
1068 PyObject_Print(value, stderr, Py_PRINT_RAW);
1069 PySys_WriteStderr("\n");
1070 exitcode = 1;
1072 done:
1073 /* Restore and clear the exception info, in order to properly decref
1074 * the exception, value, and traceback. If we just exit instead,
1075 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1076 * some finalizers from running.
1078 PyErr_Restore(exception, value, tb);
1079 PyErr_Clear();
1080 Py_Exit(exitcode);
1081 /* NOTREACHED */
1084 void
1085 PyErr_PrintEx(int set_sys_last_vars)
1087 PyObject *exception, *v, *tb, *hook;
1089 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1090 handle_system_exit();
1092 PyErr_Fetch(&exception, &v, &tb);
1093 if (exception == NULL)
1094 return;
1095 PyErr_NormalizeException(&exception, &v, &tb);
1096 if (exception == NULL)
1097 return;
1098 /* Now we know v != NULL too */
1099 if (set_sys_last_vars) {
1100 PySys_SetObject("last_type", exception);
1101 PySys_SetObject("last_value", v);
1102 PySys_SetObject("last_traceback", tb);
1104 hook = PySys_GetObject("excepthook");
1105 if (hook) {
1106 PyObject *args = PyTuple_Pack(3,
1107 exception, v, tb ? tb : Py_None);
1108 PyObject *result = PyEval_CallObject(hook, args);
1109 if (result == NULL) {
1110 PyObject *exception2, *v2, *tb2;
1111 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1112 handle_system_exit();
1114 PyErr_Fetch(&exception2, &v2, &tb2);
1115 PyErr_NormalizeException(&exception2, &v2, &tb2);
1116 /* It should not be possible for exception2 or v2
1117 to be NULL. However PyErr_Display() can't
1118 tolerate NULLs, so just be safe. */
1119 if (exception2 == NULL) {
1120 exception2 = Py_None;
1121 Py_INCREF(exception2);
1123 if (v2 == NULL) {
1124 v2 = Py_None;
1125 Py_INCREF(v2);
1127 if (Py_FlushLine())
1128 PyErr_Clear();
1129 fflush(stdout);
1130 PySys_WriteStderr("Error in sys.excepthook:\n");
1131 PyErr_Display(exception2, v2, tb2);
1132 PySys_WriteStderr("\nOriginal exception was:\n");
1133 PyErr_Display(exception, v, tb);
1134 Py_DECREF(exception2);
1135 Py_DECREF(v2);
1136 Py_XDECREF(tb2);
1138 Py_XDECREF(result);
1139 Py_XDECREF(args);
1140 } else {
1141 PySys_WriteStderr("sys.excepthook is missing\n");
1142 PyErr_Display(exception, v, tb);
1144 Py_XDECREF(exception);
1145 Py_XDECREF(v);
1146 Py_XDECREF(tb);
1149 void
1150 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1152 int err = 0;
1153 PyObject *f = PySys_GetObject("stderr");
1154 Py_INCREF(value);
1155 if (f == NULL)
1156 fprintf(stderr, "lost sys.stderr\n");
1157 else {
1158 if (Py_FlushLine())
1159 PyErr_Clear();
1160 fflush(stdout);
1161 if (tb && tb != Py_None)
1162 err = PyTraceBack_Print(tb, f);
1163 if (err == 0 &&
1164 PyObject_HasAttrString(value, "print_file_and_line"))
1166 PyObject *message;
1167 const char *filename, *text;
1168 int lineno, offset;
1169 if (!parse_syntax_error(value, &message, &filename,
1170 &lineno, &offset, &text))
1171 PyErr_Clear();
1172 else {
1173 char buf[10];
1174 PyFile_WriteString(" File \"", f);
1175 if (filename == NULL)
1176 PyFile_WriteString("<string>", f);
1177 else
1178 PyFile_WriteString(filename, f);
1179 PyFile_WriteString("\", line ", f);
1180 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1181 PyFile_WriteString(buf, f);
1182 PyFile_WriteString("\n", f);
1183 if (text != NULL)
1184 print_error_text(f, offset, text);
1185 Py_DECREF(value);
1186 value = message;
1187 /* Can't be bothered to check all those
1188 PyFile_WriteString() calls */
1189 if (PyErr_Occurred())
1190 err = -1;
1193 if (err) {
1194 /* Don't do anything else */
1196 else if (PyExceptionClass_Check(exception)) {
1197 PyObject* moduleName;
1198 char* className = PyExceptionClass_Name(exception);
1199 if (className != NULL) {
1200 char *dot = strrchr(className, '.');
1201 if (dot != NULL)
1202 className = dot+1;
1205 moduleName = PyObject_GetAttrString(exception, "__module__");
1206 if (moduleName == NULL)
1207 err = PyFile_WriteString("<unknown>", f);
1208 else {
1209 char* modstr = PyString_AsString(moduleName);
1210 if (modstr && strcmp(modstr, "exceptions"))
1212 err = PyFile_WriteString(modstr, f);
1213 err += PyFile_WriteString(".", f);
1215 Py_DECREF(moduleName);
1217 if (err == 0) {
1218 if (className == NULL)
1219 err = PyFile_WriteString("<unknown>", f);
1220 else
1221 err = PyFile_WriteString(className, f);
1224 else
1225 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1226 if (err == 0 && (value != Py_None)) {
1227 PyObject *s = PyObject_Str(value);
1228 /* only print colon if the str() of the
1229 object is not the empty string
1231 if (s == NULL)
1232 err = -1;
1233 else if (!PyString_Check(s) ||
1234 PyString_GET_SIZE(s) != 0)
1235 err = PyFile_WriteString(": ", f);
1236 if (err == 0)
1237 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1238 Py_XDECREF(s);
1240 /* try to write a newline in any case */
1241 err += PyFile_WriteString("\n", f);
1243 Py_DECREF(value);
1244 /* If an error happened here, don't show it.
1245 XXX This is wrong, but too many callers rely on this behavior. */
1246 if (err != 0)
1247 PyErr_Clear();
1250 PyObject *
1251 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1252 PyObject *locals, PyCompilerFlags *flags)
1254 PyObject *ret = NULL;
1255 mod_ty mod;
1256 PyArena *arena = PyArena_New();
1257 if (arena == NULL)
1258 return NULL;
1260 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1261 if (mod != NULL)
1262 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1263 PyArena_Free(arena);
1264 return ret;
1267 PyObject *
1268 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1269 PyObject *locals, int closeit, PyCompilerFlags *flags)
1271 PyObject *ret;
1272 mod_ty mod;
1273 PyArena *arena = PyArena_New();
1274 if (arena == NULL)
1275 return NULL;
1277 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1278 flags, NULL, arena);
1279 if (closeit)
1280 fclose(fp);
1281 if (mod == NULL) {
1282 PyArena_Free(arena);
1283 return NULL;
1285 ret = run_mod(mod, filename, globals, locals, flags, arena);
1286 PyArena_Free(arena);
1287 return ret;
1290 static PyObject *
1291 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1292 PyCompilerFlags *flags, PyArena *arena)
1294 PyCodeObject *co;
1295 PyObject *v;
1296 co = PyAST_Compile(mod, filename, flags, arena);
1297 if (co == NULL)
1298 return NULL;
1299 v = PyEval_EvalCode(co, globals, locals);
1300 Py_DECREF(co);
1301 return v;
1304 static PyObject *
1305 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1306 PyObject *locals, PyCompilerFlags *flags)
1308 PyCodeObject *co;
1309 PyObject *v;
1310 long magic;
1311 long PyImport_GetMagicNumber(void);
1313 magic = PyMarshal_ReadLongFromFile(fp);
1314 if (magic != PyImport_GetMagicNumber()) {
1315 PyErr_SetString(PyExc_RuntimeError,
1316 "Bad magic number in .pyc file");
1317 return NULL;
1319 (void) PyMarshal_ReadLongFromFile(fp);
1320 v = PyMarshal_ReadLastObjectFromFile(fp);
1321 fclose(fp);
1322 if (v == NULL || !PyCode_Check(v)) {
1323 Py_XDECREF(v);
1324 PyErr_SetString(PyExc_RuntimeError,
1325 "Bad code object in .pyc file");
1326 return NULL;
1328 co = (PyCodeObject *)v;
1329 v = PyEval_EvalCode(co, globals, locals);
1330 if (v && flags)
1331 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1332 Py_DECREF(co);
1333 return v;
1336 PyObject *
1337 Py_CompileStringFlags(const char *str, const char *filename, int start,
1338 PyCompilerFlags *flags)
1340 PyCodeObject *co;
1341 mod_ty mod;
1342 PyArena *arena = PyArena_New();
1343 if (arena == NULL)
1344 return NULL;
1346 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1347 if (mod == NULL) {
1348 PyArena_Free(arena);
1349 return NULL;
1351 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1352 PyObject *result = PyAST_mod2obj(mod);
1353 PyArena_Free(arena);
1354 return result;
1356 co = PyAST_Compile(mod, filename, flags, arena);
1357 PyArena_Free(arena);
1358 return (PyObject *)co;
1361 struct symtable *
1362 Py_SymtableString(const char *str, const char *filename, int start)
1364 struct symtable *st;
1365 mod_ty mod;
1366 PyArena *arena = PyArena_New();
1367 if (arena == NULL)
1368 return NULL;
1370 mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
1371 if (mod == NULL) {
1372 PyArena_Free(arena);
1373 return NULL;
1375 st = PySymtable_Build(mod, filename, 0);
1376 PyArena_Free(arena);
1377 return st;
1380 /* Preferred access to parser is through AST. */
1381 mod_ty
1382 PyParser_ASTFromString(const char *s, const char *filename, int start,
1383 PyCompilerFlags *flags, PyArena *arena)
1385 mod_ty mod;
1386 perrdetail err;
1387 node *n = PyParser_ParseStringFlagsFilename(s, filename,
1388 &_PyParser_Grammar, start, &err,
1389 PARSER_FLAGS(flags));
1390 if (n) {
1391 mod = PyAST_FromNode(n, flags, filename, arena);
1392 PyNode_Free(n);
1393 return mod;
1395 else {
1396 err_input(&err);
1397 return NULL;
1401 mod_ty
1402 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1403 char *ps2, PyCompilerFlags *flags, int *errcode,
1404 PyArena *arena)
1406 mod_ty mod;
1407 perrdetail err;
1408 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1409 start, ps1, ps2, &err, PARSER_FLAGS(flags));
1410 if (n) {
1411 mod = PyAST_FromNode(n, flags, filename, arena);
1412 PyNode_Free(n);
1413 return mod;
1415 else {
1416 err_input(&err);
1417 if (errcode)
1418 *errcode = err.error;
1419 return NULL;
1423 /* Simplified interface to parsefile -- return node or set exception */
1425 node *
1426 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1428 perrdetail err;
1429 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1430 start, NULL, NULL, &err, flags);
1431 if (n == NULL)
1432 err_input(&err);
1434 return n;
1437 /* Simplified interface to parsestring -- return node or set exception */
1439 node *
1440 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1442 perrdetail err;
1443 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1444 start, &err, flags);
1445 if (n == NULL)
1446 err_input(&err);
1447 return n;
1450 node *
1451 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1452 int start, int flags)
1454 perrdetail err;
1455 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1456 &_PyParser_Grammar, start, &err, flags);
1457 if (n == NULL)
1458 err_input(&err);
1459 return n;
1462 node *
1463 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1465 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1468 /* May want to move a more generalized form of this to parsetok.c or
1469 even parser modules. */
1471 void
1472 PyParser_SetError(perrdetail *err)
1474 err_input(err);
1477 /* Set the error appropriate to the given input error code (see errcode.h) */
1479 static void
1480 err_input(perrdetail *err)
1482 PyObject *v, *w, *errtype;
1483 PyObject* u = NULL;
1484 char *msg = NULL;
1485 errtype = PyExc_SyntaxError;
1486 switch (err->error) {
1487 case E_SYNTAX:
1488 errtype = PyExc_IndentationError;
1489 if (err->expected == INDENT)
1490 msg = "expected an indented block";
1491 else if (err->token == INDENT)
1492 msg = "unexpected indent";
1493 else if (err->token == DEDENT)
1494 msg = "unexpected unindent";
1495 else {
1496 errtype = PyExc_SyntaxError;
1497 msg = "invalid syntax";
1499 break;
1500 case E_TOKEN:
1501 msg = "invalid token";
1502 break;
1503 case E_EOFS:
1504 msg = "EOF while scanning triple-quoted string";
1505 break;
1506 case E_EOLS:
1507 msg = "EOL while scanning single-quoted string";
1508 break;
1509 case E_INTR:
1510 if (!PyErr_Occurred())
1511 PyErr_SetNone(PyExc_KeyboardInterrupt);
1512 return;
1513 case E_NOMEM:
1514 PyErr_NoMemory();
1515 return;
1516 case E_EOF:
1517 msg = "unexpected EOF while parsing";
1518 break;
1519 case E_TABSPACE:
1520 errtype = PyExc_TabError;
1521 msg = "inconsistent use of tabs and spaces in indentation";
1522 break;
1523 case E_OVERFLOW:
1524 msg = "expression too long";
1525 break;
1526 case E_DEDENT:
1527 errtype = PyExc_IndentationError;
1528 msg = "unindent does not match any outer indentation level";
1529 break;
1530 case E_TOODEEP:
1531 errtype = PyExc_IndentationError;
1532 msg = "too many levels of indentation";
1533 break;
1534 case E_DECODE: {
1535 PyObject *type, *value, *tb;
1536 PyErr_Fetch(&type, &value, &tb);
1537 if (value != NULL) {
1538 u = PyObject_Str(value);
1539 if (u != NULL) {
1540 msg = PyString_AsString(u);
1543 if (msg == NULL)
1544 msg = "unknown decode error";
1545 Py_XDECREF(type);
1546 Py_XDECREF(value);
1547 Py_XDECREF(tb);
1548 break;
1550 case E_LINECONT:
1551 msg = "unexpected character after line continuation character";
1552 break;
1553 default:
1554 fprintf(stderr, "error=%d\n", err->error);
1555 msg = "unknown parsing error";
1556 break;
1558 v = Py_BuildValue("(ziiz)", err->filename,
1559 err->lineno, err->offset, err->text);
1560 if (err->text != NULL) {
1561 PyObject_FREE(err->text);
1562 err->text = NULL;
1564 w = NULL;
1565 if (v != NULL)
1566 w = Py_BuildValue("(sO)", msg, v);
1567 Py_XDECREF(u);
1568 Py_XDECREF(v);
1569 PyErr_SetObject(errtype, w);
1570 Py_XDECREF(w);
1573 /* Print fatal error message and abort */
1575 void
1576 Py_FatalError(const char *msg)
1578 fprintf(stderr, "Fatal Python error: %s\n", msg);
1579 #ifdef MS_WINDOWS
1580 OutputDebugString("Fatal Python error: ");
1581 OutputDebugString(msg);
1582 OutputDebugString("\n");
1583 #ifdef _DEBUG
1584 DebugBreak();
1585 #endif
1586 #endif /* MS_WINDOWS */
1587 abort();
1590 /* Clean up and exit */
1592 #ifdef WITH_THREAD
1593 #include "pythread.h"
1594 #endif
1596 #define NEXITFUNCS 32
1597 static void (*exitfuncs[NEXITFUNCS])(void);
1598 static int nexitfuncs = 0;
1600 int Py_AtExit(void (*func)(void))
1602 if (nexitfuncs >= NEXITFUNCS)
1603 return -1;
1604 exitfuncs[nexitfuncs++] = func;
1605 return 0;
1608 static void
1609 call_sys_exitfunc(void)
1611 PyObject *exitfunc = PySys_GetObject("exitfunc");
1613 if (exitfunc) {
1614 PyObject *res;
1615 Py_INCREF(exitfunc);
1616 PySys_SetObject("exitfunc", (PyObject *)NULL);
1617 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1618 if (res == NULL) {
1619 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1620 PySys_WriteStderr("Error in sys.exitfunc:\n");
1622 PyErr_Print();
1624 Py_DECREF(exitfunc);
1627 if (Py_FlushLine())
1628 PyErr_Clear();
1631 static void
1632 call_ll_exitfuncs(void)
1634 while (nexitfuncs > 0)
1635 (*exitfuncs[--nexitfuncs])();
1637 fflush(stdout);
1638 fflush(stderr);
1641 void
1642 Py_Exit(int sts)
1644 Py_Finalize();
1646 exit(sts);
1649 static void
1650 initsigs(void)
1652 #ifdef SIGPIPE
1653 PyOS_setsig(SIGPIPE, SIG_IGN);
1654 #endif
1655 #ifdef SIGXFZ
1656 PyOS_setsig(SIGXFZ, SIG_IGN);
1657 #endif
1658 #ifdef SIGXFSZ
1659 PyOS_setsig(SIGXFSZ, SIG_IGN);
1660 #endif
1661 PyOS_InitInterrupts(); /* May imply initsignal() */
1666 * The file descriptor fd is considered ``interactive'' if either
1667 * a) isatty(fd) is TRUE, or
1668 * b) the -i flag was given, and the filename associated with
1669 * the descriptor is NULL or "<stdin>" or "???".
1672 Py_FdIsInteractive(FILE *fp, const char *filename)
1674 if (isatty((int)fileno(fp)))
1675 return 1;
1676 if (!Py_InteractiveFlag)
1677 return 0;
1678 return (filename == NULL) ||
1679 (strcmp(filename, "<stdin>") == 0) ||
1680 (strcmp(filename, "???") == 0);
1684 #if defined(USE_STACKCHECK)
1685 #if defined(WIN32) && defined(_MSC_VER)
1687 /* Stack checking for Microsoft C */
1689 #include <malloc.h>
1690 #include <excpt.h>
1693 * Return non-zero when we run out of memory on the stack; zero otherwise.
1696 PyOS_CheckStack(void)
1698 __try {
1699 /* alloca throws a stack overflow exception if there's
1700 not enough space left on the stack */
1701 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1702 return 0;
1703 } __except (EXCEPTION_EXECUTE_HANDLER) {
1704 /* just ignore all errors */
1706 return 1;
1709 #endif /* WIN32 && _MSC_VER */
1711 /* Alternate implementations can be added here... */
1713 #endif /* USE_STACKCHECK */
1716 /* Wrappers around sigaction() or signal(). */
1718 PyOS_sighandler_t
1719 PyOS_getsig(int sig)
1721 #ifdef HAVE_SIGACTION
1722 struct sigaction context;
1723 if (sigaction(sig, NULL, &context) == -1)
1724 return SIG_ERR;
1725 return context.sa_handler;
1726 #else
1727 PyOS_sighandler_t handler;
1728 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1729 #if defined(_MSC_VER) && _MSC_VER >= 1400
1730 switch (sig) {
1731 /* Only these signals are valid */
1732 case SIGINT:
1733 case SIGILL:
1734 case SIGFPE:
1735 case SIGSEGV:
1736 case SIGTERM:
1737 case SIGBREAK:
1738 case SIGABRT:
1739 break;
1740 /* Don't call signal() with other values or it will assert */
1741 default:
1742 return SIG_ERR;
1744 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1745 handler = signal(sig, SIG_IGN);
1746 if (handler != SIG_ERR)
1747 signal(sig, handler);
1748 return handler;
1749 #endif
1752 PyOS_sighandler_t
1753 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1755 #ifdef HAVE_SIGACTION
1756 struct sigaction context, ocontext;
1757 context.sa_handler = handler;
1758 sigemptyset(&context.sa_mask);
1759 context.sa_flags = 0;
1760 if (sigaction(sig, &context, &ocontext) == -1)
1761 return SIG_ERR;
1762 return ocontext.sa_handler;
1763 #else
1764 PyOS_sighandler_t oldhandler;
1765 oldhandler = signal(sig, handler);
1766 #ifdef HAVE_SIGINTERRUPT
1767 siginterrupt(sig, 1);
1768 #endif
1769 return oldhandler;
1770 #endif
1773 /* Deprecated C API functions still provided for binary compatiblity */
1775 #undef PyParser_SimpleParseFile
1776 PyAPI_FUNC(node *)
1777 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1779 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1782 #undef PyParser_SimpleParseString
1783 PyAPI_FUNC(node *)
1784 PyParser_SimpleParseString(const char *str, int start)
1786 return PyParser_SimpleParseStringFlags(str, start, 0);
1789 #undef PyRun_AnyFile
1790 PyAPI_FUNC(int)
1791 PyRun_AnyFile(FILE *fp, const char *name)
1793 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1796 #undef PyRun_AnyFileEx
1797 PyAPI_FUNC(int)
1798 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1800 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1803 #undef PyRun_AnyFileFlags
1804 PyAPI_FUNC(int)
1805 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1807 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1810 #undef PyRun_File
1811 PyAPI_FUNC(PyObject *)
1812 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1814 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1817 #undef PyRun_FileEx
1818 PyAPI_FUNC(PyObject *)
1819 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1821 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1824 #undef PyRun_FileFlags
1825 PyAPI_FUNC(PyObject *)
1826 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1827 PyCompilerFlags *flags)
1829 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1832 #undef PyRun_SimpleFile
1833 PyAPI_FUNC(int)
1834 PyRun_SimpleFile(FILE *f, const char *p)
1836 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1839 #undef PyRun_SimpleFileEx
1840 PyAPI_FUNC(int)
1841 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1843 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1847 #undef PyRun_String
1848 PyAPI_FUNC(PyObject *)
1849 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1851 return PyRun_StringFlags(str, s, g, l, NULL);
1854 #undef PyRun_SimpleString
1855 PyAPI_FUNC(int)
1856 PyRun_SimpleString(const char *s)
1858 return PyRun_SimpleStringFlags(s, NULL);
1861 #undef Py_CompileString
1862 PyAPI_FUNC(PyObject *)
1863 Py_CompileString(const char *str, const char *p, int s)
1865 return Py_CompileStringFlags(str, p, s, NULL);
1868 #undef PyRun_InteractiveOne
1869 PyAPI_FUNC(int)
1870 PyRun_InteractiveOne(FILE *f, const char *p)
1872 return PyRun_InteractiveOneFlags(f, p, NULL);
1875 #undef PyRun_InteractiveLoop
1876 PyAPI_FUNC(int)
1877 PyRun_InteractiveLoop(FILE *f, const char *p)
1879 return PyRun_InteractiveLoopFlags(f, p, NULL);
1882 #ifdef __cplusplus
1884 #endif