Issue #5175: PyLong_AsUnsignedLongLong now raises OverflowError for
[python.git] / Python / pythonrun.c
blobf2cd819b1c4d2de1e5300c88f43930ef48789b5d
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 MS_WINDOWS
26 #include "malloc.h" /* for alloca */
27 #endif
29 #ifdef HAVE_LANGINFO_H
30 #include <locale.h>
31 #include <langinfo.h>
32 #endif
34 #ifdef MS_WINDOWS
35 #undef BYTE
36 #include "windows.h"
37 #endif
39 #ifndef Py_REF_DEBUG
40 #define PRINT_TOTAL_REFS()
41 #else /* Py_REF_DEBUG */
42 #define PRINT_TOTAL_REFS() fprintf(stderr, \
43 "[%" PY_FORMAT_SIZE_T "d refs]\n", \
44 _Py_GetRefTotal())
45 #endif
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
51 extern char *Py_GetPath(void);
53 extern grammar _PyParser_Grammar; /* From graminit.c */
55 /* Forward */
56 static void initmain(void);
57 static void initsite(void);
58 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
59 PyCompilerFlags *, PyArena *);
60 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
61 PyCompilerFlags *);
62 static void err_input(perrdetail *);
63 static void initsigs(void);
64 static void call_sys_exitfunc(void);
65 static void call_ll_exitfuncs(void);
66 extern void _PyUnicode_Init(void);
67 extern void _PyUnicode_Fini(void);
69 #ifdef WITH_THREAD
70 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
71 extern void _PyGILState_Fini(void);
72 #endif /* WITH_THREAD */
74 int Py_DebugFlag; /* Needed by parser.c */
75 int Py_VerboseFlag; /* Needed by import.c */
76 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
77 int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
78 int Py_NoSiteFlag; /* Suppress 'import site' */
79 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
80 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
81 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
82 int Py_FrozenFlag; /* Needed by getpath.c */
83 int Py_UnicodeFlag = 0; /* Needed by compile.c */
84 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
85 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
86 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
87 true divisions (which they will be in 2.3). */
88 int _Py_QnewFlag = 0;
89 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
91 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
92 since _warnings is builtin. This API should not be used. */
93 PyObject *
94 PyModule_GetWarningsModule(void)
96 return PyImport_ImportModule("warnings");
99 static int initialized = 0;
101 /* API to access the initialized flag -- useful for esoteric use */
104 Py_IsInitialized(void)
106 return initialized;
109 /* Global initializations. Can be undone by Py_Finalize(). Don't
110 call this twice without an intervening Py_Finalize() call. When
111 initializations fail, a fatal error is issued and the function does
112 not return. On return, the first thread and interpreter state have
113 been created.
115 Locking: you must hold the interpreter lock while calling this.
116 (If the lock has not yet been initialized, that's equivalent to
117 having the lock, but you cannot use multiple threads.)
121 static int
122 add_flag(int flag, const char *envs)
124 int env = atoi(envs);
125 if (flag < env)
126 flag = env;
127 if (flag < 1)
128 flag = 1;
129 return flag;
132 void
133 Py_InitializeEx(int install_sigs)
135 PyInterpreterState *interp;
136 PyThreadState *tstate;
137 PyObject *bimod, *sysmod;
138 char *p;
139 char *icodeset = NULL; /* On Windows, input codeset may theoretically
140 differ from output codeset. */
141 char *codeset = NULL;
142 char *errors = NULL;
143 int free_codeset = 0;
144 int overridden = 0;
145 PyObject *sys_stream, *sys_isatty;
146 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
147 char *saved_locale, *loc_codeset;
148 #endif
149 #ifdef MS_WINDOWS
150 char ibuf[128];
151 char buf[128];
152 #endif
153 extern void _Py_ReadyTypes(void);
155 if (initialized)
156 return;
157 initialized = 1;
159 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
160 Py_DebugFlag = add_flag(Py_DebugFlag, p);
161 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
162 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
163 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
164 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
165 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
166 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
168 interp = PyInterpreterState_New();
169 if (interp == NULL)
170 Py_FatalError("Py_Initialize: can't make first interpreter");
172 tstate = PyThreadState_New(interp);
173 if (tstate == NULL)
174 Py_FatalError("Py_Initialize: can't make first thread");
175 (void) PyThreadState_Swap(tstate);
177 _Py_ReadyTypes();
179 if (!_PyFrame_Init())
180 Py_FatalError("Py_Initialize: can't init frames");
182 if (!_PyInt_Init())
183 Py_FatalError("Py_Initialize: can't init ints");
185 if (!PyByteArray_Init())
186 Py_FatalError("Py_Initialize: can't init bytearray");
188 _PyFloat_Init();
190 interp->modules = PyDict_New();
191 if (interp->modules == NULL)
192 Py_FatalError("Py_Initialize: can't make modules dictionary");
193 interp->modules_reloading = PyDict_New();
194 if (interp->modules_reloading == NULL)
195 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
197 #ifdef Py_USING_UNICODE
198 /* Init Unicode implementation; relies on the codec registry */
199 _PyUnicode_Init();
200 #endif
202 bimod = _PyBuiltin_Init();
203 if (bimod == NULL)
204 Py_FatalError("Py_Initialize: can't initialize __builtin__");
205 interp->builtins = PyModule_GetDict(bimod);
206 if (interp->builtins == NULL)
207 Py_FatalError("Py_Initialize: can't initialize builtins dict");
208 Py_INCREF(interp->builtins);
210 sysmod = _PySys_Init();
211 if (sysmod == NULL)
212 Py_FatalError("Py_Initialize: can't initialize sys");
213 interp->sysdict = PyModule_GetDict(sysmod);
214 if (interp->sysdict == NULL)
215 Py_FatalError("Py_Initialize: can't initialize sys dict");
216 Py_INCREF(interp->sysdict);
217 _PyImport_FixupExtension("sys", "sys");
218 PySys_SetPath(Py_GetPath());
219 PyDict_SetItemString(interp->sysdict, "modules",
220 interp->modules);
222 _PyImport_Init();
224 /* initialize builtin exceptions */
225 _PyExc_Init();
226 _PyImport_FixupExtension("exceptions", "exceptions");
228 /* phase 2 of builtins */
229 _PyImport_FixupExtension("__builtin__", "__builtin__");
231 _PyImportHooks_Init();
233 if (install_sigs)
234 initsigs(); /* Signal handling stuff, including initintr() */
236 /* Initialize warnings. */
237 _PyWarnings_Init();
238 if (PySys_HasWarnOptions()) {
239 PyObject *warnings_module = PyImport_ImportModule("warnings");
240 if (!warnings_module)
241 PyErr_Clear();
242 Py_XDECREF(warnings_module);
245 initmain(); /* Module __main__ */
246 if (!Py_NoSiteFlag)
247 initsite(); /* Module site */
249 /* auto-thread-state API, if available */
250 #ifdef WITH_THREAD
251 _PyGILState_Init(interp, tstate);
252 #endif /* WITH_THREAD */
254 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
255 p = icodeset = codeset = strdup(p);
256 free_codeset = 1;
257 errors = strchr(p, ':');
258 if (errors) {
259 *errors = '\0';
260 errors++;
262 overridden = 1;
265 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
266 /* On Unix, set the file system encoding according to the
267 user's preference, if the CODESET names a well-known
268 Python codec, and Py_FileSystemDefaultEncoding isn't
269 initialized by other means. Also set the encoding of
270 stdin and stdout if these are terminals, unless overridden. */
272 if (!overridden || !Py_FileSystemDefaultEncoding) {
273 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
274 setlocale(LC_CTYPE, "");
275 loc_codeset = nl_langinfo(CODESET);
276 if (loc_codeset && *loc_codeset) {
277 PyObject *enc = PyCodec_Encoder(loc_codeset);
278 if (enc) {
279 loc_codeset = strdup(loc_codeset);
280 Py_DECREF(enc);
281 } else {
282 loc_codeset = NULL;
283 PyErr_Clear();
285 } else
286 loc_codeset = NULL;
287 setlocale(LC_CTYPE, saved_locale);
288 free(saved_locale);
290 if (!overridden) {
291 codeset = icodeset = loc_codeset;
292 free_codeset = 1;
295 /* Initialize Py_FileSystemDefaultEncoding from
296 locale even if PYTHONIOENCODING is set. */
297 if (!Py_FileSystemDefaultEncoding) {
298 Py_FileSystemDefaultEncoding = loc_codeset;
299 if (!overridden)
300 free_codeset = 0;
303 #endif
305 #ifdef MS_WINDOWS
306 if (!overridden) {
307 icodeset = ibuf;
308 codeset = buf;
309 sprintf(ibuf, "cp%d", GetConsoleCP());
310 sprintf(buf, "cp%d", GetConsoleOutputCP());
312 #endif
314 if (codeset) {
315 sys_stream = PySys_GetObject("stdin");
316 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
317 if (!sys_isatty)
318 PyErr_Clear();
319 if ((overridden ||
320 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
321 PyFile_Check(sys_stream)) {
322 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
323 Py_FatalError("Cannot set codeset of stdin");
325 Py_XDECREF(sys_isatty);
327 sys_stream = PySys_GetObject("stdout");
328 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
329 if (!sys_isatty)
330 PyErr_Clear();
331 if ((overridden ||
332 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
333 PyFile_Check(sys_stream)) {
334 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
335 Py_FatalError("Cannot set codeset of stdout");
337 Py_XDECREF(sys_isatty);
339 sys_stream = PySys_GetObject("stderr");
340 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
341 if (!sys_isatty)
342 PyErr_Clear();
343 if((overridden ||
344 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
345 PyFile_Check(sys_stream)) {
346 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
347 Py_FatalError("Cannot set codeset of stderr");
349 Py_XDECREF(sys_isatty);
351 if (free_codeset)
352 free(codeset);
356 void
357 Py_Initialize(void)
359 Py_InitializeEx(1);
363 #ifdef COUNT_ALLOCS
364 extern void dump_counts(FILE*);
365 #endif
367 /* Undo the effect of Py_Initialize().
369 Beware: if multiple interpreter and/or thread states exist, these
370 are not wiped out; only the current thread and interpreter state
371 are deleted. But since everything else is deleted, those other
372 interpreter and thread states should no longer be used.
374 (XXX We should do better, e.g. wipe out all interpreters and
375 threads.)
377 Locking: as above.
381 void
382 Py_Finalize(void)
384 PyInterpreterState *interp;
385 PyThreadState *tstate;
387 if (!initialized)
388 return;
390 /* The interpreter is still entirely intact at this point, and the
391 * exit funcs may be relying on that. In particular, if some thread
392 * or exit func is still waiting to do an import, the import machinery
393 * expects Py_IsInitialized() to return true. So don't say the
394 * interpreter is uninitialized until after the exit funcs have run.
395 * Note that Threading.py uses an exit func to do a join on all the
396 * threads created thru it, so this also protects pending imports in
397 * the threads created via Threading.
399 call_sys_exitfunc();
400 initialized = 0;
402 /* Get current thread state and interpreter pointer */
403 tstate = PyThreadState_GET();
404 interp = tstate->interp;
406 /* Disable signal handling */
407 PyOS_FiniInterrupts();
409 /* Clear type lookup cache */
410 PyType_ClearCache();
412 /* Collect garbage. This may call finalizers; it's nice to call these
413 * before all modules are destroyed.
414 * XXX If a __del__ or weakref callback is triggered here, and tries to
415 * XXX import a module, bad things can happen, because Python no
416 * XXX longer believes it's initialized.
417 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
418 * XXX is easy to provoke that way. I've also seen, e.g.,
419 * XXX Exception exceptions.ImportError: 'No module named sha'
420 * XXX in <function callback at 0x008F5718> ignored
421 * XXX but I'm unclear on exactly how that one happens. In any case,
422 * XXX I haven't seen a real-life report of either of these.
424 PyGC_Collect();
425 #ifdef COUNT_ALLOCS
426 /* With COUNT_ALLOCS, it helps to run GC multiple times:
427 each collection might release some types from the type
428 list, so they become garbage. */
429 while (PyGC_Collect() > 0)
430 /* nothing */;
431 #endif
433 /* Destroy all modules */
434 PyImport_Cleanup();
436 /* Collect final garbage. This disposes of cycles created by
437 * new-style class definitions, for example.
438 * XXX This is disabled because it caused too many problems. If
439 * XXX a __del__ or weakref callback triggers here, Python code has
440 * XXX a hard time running, because even the sys module has been
441 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
442 * XXX One symptom is a sequence of information-free messages
443 * XXX coming from threads (if a __del__ or callback is invoked,
444 * XXX other threads can execute too, and any exception they encounter
445 * XXX triggers a comedy of errors as subsystem after subsystem
446 * XXX fails to find what it *expects* to find in sys to help report
447 * XXX the exception and consequent unexpected failures). I've also
448 * XXX seen segfaults then, after adding print statements to the
449 * XXX Python code getting called.
451 #if 0
452 PyGC_Collect();
453 #endif
455 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
456 _PyImport_Fini();
458 /* Debugging stuff */
459 #ifdef COUNT_ALLOCS
460 dump_counts(stdout);
461 #endif
463 PRINT_TOTAL_REFS();
465 #ifdef Py_TRACE_REFS
466 /* Display all objects still alive -- this can invoke arbitrary
467 * __repr__ overrides, so requires a mostly-intact interpreter.
468 * Alas, a lot of stuff may still be alive now that will be cleaned
469 * up later.
471 if (Py_GETENV("PYTHONDUMPREFS"))
472 _Py_PrintReferences(stderr);
473 #endif /* Py_TRACE_REFS */
475 /* Clear interpreter state */
476 PyInterpreterState_Clear(interp);
478 /* Now we decref the exception classes. After this point nothing
479 can raise an exception. That's okay, because each Fini() method
480 below has been checked to make sure no exceptions are ever
481 raised.
484 _PyExc_Fini();
486 /* Cleanup auto-thread-state */
487 #ifdef WITH_THREAD
488 _PyGILState_Fini();
489 #endif /* WITH_THREAD */
491 /* Delete current thread */
492 PyThreadState_Swap(NULL);
493 PyInterpreterState_Delete(interp);
495 /* Sundry finalizers */
496 PyMethod_Fini();
497 PyFrame_Fini();
498 PyCFunction_Fini();
499 PyTuple_Fini();
500 PyList_Fini();
501 PySet_Fini();
502 PyString_Fini();
503 PyByteArray_Fini();
504 PyInt_Fini();
505 PyFloat_Fini();
506 PyDict_Fini();
508 #ifdef Py_USING_UNICODE
509 /* Cleanup Unicode implementation */
510 _PyUnicode_Fini();
511 #endif
513 /* XXX Still allocated:
514 - various static ad-hoc pointers to interned strings
515 - int and float free list blocks
516 - whatever various modules and libraries allocate
519 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
521 #ifdef Py_TRACE_REFS
522 /* Display addresses (& refcnts) of all objects still alive.
523 * An address can be used to find the repr of the object, printed
524 * above by _Py_PrintReferences.
526 if (Py_GETENV("PYTHONDUMPREFS"))
527 _Py_PrintReferenceAddresses(stderr);
528 #endif /* Py_TRACE_REFS */
529 #ifdef PYMALLOC_DEBUG
530 if (Py_GETENV("PYTHONMALLOCSTATS"))
531 _PyObject_DebugMallocStats();
532 #endif
534 call_ll_exitfuncs();
537 /* Create and initialize a new interpreter and thread, and return the
538 new thread. This requires that Py_Initialize() has been called
539 first.
541 Unsuccessful initialization yields a NULL pointer. Note that *no*
542 exception information is available even in this case -- the
543 exception information is held in the thread, and there is no
544 thread.
546 Locking: as above.
550 PyThreadState *
551 Py_NewInterpreter(void)
553 PyInterpreterState *interp;
554 PyThreadState *tstate, *save_tstate;
555 PyObject *bimod, *sysmod;
557 if (!initialized)
558 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
560 interp = PyInterpreterState_New();
561 if (interp == NULL)
562 return NULL;
564 tstate = PyThreadState_New(interp);
565 if (tstate == NULL) {
566 PyInterpreterState_Delete(interp);
567 return NULL;
570 save_tstate = PyThreadState_Swap(tstate);
572 /* XXX The following is lax in error checking */
574 interp->modules = PyDict_New();
575 interp->modules_reloading = PyDict_New();
577 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
578 if (bimod != NULL) {
579 interp->builtins = PyModule_GetDict(bimod);
580 if (interp->builtins == NULL)
581 goto handle_error;
582 Py_INCREF(interp->builtins);
584 sysmod = _PyImport_FindExtension("sys", "sys");
585 if (bimod != NULL && sysmod != NULL) {
586 interp->sysdict = PyModule_GetDict(sysmod);
587 if (interp->sysdict == NULL)
588 goto handle_error;
589 Py_INCREF(interp->sysdict);
590 PySys_SetPath(Py_GetPath());
591 PyDict_SetItemString(interp->sysdict, "modules",
592 interp->modules);
593 _PyImportHooks_Init();
594 initmain();
595 if (!Py_NoSiteFlag)
596 initsite();
599 if (!PyErr_Occurred())
600 return tstate;
602 handle_error:
603 /* Oops, it didn't work. Undo it all. */
605 PyErr_Print();
606 PyThreadState_Clear(tstate);
607 PyThreadState_Swap(save_tstate);
608 PyThreadState_Delete(tstate);
609 PyInterpreterState_Delete(interp);
611 return NULL;
614 /* Delete an interpreter and its last thread. This requires that the
615 given thread state is current, that the thread has no remaining
616 frames, and that it is its interpreter's only remaining thread.
617 It is a fatal error to violate these constraints.
619 (Py_Finalize() doesn't have these constraints -- it zaps
620 everything, regardless.)
622 Locking: as above.
626 void
627 Py_EndInterpreter(PyThreadState *tstate)
629 PyInterpreterState *interp = tstate->interp;
631 if (tstate != PyThreadState_GET())
632 Py_FatalError("Py_EndInterpreter: thread is not current");
633 if (tstate->frame != NULL)
634 Py_FatalError("Py_EndInterpreter: thread still has a frame");
635 if (tstate != interp->tstate_head || tstate->next != NULL)
636 Py_FatalError("Py_EndInterpreter: not the last thread");
638 PyImport_Cleanup();
639 PyInterpreterState_Clear(interp);
640 PyThreadState_Swap(NULL);
641 PyInterpreterState_Delete(interp);
644 static char *progname = "python";
646 void
647 Py_SetProgramName(char *pn)
649 if (pn && *pn)
650 progname = pn;
653 char *
654 Py_GetProgramName(void)
656 return progname;
659 static char *default_home = NULL;
661 void
662 Py_SetPythonHome(char *home)
664 default_home = home;
667 char *
668 Py_GetPythonHome(void)
670 char *home = default_home;
671 if (home == NULL && !Py_IgnoreEnvironmentFlag)
672 home = Py_GETENV("PYTHONHOME");
673 return home;
676 /* Create __main__ module */
678 static void
679 initmain(void)
681 PyObject *m, *d;
682 m = PyImport_AddModule("__main__");
683 if (m == NULL)
684 Py_FatalError("can't create __main__ module");
685 d = PyModule_GetDict(m);
686 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
687 PyObject *bimod = PyImport_ImportModule("__builtin__");
688 if (bimod == NULL ||
689 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
690 Py_FatalError("can't add __builtins__ to __main__");
691 Py_DECREF(bimod);
695 /* Import the site module (not into __main__ though) */
697 static void
698 initsite(void)
700 PyObject *m, *f;
701 m = PyImport_ImportModule("site");
702 if (m == NULL) {
703 f = PySys_GetObject("stderr");
704 if (Py_VerboseFlag) {
705 PyFile_WriteString(
706 "'import site' failed; traceback:\n", f);
707 PyErr_Print();
709 else {
710 PyFile_WriteString(
711 "'import site' failed; use -v for traceback\n", f);
712 PyErr_Clear();
715 else {
716 Py_DECREF(m);
720 /* Parse input from a file and execute it */
723 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
724 PyCompilerFlags *flags)
726 if (filename == NULL)
727 filename = "???";
728 if (Py_FdIsInteractive(fp, filename)) {
729 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
730 if (closeit)
731 fclose(fp);
732 return err;
734 else
735 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
739 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
741 PyObject *v;
742 int ret;
743 PyCompilerFlags local_flags;
745 if (flags == NULL) {
746 flags = &local_flags;
747 local_flags.cf_flags = 0;
749 v = PySys_GetObject("ps1");
750 if (v == NULL) {
751 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
752 Py_XDECREF(v);
754 v = PySys_GetObject("ps2");
755 if (v == NULL) {
756 PySys_SetObject("ps2", v = PyString_FromString("... "));
757 Py_XDECREF(v);
759 for (;;) {
760 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
761 PRINT_TOTAL_REFS();
762 if (ret == E_EOF)
763 return 0;
765 if (ret == E_NOMEM)
766 return -1;
771 #if 0
772 /* compute parser flags based on compiler flags */
773 #define PARSER_FLAGS(flags) \
774 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
775 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
776 #endif
777 #if 1
778 /* Keep an example of flags with future keyword support. */
779 #define PARSER_FLAGS(flags) \
780 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
781 PyPARSE_DONT_IMPLY_DEDENT : 0) \
782 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
783 PyPARSE_PRINT_IS_FUNCTION : 0) \
784 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
785 PyPARSE_UNICODE_LITERALS : 0) \
786 ) : 0)
787 #endif
790 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
792 PyObject *m, *d, *v, *w;
793 mod_ty mod;
794 PyArena *arena;
795 char *ps1 = "", *ps2 = "";
796 int errcode = 0;
798 v = PySys_GetObject("ps1");
799 if (v != NULL) {
800 v = PyObject_Str(v);
801 if (v == NULL)
802 PyErr_Clear();
803 else if (PyString_Check(v))
804 ps1 = PyString_AsString(v);
806 w = PySys_GetObject("ps2");
807 if (w != NULL) {
808 w = PyObject_Str(w);
809 if (w == NULL)
810 PyErr_Clear();
811 else if (PyString_Check(w))
812 ps2 = PyString_AsString(w);
814 arena = PyArena_New();
815 if (arena == NULL) {
816 Py_XDECREF(v);
817 Py_XDECREF(w);
818 return -1;
820 mod = PyParser_ASTFromFile(fp, filename,
821 Py_single_input, ps1, ps2,
822 flags, &errcode, arena);
823 Py_XDECREF(v);
824 Py_XDECREF(w);
825 if (mod == NULL) {
826 PyArena_Free(arena);
827 if (errcode == E_EOF) {
828 PyErr_Clear();
829 return E_EOF;
831 PyErr_Print();
832 return -1;
834 m = PyImport_AddModule("__main__");
835 if (m == NULL) {
836 PyArena_Free(arena);
837 return -1;
839 d = PyModule_GetDict(m);
840 v = run_mod(mod, filename, d, d, flags, arena);
841 PyArena_Free(arena);
842 if (v == NULL) {
843 PyErr_Print();
844 return -1;
846 Py_DECREF(v);
847 if (Py_FlushLine())
848 PyErr_Clear();
849 return 0;
852 /* Check whether a file maybe a pyc file: Look at the extension,
853 the file type, and, if we may close it, at the first few bytes. */
855 static int
856 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
858 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
859 return 1;
861 /* Only look into the file if we are allowed to close it, since
862 it then should also be seekable. */
863 if (closeit) {
864 /* Read only two bytes of the magic. If the file was opened in
865 text mode, the bytes 3 and 4 of the magic (\r\n) might not
866 be read as they are on disk. */
867 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
868 unsigned char buf[2];
869 /* Mess: In case of -x, the stream is NOT at its start now,
870 and ungetc() was used to push back the first newline,
871 which makes the current stream position formally undefined,
872 and a x-platform nightmare.
873 Unfortunately, we have no direct way to know whether -x
874 was specified. So we use a terrible hack: if the current
875 stream position is not 0, we assume -x was specified, and
876 give up. Bug 132850 on SourceForge spells out the
877 hopelessness of trying anything else (fseek and ftell
878 don't work predictably x-platform for text-mode files).
880 int ispyc = 0;
881 if (ftell(fp) == 0) {
882 if (fread(buf, 1, 2, fp) == 2 &&
883 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
884 ispyc = 1;
885 rewind(fp);
887 return ispyc;
889 return 0;
893 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
894 PyCompilerFlags *flags)
896 PyObject *m, *d, *v;
897 const char *ext;
898 int set_file_name = 0, ret;
900 m = PyImport_AddModule("__main__");
901 if (m == NULL)
902 return -1;
903 d = PyModule_GetDict(m);
904 if (PyDict_GetItemString(d, "__file__") == NULL) {
905 PyObject *f = PyString_FromString(filename);
906 if (f == NULL)
907 return -1;
908 if (PyDict_SetItemString(d, "__file__", f) < 0) {
909 Py_DECREF(f);
910 return -1;
912 set_file_name = 1;
913 Py_DECREF(f);
915 ext = filename + strlen(filename) - 4;
916 if (maybe_pyc_file(fp, filename, ext, closeit)) {
917 /* Try to run a pyc file. First, re-open in binary */
918 if (closeit)
919 fclose(fp);
920 if ((fp = fopen(filename, "rb")) == NULL) {
921 fprintf(stderr, "python: Can't reopen .pyc file\n");
922 ret = -1;
923 goto done;
925 /* Turn on optimization if a .pyo file is given */
926 if (strcmp(ext, ".pyo") == 0)
927 Py_OptimizeFlag = 1;
928 v = run_pyc_file(fp, filename, d, d, flags);
929 } else {
930 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
931 closeit, flags);
933 if (v == NULL) {
934 PyErr_Print();
935 ret = -1;
936 goto done;
938 Py_DECREF(v);
939 if (Py_FlushLine())
940 PyErr_Clear();
941 ret = 0;
942 done:
943 if (set_file_name && PyDict_DelItemString(d, "__file__"))
944 PyErr_Clear();
945 return ret;
949 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
951 PyObject *m, *d, *v;
952 m = PyImport_AddModule("__main__");
953 if (m == NULL)
954 return -1;
955 d = PyModule_GetDict(m);
956 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
957 if (v == NULL) {
958 PyErr_Print();
959 return -1;
961 Py_DECREF(v);
962 if (Py_FlushLine())
963 PyErr_Clear();
964 return 0;
967 static int
968 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
969 int *lineno, int *offset, const char **text)
971 long hold;
972 PyObject *v;
974 /* old style errors */
975 if (PyTuple_Check(err))
976 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
977 lineno, offset, text);
979 /* new style errors. `err' is an instance */
981 if (! (v = PyObject_GetAttrString(err, "msg")))
982 goto finally;
983 *message = v;
985 if (!(v = PyObject_GetAttrString(err, "filename")))
986 goto finally;
987 if (v == Py_None)
988 *filename = NULL;
989 else if (! (*filename = PyString_AsString(v)))
990 goto finally;
992 Py_DECREF(v);
993 if (!(v = PyObject_GetAttrString(err, "lineno")))
994 goto finally;
995 hold = PyInt_AsLong(v);
996 Py_DECREF(v);
997 v = NULL;
998 if (hold < 0 && PyErr_Occurred())
999 goto finally;
1000 *lineno = (int)hold;
1002 if (!(v = PyObject_GetAttrString(err, "offset")))
1003 goto finally;
1004 if (v == Py_None) {
1005 *offset = -1;
1006 Py_DECREF(v);
1007 v = NULL;
1008 } else {
1009 hold = PyInt_AsLong(v);
1010 Py_DECREF(v);
1011 v = NULL;
1012 if (hold < 0 && PyErr_Occurred())
1013 goto finally;
1014 *offset = (int)hold;
1017 if (!(v = PyObject_GetAttrString(err, "text")))
1018 goto finally;
1019 if (v == Py_None)
1020 *text = NULL;
1021 else if (! (*text = PyString_AsString(v)))
1022 goto finally;
1023 Py_DECREF(v);
1024 return 1;
1026 finally:
1027 Py_XDECREF(v);
1028 return 0;
1031 void
1032 PyErr_Print(void)
1034 PyErr_PrintEx(1);
1037 static void
1038 print_error_text(PyObject *f, int offset, const char *text)
1040 char *nl;
1041 if (offset >= 0) {
1042 if (offset > 0 && offset == (int)strlen(text))
1043 offset--;
1044 for (;;) {
1045 nl = strchr(text, '\n');
1046 if (nl == NULL || nl-text >= offset)
1047 break;
1048 offset -= (int)(nl+1-text);
1049 text = nl+1;
1051 while (*text == ' ' || *text == '\t') {
1052 text++;
1053 offset--;
1056 PyFile_WriteString(" ", f);
1057 PyFile_WriteString(text, f);
1058 if (*text == '\0' || text[strlen(text)-1] != '\n')
1059 PyFile_WriteString("\n", f);
1060 if (offset == -1)
1061 return;
1062 PyFile_WriteString(" ", f);
1063 offset--;
1064 while (offset > 0) {
1065 PyFile_WriteString(" ", f);
1066 offset--;
1068 PyFile_WriteString("^\n", f);
1071 static void
1072 handle_system_exit(void)
1074 PyObject *exception, *value, *tb;
1075 int exitcode = 0;
1077 if (Py_InspectFlag)
1078 /* Don't exit if -i flag was given. This flag is set to 0
1079 * when entering interactive mode for inspecting. */
1080 return;
1082 PyErr_Fetch(&exception, &value, &tb);
1083 if (Py_FlushLine())
1084 PyErr_Clear();
1085 fflush(stdout);
1086 if (value == NULL || value == Py_None)
1087 goto done;
1088 if (PyExceptionInstance_Check(value)) {
1089 /* The error code should be in the `code' attribute. */
1090 PyObject *code = PyObject_GetAttrString(value, "code");
1091 if (code) {
1092 Py_DECREF(value);
1093 value = code;
1094 if (value == Py_None)
1095 goto done;
1097 /* If we failed to dig out the 'code' attribute,
1098 just let the else clause below print the error. */
1100 if (PyInt_Check(value))
1101 exitcode = (int)PyInt_AsLong(value);
1102 else {
1103 PyObject_Print(value, stderr, Py_PRINT_RAW);
1104 PySys_WriteStderr("\n");
1105 exitcode = 1;
1107 done:
1108 /* Restore and clear the exception info, in order to properly decref
1109 * the exception, value, and traceback. If we just exit instead,
1110 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1111 * some finalizers from running.
1113 PyErr_Restore(exception, value, tb);
1114 PyErr_Clear();
1115 Py_Exit(exitcode);
1116 /* NOTREACHED */
1119 void
1120 PyErr_PrintEx(int set_sys_last_vars)
1122 PyObject *exception, *v, *tb, *hook;
1124 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1125 handle_system_exit();
1127 PyErr_Fetch(&exception, &v, &tb);
1128 if (exception == NULL)
1129 return;
1130 PyErr_NormalizeException(&exception, &v, &tb);
1131 if (exception == NULL)
1132 return;
1133 /* Now we know v != NULL too */
1134 if (set_sys_last_vars) {
1135 PySys_SetObject("last_type", exception);
1136 PySys_SetObject("last_value", v);
1137 PySys_SetObject("last_traceback", tb);
1139 hook = PySys_GetObject("excepthook");
1140 if (hook) {
1141 PyObject *args = PyTuple_Pack(3,
1142 exception, v, tb ? tb : Py_None);
1143 PyObject *result = PyEval_CallObject(hook, args);
1144 if (result == NULL) {
1145 PyObject *exception2, *v2, *tb2;
1146 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1147 handle_system_exit();
1149 PyErr_Fetch(&exception2, &v2, &tb2);
1150 PyErr_NormalizeException(&exception2, &v2, &tb2);
1151 /* It should not be possible for exception2 or v2
1152 to be NULL. However PyErr_Display() can't
1153 tolerate NULLs, so just be safe. */
1154 if (exception2 == NULL) {
1155 exception2 = Py_None;
1156 Py_INCREF(exception2);
1158 if (v2 == NULL) {
1159 v2 = Py_None;
1160 Py_INCREF(v2);
1162 if (Py_FlushLine())
1163 PyErr_Clear();
1164 fflush(stdout);
1165 PySys_WriteStderr("Error in sys.excepthook:\n");
1166 PyErr_Display(exception2, v2, tb2);
1167 PySys_WriteStderr("\nOriginal exception was:\n");
1168 PyErr_Display(exception, v, tb);
1169 Py_DECREF(exception2);
1170 Py_DECREF(v2);
1171 Py_XDECREF(tb2);
1173 Py_XDECREF(result);
1174 Py_XDECREF(args);
1175 } else {
1176 PySys_WriteStderr("sys.excepthook is missing\n");
1177 PyErr_Display(exception, v, tb);
1179 Py_XDECREF(exception);
1180 Py_XDECREF(v);
1181 Py_XDECREF(tb);
1184 void
1185 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1187 int err = 0;
1188 PyObject *f = PySys_GetObject("stderr");
1189 Py_INCREF(value);
1190 if (f == NULL)
1191 fprintf(stderr, "lost sys.stderr\n");
1192 else {
1193 if (Py_FlushLine())
1194 PyErr_Clear();
1195 fflush(stdout);
1196 if (tb && tb != Py_None)
1197 err = PyTraceBack_Print(tb, f);
1198 if (err == 0 &&
1199 PyObject_HasAttrString(value, "print_file_and_line"))
1201 PyObject *message;
1202 const char *filename, *text;
1203 int lineno, offset;
1204 if (!parse_syntax_error(value, &message, &filename,
1205 &lineno, &offset, &text))
1206 PyErr_Clear();
1207 else {
1208 char buf[10];
1209 PyFile_WriteString(" File \"", f);
1210 if (filename == NULL)
1211 PyFile_WriteString("<string>", f);
1212 else
1213 PyFile_WriteString(filename, f);
1214 PyFile_WriteString("\", line ", f);
1215 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1216 PyFile_WriteString(buf, f);
1217 PyFile_WriteString("\n", f);
1218 if (text != NULL)
1219 print_error_text(f, offset, text);
1220 Py_DECREF(value);
1221 value = message;
1222 /* Can't be bothered to check all those
1223 PyFile_WriteString() calls */
1224 if (PyErr_Occurred())
1225 err = -1;
1228 if (err) {
1229 /* Don't do anything else */
1231 else if (PyExceptionClass_Check(exception)) {
1232 PyObject* moduleName;
1233 char* className = PyExceptionClass_Name(exception);
1234 if (className != NULL) {
1235 char *dot = strrchr(className, '.');
1236 if (dot != NULL)
1237 className = dot+1;
1240 moduleName = PyObject_GetAttrString(exception, "__module__");
1241 if (moduleName == NULL)
1242 err = PyFile_WriteString("<unknown>", f);
1243 else {
1244 char* modstr = PyString_AsString(moduleName);
1245 if (modstr && strcmp(modstr, "exceptions"))
1247 err = PyFile_WriteString(modstr, f);
1248 err += PyFile_WriteString(".", f);
1250 Py_DECREF(moduleName);
1252 if (err == 0) {
1253 if (className == NULL)
1254 err = PyFile_WriteString("<unknown>", f);
1255 else
1256 err = PyFile_WriteString(className, f);
1259 else
1260 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1261 if (err == 0 && (value != Py_None)) {
1262 PyObject *s = PyObject_Str(value);
1263 /* only print colon if the str() of the
1264 object is not the empty string
1266 if (s == NULL)
1267 err = -1;
1268 else if (!PyString_Check(s) ||
1269 PyString_GET_SIZE(s) != 0)
1270 err = PyFile_WriteString(": ", f);
1271 if (err == 0)
1272 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1273 Py_XDECREF(s);
1275 /* try to write a newline in any case */
1276 err += PyFile_WriteString("\n", f);
1278 Py_DECREF(value);
1279 /* If an error happened here, don't show it.
1280 XXX This is wrong, but too many callers rely on this behavior. */
1281 if (err != 0)
1282 PyErr_Clear();
1285 PyObject *
1286 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1287 PyObject *locals, PyCompilerFlags *flags)
1289 PyObject *ret = NULL;
1290 mod_ty mod;
1291 PyArena *arena = PyArena_New();
1292 if (arena == NULL)
1293 return NULL;
1295 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1296 if (mod != NULL)
1297 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1298 PyArena_Free(arena);
1299 return ret;
1302 PyObject *
1303 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1304 PyObject *locals, int closeit, PyCompilerFlags *flags)
1306 PyObject *ret;
1307 mod_ty mod;
1308 PyArena *arena = PyArena_New();
1309 if (arena == NULL)
1310 return NULL;
1312 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1313 flags, NULL, arena);
1314 if (closeit)
1315 fclose(fp);
1316 if (mod == NULL) {
1317 PyArena_Free(arena);
1318 return NULL;
1320 ret = run_mod(mod, filename, globals, locals, flags, arena);
1321 PyArena_Free(arena);
1322 return ret;
1325 static PyObject *
1326 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1327 PyCompilerFlags *flags, PyArena *arena)
1329 PyCodeObject *co;
1330 PyObject *v;
1331 co = PyAST_Compile(mod, filename, flags, arena);
1332 if (co == NULL)
1333 return NULL;
1334 v = PyEval_EvalCode(co, globals, locals);
1335 Py_DECREF(co);
1336 return v;
1339 static PyObject *
1340 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1341 PyObject *locals, PyCompilerFlags *flags)
1343 PyCodeObject *co;
1344 PyObject *v;
1345 long magic;
1346 long PyImport_GetMagicNumber(void);
1348 magic = PyMarshal_ReadLongFromFile(fp);
1349 if (magic != PyImport_GetMagicNumber()) {
1350 PyErr_SetString(PyExc_RuntimeError,
1351 "Bad magic number in .pyc file");
1352 return NULL;
1354 (void) PyMarshal_ReadLongFromFile(fp);
1355 v = PyMarshal_ReadLastObjectFromFile(fp);
1356 fclose(fp);
1357 if (v == NULL || !PyCode_Check(v)) {
1358 Py_XDECREF(v);
1359 PyErr_SetString(PyExc_RuntimeError,
1360 "Bad code object in .pyc file");
1361 return NULL;
1363 co = (PyCodeObject *)v;
1364 v = PyEval_EvalCode(co, globals, locals);
1365 if (v && flags)
1366 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1367 Py_DECREF(co);
1368 return v;
1371 PyObject *
1372 Py_CompileStringFlags(const char *str, const char *filename, int start,
1373 PyCompilerFlags *flags)
1375 PyCodeObject *co;
1376 mod_ty mod;
1377 PyArena *arena = PyArena_New();
1378 if (arena == NULL)
1379 return NULL;
1381 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1382 if (mod == NULL) {
1383 PyArena_Free(arena);
1384 return NULL;
1386 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1387 PyObject *result = PyAST_mod2obj(mod);
1388 PyArena_Free(arena);
1389 return result;
1391 co = PyAST_Compile(mod, filename, flags, arena);
1392 PyArena_Free(arena);
1393 return (PyObject *)co;
1396 struct symtable *
1397 Py_SymtableString(const char *str, const char *filename, int start)
1399 struct symtable *st;
1400 mod_ty mod;
1401 PyCompilerFlags flags;
1402 PyArena *arena = PyArena_New();
1403 if (arena == NULL)
1404 return NULL;
1406 flags.cf_flags = 0;
1408 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1409 if (mod == NULL) {
1410 PyArena_Free(arena);
1411 return NULL;
1413 st = PySymtable_Build(mod, filename, 0);
1414 PyArena_Free(arena);
1415 return st;
1418 /* Preferred access to parser is through AST. */
1419 mod_ty
1420 PyParser_ASTFromString(const char *s, const char *filename, int start,
1421 PyCompilerFlags *flags, PyArena *arena)
1423 mod_ty mod;
1424 PyCompilerFlags localflags;
1425 perrdetail err;
1426 int iflags = PARSER_FLAGS(flags);
1428 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1429 &_PyParser_Grammar, start, &err,
1430 &iflags);
1431 if (flags == NULL) {
1432 localflags.cf_flags = 0;
1433 flags = &localflags;
1435 if (n) {
1436 flags->cf_flags |= iflags & PyCF_MASK;
1437 mod = PyAST_FromNode(n, flags, filename, arena);
1438 PyNode_Free(n);
1439 return mod;
1441 else {
1442 err_input(&err);
1443 return NULL;
1447 mod_ty
1448 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1449 char *ps2, PyCompilerFlags *flags, int *errcode,
1450 PyArena *arena)
1452 mod_ty mod;
1453 PyCompilerFlags localflags;
1454 perrdetail err;
1455 int iflags = PARSER_FLAGS(flags);
1457 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1458 start, ps1, ps2, &err, &iflags);
1459 if (flags == NULL) {
1460 localflags.cf_flags = 0;
1461 flags = &localflags;
1463 if (n) {
1464 flags->cf_flags |= iflags & PyCF_MASK;
1465 mod = PyAST_FromNode(n, flags, filename, arena);
1466 PyNode_Free(n);
1467 return mod;
1469 else {
1470 err_input(&err);
1471 if (errcode)
1472 *errcode = err.error;
1473 return NULL;
1477 /* Simplified interface to parsefile -- return node or set exception */
1479 node *
1480 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1482 perrdetail err;
1483 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1484 start, NULL, NULL, &err, flags);
1485 if (n == NULL)
1486 err_input(&err);
1488 return n;
1491 /* Simplified interface to parsestring -- return node or set exception */
1493 node *
1494 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1496 perrdetail err;
1497 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1498 start, &err, flags);
1499 if (n == NULL)
1500 err_input(&err);
1501 return n;
1504 node *
1505 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1506 int start, int flags)
1508 perrdetail err;
1509 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1510 &_PyParser_Grammar, start, &err, flags);
1511 if (n == NULL)
1512 err_input(&err);
1513 return n;
1516 node *
1517 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1519 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1522 /* May want to move a more generalized form of this to parsetok.c or
1523 even parser modules. */
1525 void
1526 PyParser_SetError(perrdetail *err)
1528 err_input(err);
1531 /* Set the error appropriate to the given input error code (see errcode.h) */
1533 static void
1534 err_input(perrdetail *err)
1536 PyObject *v, *w, *errtype;
1537 PyObject* u = NULL;
1538 char *msg = NULL;
1539 errtype = PyExc_SyntaxError;
1540 switch (err->error) {
1541 case E_SYNTAX:
1542 errtype = PyExc_IndentationError;
1543 if (err->expected == INDENT)
1544 msg = "expected an indented block";
1545 else if (err->token == INDENT)
1546 msg = "unexpected indent";
1547 else if (err->token == DEDENT)
1548 msg = "unexpected unindent";
1549 else {
1550 errtype = PyExc_SyntaxError;
1551 msg = "invalid syntax";
1553 break;
1554 case E_TOKEN:
1555 msg = "invalid token";
1556 break;
1557 case E_EOFS:
1558 msg = "EOF while scanning triple-quoted string literal";
1559 break;
1560 case E_EOLS:
1561 msg = "EOL while scanning string literal";
1562 break;
1563 case E_INTR:
1564 if (!PyErr_Occurred())
1565 PyErr_SetNone(PyExc_KeyboardInterrupt);
1566 goto cleanup;
1567 case E_NOMEM:
1568 PyErr_NoMemory();
1569 goto cleanup;
1570 case E_EOF:
1571 msg = "unexpected EOF while parsing";
1572 break;
1573 case E_TABSPACE:
1574 errtype = PyExc_TabError;
1575 msg = "inconsistent use of tabs and spaces in indentation";
1576 break;
1577 case E_OVERFLOW:
1578 msg = "expression too long";
1579 break;
1580 case E_DEDENT:
1581 errtype = PyExc_IndentationError;
1582 msg = "unindent does not match any outer indentation level";
1583 break;
1584 case E_TOODEEP:
1585 errtype = PyExc_IndentationError;
1586 msg = "too many levels of indentation";
1587 break;
1588 case E_DECODE: {
1589 PyObject *type, *value, *tb;
1590 PyErr_Fetch(&type, &value, &tb);
1591 if (value != NULL) {
1592 u = PyObject_Str(value);
1593 if (u != NULL) {
1594 msg = PyString_AsString(u);
1597 if (msg == NULL)
1598 msg = "unknown decode error";
1599 Py_XDECREF(type);
1600 Py_XDECREF(value);
1601 Py_XDECREF(tb);
1602 break;
1604 case E_LINECONT:
1605 msg = "unexpected character after line continuation character";
1606 break;
1607 default:
1608 fprintf(stderr, "error=%d\n", err->error);
1609 msg = "unknown parsing error";
1610 break;
1612 v = Py_BuildValue("(ziiz)", err->filename,
1613 err->lineno, err->offset, err->text);
1614 w = NULL;
1615 if (v != NULL)
1616 w = Py_BuildValue("(sO)", msg, v);
1617 Py_XDECREF(u);
1618 Py_XDECREF(v);
1619 PyErr_SetObject(errtype, w);
1620 Py_XDECREF(w);
1621 cleanup:
1622 if (err->text != NULL) {
1623 PyObject_FREE(err->text);
1624 err->text = NULL;
1628 /* Print fatal error message and abort */
1630 void
1631 Py_FatalError(const char *msg)
1633 fprintf(stderr, "Fatal Python error: %s\n", msg);
1634 #ifdef MS_WINDOWS
1636 size_t len = strlen(msg);
1637 WCHAR* buffer;
1638 size_t i;
1640 /* Convert the message to wchar_t. This uses a simple one-to-one
1641 conversion, assuming that the this error message actually uses ASCII
1642 only. If this ceases to be true, we will have to convert. */
1643 buffer = alloca( (len+1) * (sizeof *buffer));
1644 for( i=0; i<=len; ++i)
1645 buffer[i] = msg[i];
1646 OutputDebugStringW(L"Fatal Python error: ");
1647 OutputDebugStringW(buffer);
1648 OutputDebugStringW(L"\n");
1650 #ifdef _DEBUG
1651 DebugBreak();
1652 #endif
1653 #endif /* MS_WINDOWS */
1654 abort();
1657 /* Clean up and exit */
1659 #ifdef WITH_THREAD
1660 #include "pythread.h"
1661 #endif
1663 #define NEXITFUNCS 32
1664 static void (*exitfuncs[NEXITFUNCS])(void);
1665 static int nexitfuncs = 0;
1667 int Py_AtExit(void (*func)(void))
1669 if (nexitfuncs >= NEXITFUNCS)
1670 return -1;
1671 exitfuncs[nexitfuncs++] = func;
1672 return 0;
1675 static void
1676 call_sys_exitfunc(void)
1678 PyObject *exitfunc = PySys_GetObject("exitfunc");
1680 if (exitfunc) {
1681 PyObject *res;
1682 Py_INCREF(exitfunc);
1683 PySys_SetObject("exitfunc", (PyObject *)NULL);
1684 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1685 if (res == NULL) {
1686 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1687 PySys_WriteStderr("Error in sys.exitfunc:\n");
1689 PyErr_Print();
1691 Py_DECREF(exitfunc);
1694 if (Py_FlushLine())
1695 PyErr_Clear();
1698 static void
1699 call_ll_exitfuncs(void)
1701 while (nexitfuncs > 0)
1702 (*exitfuncs[--nexitfuncs])();
1704 fflush(stdout);
1705 fflush(stderr);
1708 void
1709 Py_Exit(int sts)
1711 Py_Finalize();
1713 exit(sts);
1716 static void
1717 initsigs(void)
1719 #ifdef SIGPIPE
1720 PyOS_setsig(SIGPIPE, SIG_IGN);
1721 #endif
1722 #ifdef SIGXFZ
1723 PyOS_setsig(SIGXFZ, SIG_IGN);
1724 #endif
1725 #ifdef SIGXFSZ
1726 PyOS_setsig(SIGXFSZ, SIG_IGN);
1727 #endif
1728 PyOS_InitInterrupts(); /* May imply initsignal() */
1733 * The file descriptor fd is considered ``interactive'' if either
1734 * a) isatty(fd) is TRUE, or
1735 * b) the -i flag was given, and the filename associated with
1736 * the descriptor is NULL or "<stdin>" or "???".
1739 Py_FdIsInteractive(FILE *fp, const char *filename)
1741 if (isatty((int)fileno(fp)))
1742 return 1;
1743 if (!Py_InteractiveFlag)
1744 return 0;
1745 return (filename == NULL) ||
1746 (strcmp(filename, "<stdin>") == 0) ||
1747 (strcmp(filename, "???") == 0);
1751 #if defined(USE_STACKCHECK)
1752 #if defined(WIN32) && defined(_MSC_VER)
1754 /* Stack checking for Microsoft C */
1756 #include <malloc.h>
1757 #include <excpt.h>
1760 * Return non-zero when we run out of memory on the stack; zero otherwise.
1763 PyOS_CheckStack(void)
1765 __try {
1766 /* alloca throws a stack overflow exception if there's
1767 not enough space left on the stack */
1768 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1769 return 0;
1770 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1771 EXCEPTION_EXECUTE_HANDLER :
1772 EXCEPTION_CONTINUE_SEARCH) {
1773 int errcode = _resetstkoflw();
1774 if (errcode == 0)
1776 Py_FatalError("Could not reset the stack!");
1779 return 1;
1782 #endif /* WIN32 && _MSC_VER */
1784 /* Alternate implementations can be added here... */
1786 #endif /* USE_STACKCHECK */
1789 /* Wrappers around sigaction() or signal(). */
1791 PyOS_sighandler_t
1792 PyOS_getsig(int sig)
1794 #ifdef HAVE_SIGACTION
1795 struct sigaction context;
1796 if (sigaction(sig, NULL, &context) == -1)
1797 return SIG_ERR;
1798 return context.sa_handler;
1799 #else
1800 PyOS_sighandler_t handler;
1801 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1802 #if defined(_MSC_VER) && _MSC_VER >= 1400
1803 switch (sig) {
1804 /* Only these signals are valid */
1805 case SIGINT:
1806 case SIGILL:
1807 case SIGFPE:
1808 case SIGSEGV:
1809 case SIGTERM:
1810 case SIGBREAK:
1811 case SIGABRT:
1812 break;
1813 /* Don't call signal() with other values or it will assert */
1814 default:
1815 return SIG_ERR;
1817 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1818 handler = signal(sig, SIG_IGN);
1819 if (handler != SIG_ERR)
1820 signal(sig, handler);
1821 return handler;
1822 #endif
1825 PyOS_sighandler_t
1826 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1828 #ifdef HAVE_SIGACTION
1829 struct sigaction context, ocontext;
1830 context.sa_handler = handler;
1831 sigemptyset(&context.sa_mask);
1832 context.sa_flags = 0;
1833 if (sigaction(sig, &context, &ocontext) == -1)
1834 return SIG_ERR;
1835 return ocontext.sa_handler;
1836 #else
1837 PyOS_sighandler_t oldhandler;
1838 oldhandler = signal(sig, handler);
1839 #ifdef HAVE_SIGINTERRUPT
1840 siginterrupt(sig, 1);
1841 #endif
1842 return oldhandler;
1843 #endif
1846 /* Deprecated C API functions still provided for binary compatiblity */
1848 #undef PyParser_SimpleParseFile
1849 PyAPI_FUNC(node *)
1850 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1852 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1855 #undef PyParser_SimpleParseString
1856 PyAPI_FUNC(node *)
1857 PyParser_SimpleParseString(const char *str, int start)
1859 return PyParser_SimpleParseStringFlags(str, start, 0);
1862 #undef PyRun_AnyFile
1863 PyAPI_FUNC(int)
1864 PyRun_AnyFile(FILE *fp, const char *name)
1866 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1869 #undef PyRun_AnyFileEx
1870 PyAPI_FUNC(int)
1871 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1873 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1876 #undef PyRun_AnyFileFlags
1877 PyAPI_FUNC(int)
1878 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1880 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1883 #undef PyRun_File
1884 PyAPI_FUNC(PyObject *)
1885 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1887 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1890 #undef PyRun_FileEx
1891 PyAPI_FUNC(PyObject *)
1892 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1894 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1897 #undef PyRun_FileFlags
1898 PyAPI_FUNC(PyObject *)
1899 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1900 PyCompilerFlags *flags)
1902 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1905 #undef PyRun_SimpleFile
1906 PyAPI_FUNC(int)
1907 PyRun_SimpleFile(FILE *f, const char *p)
1909 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1912 #undef PyRun_SimpleFileEx
1913 PyAPI_FUNC(int)
1914 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1916 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1920 #undef PyRun_String
1921 PyAPI_FUNC(PyObject *)
1922 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1924 return PyRun_StringFlags(str, s, g, l, NULL);
1927 #undef PyRun_SimpleString
1928 PyAPI_FUNC(int)
1929 PyRun_SimpleString(const char *s)
1931 return PyRun_SimpleStringFlags(s, NULL);
1934 #undef Py_CompileString
1935 PyAPI_FUNC(PyObject *)
1936 Py_CompileString(const char *str, const char *p, int s)
1938 return Py_CompileStringFlags(str, p, s, NULL);
1941 #undef PyRun_InteractiveOne
1942 PyAPI_FUNC(int)
1943 PyRun_InteractiveOne(FILE *f, const char *p)
1945 return PyRun_InteractiveOneFlags(f, p, NULL);
1948 #undef PyRun_InteractiveLoop
1949 PyAPI_FUNC(int)
1950 PyRun_InteractiveLoop(FILE *f, const char *p)
1952 return PyRun_InteractiveLoopFlags(f, p, NULL);
1955 #ifdef __cplusplus
1957 #endif