Expand test coverage for struct.pack with native integer packing;
[python.git] / Python / pythonrun.c
blob8fc0ca199f2af2e31ce10b0a8d21a31347521c3f
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 (!_PyLong_Init())
186 Py_FatalError("Py_Initialize: can't init longs");
188 if (!PyByteArray_Init())
189 Py_FatalError("Py_Initialize: can't init bytearray");
191 _PyFloat_Init();
193 interp->modules = PyDict_New();
194 if (interp->modules == NULL)
195 Py_FatalError("Py_Initialize: can't make modules dictionary");
196 interp->modules_reloading = PyDict_New();
197 if (interp->modules_reloading == NULL)
198 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
200 #ifdef Py_USING_UNICODE
201 /* Init Unicode implementation; relies on the codec registry */
202 _PyUnicode_Init();
203 #endif
205 bimod = _PyBuiltin_Init();
206 if (bimod == NULL)
207 Py_FatalError("Py_Initialize: can't initialize __builtin__");
208 interp->builtins = PyModule_GetDict(bimod);
209 if (interp->builtins == NULL)
210 Py_FatalError("Py_Initialize: can't initialize builtins dict");
211 Py_INCREF(interp->builtins);
213 sysmod = _PySys_Init();
214 if (sysmod == NULL)
215 Py_FatalError("Py_Initialize: can't initialize sys");
216 interp->sysdict = PyModule_GetDict(sysmod);
217 if (interp->sysdict == NULL)
218 Py_FatalError("Py_Initialize: can't initialize sys dict");
219 Py_INCREF(interp->sysdict);
220 _PyImport_FixupExtension("sys", "sys");
221 PySys_SetPath(Py_GetPath());
222 PyDict_SetItemString(interp->sysdict, "modules",
223 interp->modules);
225 _PyImport_Init();
227 /* initialize builtin exceptions */
228 _PyExc_Init();
229 _PyImport_FixupExtension("exceptions", "exceptions");
231 /* phase 2 of builtins */
232 _PyImport_FixupExtension("__builtin__", "__builtin__");
234 _PyImportHooks_Init();
236 if (install_sigs)
237 initsigs(); /* Signal handling stuff, including initintr() */
239 /* Initialize warnings. */
240 _PyWarnings_Init();
241 if (PySys_HasWarnOptions()) {
242 PyObject *warnings_module = PyImport_ImportModule("warnings");
243 if (!warnings_module)
244 PyErr_Clear();
245 Py_XDECREF(warnings_module);
248 initmain(); /* Module __main__ */
249 if (!Py_NoSiteFlag)
250 initsite(); /* Module site */
252 /* auto-thread-state API, if available */
253 #ifdef WITH_THREAD
254 _PyGILState_Init(interp, tstate);
255 #endif /* WITH_THREAD */
257 if ((p = Py_GETENV("PYTHONIOENCODING")) && *p != '\0') {
258 p = icodeset = codeset = strdup(p);
259 free_codeset = 1;
260 errors = strchr(p, ':');
261 if (errors) {
262 *errors = '\0';
263 errors++;
265 overridden = 1;
268 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
269 /* On Unix, set the file system encoding according to the
270 user's preference, if the CODESET names a well-known
271 Python codec, and Py_FileSystemDefaultEncoding isn't
272 initialized by other means. Also set the encoding of
273 stdin and stdout if these are terminals, unless overridden. */
275 if (!overridden || !Py_FileSystemDefaultEncoding) {
276 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
277 setlocale(LC_CTYPE, "");
278 loc_codeset = nl_langinfo(CODESET);
279 if (loc_codeset && *loc_codeset) {
280 PyObject *enc = PyCodec_Encoder(loc_codeset);
281 if (enc) {
282 loc_codeset = strdup(loc_codeset);
283 Py_DECREF(enc);
284 } else {
285 loc_codeset = NULL;
286 PyErr_Clear();
288 } else
289 loc_codeset = NULL;
290 setlocale(LC_CTYPE, saved_locale);
291 free(saved_locale);
293 if (!overridden) {
294 codeset = icodeset = loc_codeset;
295 free_codeset = 1;
298 /* Initialize Py_FileSystemDefaultEncoding from
299 locale even if PYTHONIOENCODING is set. */
300 if (!Py_FileSystemDefaultEncoding) {
301 Py_FileSystemDefaultEncoding = loc_codeset;
302 if (!overridden)
303 free_codeset = 0;
306 #endif
308 #ifdef MS_WINDOWS
309 if (!overridden) {
310 icodeset = ibuf;
311 codeset = buf;
312 sprintf(ibuf, "cp%d", GetConsoleCP());
313 sprintf(buf, "cp%d", GetConsoleOutputCP());
315 #endif
317 if (codeset) {
318 sys_stream = PySys_GetObject("stdin");
319 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
320 if (!sys_isatty)
321 PyErr_Clear();
322 if ((overridden ||
323 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
324 PyFile_Check(sys_stream)) {
325 if (!PyFile_SetEncodingAndErrors(sys_stream, icodeset, errors))
326 Py_FatalError("Cannot set codeset of stdin");
328 Py_XDECREF(sys_isatty);
330 sys_stream = PySys_GetObject("stdout");
331 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
332 if (!sys_isatty)
333 PyErr_Clear();
334 if ((overridden ||
335 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
336 PyFile_Check(sys_stream)) {
337 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
338 Py_FatalError("Cannot set codeset of stdout");
340 Py_XDECREF(sys_isatty);
342 sys_stream = PySys_GetObject("stderr");
343 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
344 if (!sys_isatty)
345 PyErr_Clear();
346 if((overridden ||
347 (sys_isatty && PyObject_IsTrue(sys_isatty))) &&
348 PyFile_Check(sys_stream)) {
349 if (!PyFile_SetEncodingAndErrors(sys_stream, codeset, errors))
350 Py_FatalError("Cannot set codeset of stderr");
352 Py_XDECREF(sys_isatty);
354 if (free_codeset)
355 free(codeset);
359 void
360 Py_Initialize(void)
362 Py_InitializeEx(1);
366 #ifdef COUNT_ALLOCS
367 extern void dump_counts(FILE*);
368 #endif
370 /* Undo the effect of Py_Initialize().
372 Beware: if multiple interpreter and/or thread states exist, these
373 are not wiped out; only the current thread and interpreter state
374 are deleted. But since everything else is deleted, those other
375 interpreter and thread states should no longer be used.
377 (XXX We should do better, e.g. wipe out all interpreters and
378 threads.)
380 Locking: as above.
384 void
385 Py_Finalize(void)
387 PyInterpreterState *interp;
388 PyThreadState *tstate;
390 if (!initialized)
391 return;
393 /* The interpreter is still entirely intact at this point, and the
394 * exit funcs may be relying on that. In particular, if some thread
395 * or exit func is still waiting to do an import, the import machinery
396 * expects Py_IsInitialized() to return true. So don't say the
397 * interpreter is uninitialized until after the exit funcs have run.
398 * Note that Threading.py uses an exit func to do a join on all the
399 * threads created thru it, so this also protects pending imports in
400 * the threads created via Threading.
402 call_sys_exitfunc();
403 initialized = 0;
405 /* Get current thread state and interpreter pointer */
406 tstate = PyThreadState_GET();
407 interp = tstate->interp;
409 /* Disable signal handling */
410 PyOS_FiniInterrupts();
412 /* Clear type lookup cache */
413 PyType_ClearCache();
415 /* Collect garbage. This may call finalizers; it's nice to call these
416 * before all modules are destroyed.
417 * XXX If a __del__ or weakref callback is triggered here, and tries to
418 * XXX import a module, bad things can happen, because Python no
419 * XXX longer believes it's initialized.
420 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
421 * XXX is easy to provoke that way. I've also seen, e.g.,
422 * XXX Exception exceptions.ImportError: 'No module named sha'
423 * XXX in <function callback at 0x008F5718> ignored
424 * XXX but I'm unclear on exactly how that one happens. In any case,
425 * XXX I haven't seen a real-life report of either of these.
427 PyGC_Collect();
428 #ifdef COUNT_ALLOCS
429 /* With COUNT_ALLOCS, it helps to run GC multiple times:
430 each collection might release some types from the type
431 list, so they become garbage. */
432 while (PyGC_Collect() > 0)
433 /* nothing */;
434 #endif
436 /* Destroy all modules */
437 PyImport_Cleanup();
439 /* Collect final garbage. This disposes of cycles created by
440 * new-style class definitions, for example.
441 * XXX This is disabled because it caused too many problems. If
442 * XXX a __del__ or weakref callback triggers here, Python code has
443 * XXX a hard time running, because even the sys module has been
444 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
445 * XXX One symptom is a sequence of information-free messages
446 * XXX coming from threads (if a __del__ or callback is invoked,
447 * XXX other threads can execute too, and any exception they encounter
448 * XXX triggers a comedy of errors as subsystem after subsystem
449 * XXX fails to find what it *expects* to find in sys to help report
450 * XXX the exception and consequent unexpected failures). I've also
451 * XXX seen segfaults then, after adding print statements to the
452 * XXX Python code getting called.
454 #if 0
455 PyGC_Collect();
456 #endif
458 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
459 _PyImport_Fini();
461 /* Debugging stuff */
462 #ifdef COUNT_ALLOCS
463 dump_counts(stdout);
464 #endif
466 PRINT_TOTAL_REFS();
468 #ifdef Py_TRACE_REFS
469 /* Display all objects still alive -- this can invoke arbitrary
470 * __repr__ overrides, so requires a mostly-intact interpreter.
471 * Alas, a lot of stuff may still be alive now that will be cleaned
472 * up later.
474 if (Py_GETENV("PYTHONDUMPREFS"))
475 _Py_PrintReferences(stderr);
476 #endif /* Py_TRACE_REFS */
478 /* Clear interpreter state */
479 PyInterpreterState_Clear(interp);
481 /* Now we decref the exception classes. After this point nothing
482 can raise an exception. That's okay, because each Fini() method
483 below has been checked to make sure no exceptions are ever
484 raised.
487 _PyExc_Fini();
489 /* Cleanup auto-thread-state */
490 #ifdef WITH_THREAD
491 _PyGILState_Fini();
492 #endif /* WITH_THREAD */
494 /* Delete current thread */
495 PyThreadState_Swap(NULL);
496 PyInterpreterState_Delete(interp);
498 /* Sundry finalizers */
499 PyMethod_Fini();
500 PyFrame_Fini();
501 PyCFunction_Fini();
502 PyTuple_Fini();
503 PyList_Fini();
504 PySet_Fini();
505 PyString_Fini();
506 PyByteArray_Fini();
507 PyInt_Fini();
508 PyFloat_Fini();
509 PyDict_Fini();
511 #ifdef Py_USING_UNICODE
512 /* Cleanup Unicode implementation */
513 _PyUnicode_Fini();
514 #endif
516 /* XXX Still allocated:
517 - various static ad-hoc pointers to interned strings
518 - int and float free list blocks
519 - whatever various modules and libraries allocate
522 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
524 #ifdef Py_TRACE_REFS
525 /* Display addresses (& refcnts) of all objects still alive.
526 * An address can be used to find the repr of the object, printed
527 * above by _Py_PrintReferences.
529 if (Py_GETENV("PYTHONDUMPREFS"))
530 _Py_PrintReferenceAddresses(stderr);
531 #endif /* Py_TRACE_REFS */
532 #ifdef PYMALLOC_DEBUG
533 if (Py_GETENV("PYTHONMALLOCSTATS"))
534 _PyObject_DebugMallocStats();
535 #endif
537 call_ll_exitfuncs();
540 /* Create and initialize a new interpreter and thread, and return the
541 new thread. This requires that Py_Initialize() has been called
542 first.
544 Unsuccessful initialization yields a NULL pointer. Note that *no*
545 exception information is available even in this case -- the
546 exception information is held in the thread, and there is no
547 thread.
549 Locking: as above.
553 PyThreadState *
554 Py_NewInterpreter(void)
556 PyInterpreterState *interp;
557 PyThreadState *tstate, *save_tstate;
558 PyObject *bimod, *sysmod;
560 if (!initialized)
561 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
563 interp = PyInterpreterState_New();
564 if (interp == NULL)
565 return NULL;
567 tstate = PyThreadState_New(interp);
568 if (tstate == NULL) {
569 PyInterpreterState_Delete(interp);
570 return NULL;
573 save_tstate = PyThreadState_Swap(tstate);
575 /* XXX The following is lax in error checking */
577 interp->modules = PyDict_New();
578 interp->modules_reloading = PyDict_New();
580 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
581 if (bimod != NULL) {
582 interp->builtins = PyModule_GetDict(bimod);
583 if (interp->builtins == NULL)
584 goto handle_error;
585 Py_INCREF(interp->builtins);
587 sysmod = _PyImport_FindExtension("sys", "sys");
588 if (bimod != NULL && sysmod != NULL) {
589 interp->sysdict = PyModule_GetDict(sysmod);
590 if (interp->sysdict == NULL)
591 goto handle_error;
592 Py_INCREF(interp->sysdict);
593 PySys_SetPath(Py_GetPath());
594 PyDict_SetItemString(interp->sysdict, "modules",
595 interp->modules);
596 _PyImportHooks_Init();
597 initmain();
598 if (!Py_NoSiteFlag)
599 initsite();
602 if (!PyErr_Occurred())
603 return tstate;
605 handle_error:
606 /* Oops, it didn't work. Undo it all. */
608 PyErr_Print();
609 PyThreadState_Clear(tstate);
610 PyThreadState_Swap(save_tstate);
611 PyThreadState_Delete(tstate);
612 PyInterpreterState_Delete(interp);
614 return NULL;
617 /* Delete an interpreter and its last thread. This requires that the
618 given thread state is current, that the thread has no remaining
619 frames, and that it is its interpreter's only remaining thread.
620 It is a fatal error to violate these constraints.
622 (Py_Finalize() doesn't have these constraints -- it zaps
623 everything, regardless.)
625 Locking: as above.
629 void
630 Py_EndInterpreter(PyThreadState *tstate)
632 PyInterpreterState *interp = tstate->interp;
634 if (tstate != PyThreadState_GET())
635 Py_FatalError("Py_EndInterpreter: thread is not current");
636 if (tstate->frame != NULL)
637 Py_FatalError("Py_EndInterpreter: thread still has a frame");
638 if (tstate != interp->tstate_head || tstate->next != NULL)
639 Py_FatalError("Py_EndInterpreter: not the last thread");
641 PyImport_Cleanup();
642 PyInterpreterState_Clear(interp);
643 PyThreadState_Swap(NULL);
644 PyInterpreterState_Delete(interp);
647 static char *progname = "python";
649 void
650 Py_SetProgramName(char *pn)
652 if (pn && *pn)
653 progname = pn;
656 char *
657 Py_GetProgramName(void)
659 return progname;
662 static char *default_home = NULL;
664 void
665 Py_SetPythonHome(char *home)
667 default_home = home;
670 char *
671 Py_GetPythonHome(void)
673 char *home = default_home;
674 if (home == NULL && !Py_IgnoreEnvironmentFlag)
675 home = Py_GETENV("PYTHONHOME");
676 return home;
679 /* Create __main__ module */
681 static void
682 initmain(void)
684 PyObject *m, *d;
685 m = PyImport_AddModule("__main__");
686 if (m == NULL)
687 Py_FatalError("can't create __main__ module");
688 d = PyModule_GetDict(m);
689 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
690 PyObject *bimod = PyImport_ImportModule("__builtin__");
691 if (bimod == NULL ||
692 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
693 Py_FatalError("can't add __builtins__ to __main__");
694 Py_DECREF(bimod);
698 /* Import the site module (not into __main__ though) */
700 static void
701 initsite(void)
703 PyObject *m, *f;
704 m = PyImport_ImportModule("site");
705 if (m == NULL) {
706 f = PySys_GetObject("stderr");
707 if (Py_VerboseFlag) {
708 PyFile_WriteString(
709 "'import site' failed; traceback:\n", f);
710 PyErr_Print();
712 else {
713 PyFile_WriteString(
714 "'import site' failed; use -v for traceback\n", f);
715 PyErr_Clear();
718 else {
719 Py_DECREF(m);
723 /* Parse input from a file and execute it */
726 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
727 PyCompilerFlags *flags)
729 if (filename == NULL)
730 filename = "???";
731 if (Py_FdIsInteractive(fp, filename)) {
732 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
733 if (closeit)
734 fclose(fp);
735 return err;
737 else
738 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
742 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
744 PyObject *v;
745 int ret;
746 PyCompilerFlags local_flags;
748 if (flags == NULL) {
749 flags = &local_flags;
750 local_flags.cf_flags = 0;
752 v = PySys_GetObject("ps1");
753 if (v == NULL) {
754 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
755 Py_XDECREF(v);
757 v = PySys_GetObject("ps2");
758 if (v == NULL) {
759 PySys_SetObject("ps2", v = PyString_FromString("... "));
760 Py_XDECREF(v);
762 for (;;) {
763 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
764 PRINT_TOTAL_REFS();
765 if (ret == E_EOF)
766 return 0;
768 if (ret == E_NOMEM)
769 return -1;
774 #if 0
775 /* compute parser flags based on compiler flags */
776 #define PARSER_FLAGS(flags) \
777 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
778 PyPARSE_DONT_IMPLY_DEDENT : 0)) : 0)
779 #endif
780 #if 1
781 /* Keep an example of flags with future keyword support. */
782 #define PARSER_FLAGS(flags) \
783 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
784 PyPARSE_DONT_IMPLY_DEDENT : 0) \
785 | (((flags)->cf_flags & CO_FUTURE_PRINT_FUNCTION) ? \
786 PyPARSE_PRINT_IS_FUNCTION : 0) \
787 | (((flags)->cf_flags & CO_FUTURE_UNICODE_LITERALS) ? \
788 PyPARSE_UNICODE_LITERALS : 0) \
789 ) : 0)
790 #endif
793 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
795 PyObject *m, *d, *v, *w;
796 mod_ty mod;
797 PyArena *arena;
798 char *ps1 = "", *ps2 = "";
799 int errcode = 0;
801 v = PySys_GetObject("ps1");
802 if (v != NULL) {
803 v = PyObject_Str(v);
804 if (v == NULL)
805 PyErr_Clear();
806 else if (PyString_Check(v))
807 ps1 = PyString_AsString(v);
809 w = PySys_GetObject("ps2");
810 if (w != NULL) {
811 w = PyObject_Str(w);
812 if (w == NULL)
813 PyErr_Clear();
814 else if (PyString_Check(w))
815 ps2 = PyString_AsString(w);
817 arena = PyArena_New();
818 if (arena == NULL) {
819 Py_XDECREF(v);
820 Py_XDECREF(w);
821 return -1;
823 mod = PyParser_ASTFromFile(fp, filename,
824 Py_single_input, ps1, ps2,
825 flags, &errcode, arena);
826 Py_XDECREF(v);
827 Py_XDECREF(w);
828 if (mod == NULL) {
829 PyArena_Free(arena);
830 if (errcode == E_EOF) {
831 PyErr_Clear();
832 return E_EOF;
834 PyErr_Print();
835 return -1;
837 m = PyImport_AddModule("__main__");
838 if (m == NULL) {
839 PyArena_Free(arena);
840 return -1;
842 d = PyModule_GetDict(m);
843 v = run_mod(mod, filename, d, d, flags, arena);
844 PyArena_Free(arena);
845 if (v == NULL) {
846 PyErr_Print();
847 return -1;
849 Py_DECREF(v);
850 if (Py_FlushLine())
851 PyErr_Clear();
852 return 0;
855 /* Check whether a file maybe a pyc file: Look at the extension,
856 the file type, and, if we may close it, at the first few bytes. */
858 static int
859 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
861 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
862 return 1;
864 /* Only look into the file if we are allowed to close it, since
865 it then should also be seekable. */
866 if (closeit) {
867 /* Read only two bytes of the magic. If the file was opened in
868 text mode, the bytes 3 and 4 of the magic (\r\n) might not
869 be read as they are on disk. */
870 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
871 unsigned char buf[2];
872 /* Mess: In case of -x, the stream is NOT at its start now,
873 and ungetc() was used to push back the first newline,
874 which makes the current stream position formally undefined,
875 and a x-platform nightmare.
876 Unfortunately, we have no direct way to know whether -x
877 was specified. So we use a terrible hack: if the current
878 stream position is not 0, we assume -x was specified, and
879 give up. Bug 132850 on SourceForge spells out the
880 hopelessness of trying anything else (fseek and ftell
881 don't work predictably x-platform for text-mode files).
883 int ispyc = 0;
884 if (ftell(fp) == 0) {
885 if (fread(buf, 1, 2, fp) == 2 &&
886 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
887 ispyc = 1;
888 rewind(fp);
890 return ispyc;
892 return 0;
896 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
897 PyCompilerFlags *flags)
899 PyObject *m, *d, *v;
900 const char *ext;
901 int set_file_name = 0, ret, len;
903 m = PyImport_AddModule("__main__");
904 if (m == NULL)
905 return -1;
906 d = PyModule_GetDict(m);
907 if (PyDict_GetItemString(d, "__file__") == NULL) {
908 PyObject *f = PyString_FromString(filename);
909 if (f == NULL)
910 return -1;
911 if (PyDict_SetItemString(d, "__file__", f) < 0) {
912 Py_DECREF(f);
913 return -1;
915 set_file_name = 1;
916 Py_DECREF(f);
918 len = strlen(filename);
919 ext = filename + len - (len > 4 ? 4 : 0);
920 if (maybe_pyc_file(fp, filename, ext, closeit)) {
921 /* Try to run a pyc file. First, re-open in binary */
922 if (closeit)
923 fclose(fp);
924 if ((fp = fopen(filename, "rb")) == NULL) {
925 fprintf(stderr, "python: Can't reopen .pyc file\n");
926 ret = -1;
927 goto done;
929 /* Turn on optimization if a .pyo file is given */
930 if (strcmp(ext, ".pyo") == 0)
931 Py_OptimizeFlag = 1;
932 v = run_pyc_file(fp, filename, d, d, flags);
933 } else {
934 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
935 closeit, flags);
937 if (v == NULL) {
938 PyErr_Print();
939 ret = -1;
940 goto done;
942 Py_DECREF(v);
943 if (Py_FlushLine())
944 PyErr_Clear();
945 ret = 0;
946 done:
947 if (set_file_name && PyDict_DelItemString(d, "__file__"))
948 PyErr_Clear();
949 return ret;
953 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
955 PyObject *m, *d, *v;
956 m = PyImport_AddModule("__main__");
957 if (m == NULL)
958 return -1;
959 d = PyModule_GetDict(m);
960 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
961 if (v == NULL) {
962 PyErr_Print();
963 return -1;
965 Py_DECREF(v);
966 if (Py_FlushLine())
967 PyErr_Clear();
968 return 0;
971 static int
972 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
973 int *lineno, int *offset, const char **text)
975 long hold;
976 PyObject *v;
978 /* old style errors */
979 if (PyTuple_Check(err))
980 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
981 lineno, offset, text);
983 /* new style errors. `err' is an instance */
985 if (! (v = PyObject_GetAttrString(err, "msg")))
986 goto finally;
987 *message = v;
989 if (!(v = PyObject_GetAttrString(err, "filename")))
990 goto finally;
991 if (v == Py_None)
992 *filename = NULL;
993 else if (! (*filename = PyString_AsString(v)))
994 goto finally;
996 Py_DECREF(v);
997 if (!(v = PyObject_GetAttrString(err, "lineno")))
998 goto finally;
999 hold = PyInt_AsLong(v);
1000 Py_DECREF(v);
1001 v = NULL;
1002 if (hold < 0 && PyErr_Occurred())
1003 goto finally;
1004 *lineno = (int)hold;
1006 if (!(v = PyObject_GetAttrString(err, "offset")))
1007 goto finally;
1008 if (v == Py_None) {
1009 *offset = -1;
1010 Py_DECREF(v);
1011 v = NULL;
1012 } else {
1013 hold = PyInt_AsLong(v);
1014 Py_DECREF(v);
1015 v = NULL;
1016 if (hold < 0 && PyErr_Occurred())
1017 goto finally;
1018 *offset = (int)hold;
1021 if (!(v = PyObject_GetAttrString(err, "text")))
1022 goto finally;
1023 if (v == Py_None)
1024 *text = NULL;
1025 else if (! (*text = PyString_AsString(v)))
1026 goto finally;
1027 Py_DECREF(v);
1028 return 1;
1030 finally:
1031 Py_XDECREF(v);
1032 return 0;
1035 void
1036 PyErr_Print(void)
1038 PyErr_PrintEx(1);
1041 static void
1042 print_error_text(PyObject *f, int offset, const char *text)
1044 char *nl;
1045 if (offset >= 0) {
1046 if (offset > 0 && offset == (int)strlen(text))
1047 offset--;
1048 for (;;) {
1049 nl = strchr(text, '\n');
1050 if (nl == NULL || nl-text >= offset)
1051 break;
1052 offset -= (int)(nl+1-text);
1053 text = nl+1;
1055 while (*text == ' ' || *text == '\t') {
1056 text++;
1057 offset--;
1060 PyFile_WriteString(" ", f);
1061 PyFile_WriteString(text, f);
1062 if (*text == '\0' || text[strlen(text)-1] != '\n')
1063 PyFile_WriteString("\n", f);
1064 if (offset == -1)
1065 return;
1066 PyFile_WriteString(" ", f);
1067 offset--;
1068 while (offset > 0) {
1069 PyFile_WriteString(" ", f);
1070 offset--;
1072 PyFile_WriteString("^\n", f);
1075 static void
1076 handle_system_exit(void)
1078 PyObject *exception, *value, *tb;
1079 int exitcode = 0;
1081 if (Py_InspectFlag)
1082 /* Don't exit if -i flag was given. This flag is set to 0
1083 * when entering interactive mode for inspecting. */
1084 return;
1086 PyErr_Fetch(&exception, &value, &tb);
1087 if (Py_FlushLine())
1088 PyErr_Clear();
1089 fflush(stdout);
1090 if (value == NULL || value == Py_None)
1091 goto done;
1092 if (PyExceptionInstance_Check(value)) {
1093 /* The error code should be in the `code' attribute. */
1094 PyObject *code = PyObject_GetAttrString(value, "code");
1095 if (code) {
1096 Py_DECREF(value);
1097 value = code;
1098 if (value == Py_None)
1099 goto done;
1101 /* If we failed to dig out the 'code' attribute,
1102 just let the else clause below print the error. */
1104 if (PyInt_Check(value))
1105 exitcode = (int)PyInt_AsLong(value);
1106 else {
1107 PyObject_Print(value, stderr, Py_PRINT_RAW);
1108 PySys_WriteStderr("\n");
1109 exitcode = 1;
1111 done:
1112 /* Restore and clear the exception info, in order to properly decref
1113 * the exception, value, and traceback. If we just exit instead,
1114 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1115 * some finalizers from running.
1117 PyErr_Restore(exception, value, tb);
1118 PyErr_Clear();
1119 Py_Exit(exitcode);
1120 /* NOTREACHED */
1123 void
1124 PyErr_PrintEx(int set_sys_last_vars)
1126 PyObject *exception, *v, *tb, *hook;
1128 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1129 handle_system_exit();
1131 PyErr_Fetch(&exception, &v, &tb);
1132 if (exception == NULL)
1133 return;
1134 PyErr_NormalizeException(&exception, &v, &tb);
1135 if (exception == NULL)
1136 return;
1137 /* Now we know v != NULL too */
1138 if (set_sys_last_vars) {
1139 PySys_SetObject("last_type", exception);
1140 PySys_SetObject("last_value", v);
1141 PySys_SetObject("last_traceback", tb);
1143 hook = PySys_GetObject("excepthook");
1144 if (hook) {
1145 PyObject *args = PyTuple_Pack(3,
1146 exception, v, tb ? tb : Py_None);
1147 PyObject *result = PyEval_CallObject(hook, args);
1148 if (result == NULL) {
1149 PyObject *exception2, *v2, *tb2;
1150 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1151 handle_system_exit();
1153 PyErr_Fetch(&exception2, &v2, &tb2);
1154 PyErr_NormalizeException(&exception2, &v2, &tb2);
1155 /* It should not be possible for exception2 or v2
1156 to be NULL. However PyErr_Display() can't
1157 tolerate NULLs, so just be safe. */
1158 if (exception2 == NULL) {
1159 exception2 = Py_None;
1160 Py_INCREF(exception2);
1162 if (v2 == NULL) {
1163 v2 = Py_None;
1164 Py_INCREF(v2);
1166 if (Py_FlushLine())
1167 PyErr_Clear();
1168 fflush(stdout);
1169 PySys_WriteStderr("Error in sys.excepthook:\n");
1170 PyErr_Display(exception2, v2, tb2);
1171 PySys_WriteStderr("\nOriginal exception was:\n");
1172 PyErr_Display(exception, v, tb);
1173 Py_DECREF(exception2);
1174 Py_DECREF(v2);
1175 Py_XDECREF(tb2);
1177 Py_XDECREF(result);
1178 Py_XDECREF(args);
1179 } else {
1180 PySys_WriteStderr("sys.excepthook is missing\n");
1181 PyErr_Display(exception, v, tb);
1183 Py_XDECREF(exception);
1184 Py_XDECREF(v);
1185 Py_XDECREF(tb);
1188 void
1189 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1191 int err = 0;
1192 PyObject *f = PySys_GetObject("stderr");
1193 Py_INCREF(value);
1194 if (f == NULL)
1195 fprintf(stderr, "lost sys.stderr\n");
1196 else {
1197 if (Py_FlushLine())
1198 PyErr_Clear();
1199 fflush(stdout);
1200 if (tb && tb != Py_None)
1201 err = PyTraceBack_Print(tb, f);
1202 if (err == 0 &&
1203 PyObject_HasAttrString(value, "print_file_and_line"))
1205 PyObject *message;
1206 const char *filename, *text;
1207 int lineno, offset;
1208 if (!parse_syntax_error(value, &message, &filename,
1209 &lineno, &offset, &text))
1210 PyErr_Clear();
1211 else {
1212 char buf[10];
1213 PyFile_WriteString(" File \"", f);
1214 if (filename == NULL)
1215 PyFile_WriteString("<string>", f);
1216 else
1217 PyFile_WriteString(filename, f);
1218 PyFile_WriteString("\", line ", f);
1219 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1220 PyFile_WriteString(buf, f);
1221 PyFile_WriteString("\n", f);
1222 if (text != NULL)
1223 print_error_text(f, offset, text);
1224 Py_DECREF(value);
1225 value = message;
1226 /* Can't be bothered to check all those
1227 PyFile_WriteString() calls */
1228 if (PyErr_Occurred())
1229 err = -1;
1232 if (err) {
1233 /* Don't do anything else */
1235 else if (PyExceptionClass_Check(exception)) {
1236 PyObject* moduleName;
1237 char* className = PyExceptionClass_Name(exception);
1238 if (className != NULL) {
1239 char *dot = strrchr(className, '.');
1240 if (dot != NULL)
1241 className = dot+1;
1244 moduleName = PyObject_GetAttrString(exception, "__module__");
1245 if (moduleName == NULL)
1246 err = PyFile_WriteString("<unknown>", f);
1247 else {
1248 char* modstr = PyString_AsString(moduleName);
1249 if (modstr && strcmp(modstr, "exceptions"))
1251 err = PyFile_WriteString(modstr, f);
1252 err += PyFile_WriteString(".", f);
1254 Py_DECREF(moduleName);
1256 if (err == 0) {
1257 if (className == NULL)
1258 err = PyFile_WriteString("<unknown>", f);
1259 else
1260 err = PyFile_WriteString(className, f);
1263 else
1264 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1265 if (err == 0 && (value != Py_None)) {
1266 PyObject *s = PyObject_Str(value);
1267 /* only print colon if the str() of the
1268 object is not the empty string
1270 if (s == NULL)
1271 err = -1;
1272 else if (!PyString_Check(s) ||
1273 PyString_GET_SIZE(s) != 0)
1274 err = PyFile_WriteString(": ", f);
1275 if (err == 0)
1276 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1277 Py_XDECREF(s);
1279 /* try to write a newline in any case */
1280 err += PyFile_WriteString("\n", f);
1282 Py_DECREF(value);
1283 /* If an error happened here, don't show it.
1284 XXX This is wrong, but too many callers rely on this behavior. */
1285 if (err != 0)
1286 PyErr_Clear();
1289 PyObject *
1290 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1291 PyObject *locals, PyCompilerFlags *flags)
1293 PyObject *ret = NULL;
1294 mod_ty mod;
1295 PyArena *arena = PyArena_New();
1296 if (arena == NULL)
1297 return NULL;
1299 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1300 if (mod != NULL)
1301 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1302 PyArena_Free(arena);
1303 return ret;
1306 PyObject *
1307 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1308 PyObject *locals, int closeit, PyCompilerFlags *flags)
1310 PyObject *ret;
1311 mod_ty mod;
1312 PyArena *arena = PyArena_New();
1313 if (arena == NULL)
1314 return NULL;
1316 mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1317 flags, NULL, arena);
1318 if (closeit)
1319 fclose(fp);
1320 if (mod == NULL) {
1321 PyArena_Free(arena);
1322 return NULL;
1324 ret = run_mod(mod, filename, globals, locals, flags, arena);
1325 PyArena_Free(arena);
1326 return ret;
1329 static PyObject *
1330 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1331 PyCompilerFlags *flags, PyArena *arena)
1333 PyCodeObject *co;
1334 PyObject *v;
1335 co = PyAST_Compile(mod, filename, flags, arena);
1336 if (co == NULL)
1337 return NULL;
1338 v = PyEval_EvalCode(co, globals, locals);
1339 Py_DECREF(co);
1340 return v;
1343 static PyObject *
1344 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1345 PyObject *locals, PyCompilerFlags *flags)
1347 PyCodeObject *co;
1348 PyObject *v;
1349 long magic;
1350 long PyImport_GetMagicNumber(void);
1352 magic = PyMarshal_ReadLongFromFile(fp);
1353 if (magic != PyImport_GetMagicNumber()) {
1354 PyErr_SetString(PyExc_RuntimeError,
1355 "Bad magic number in .pyc file");
1356 return NULL;
1358 (void) PyMarshal_ReadLongFromFile(fp);
1359 v = PyMarshal_ReadLastObjectFromFile(fp);
1360 fclose(fp);
1361 if (v == NULL || !PyCode_Check(v)) {
1362 Py_XDECREF(v);
1363 PyErr_SetString(PyExc_RuntimeError,
1364 "Bad code object in .pyc file");
1365 return NULL;
1367 co = (PyCodeObject *)v;
1368 v = PyEval_EvalCode(co, globals, locals);
1369 if (v && flags)
1370 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1371 Py_DECREF(co);
1372 return v;
1375 PyObject *
1376 Py_CompileStringFlags(const char *str, const char *filename, int start,
1377 PyCompilerFlags *flags)
1379 PyCodeObject *co;
1380 mod_ty mod;
1381 PyArena *arena = PyArena_New();
1382 if (arena == NULL)
1383 return NULL;
1385 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1386 if (mod == NULL) {
1387 PyArena_Free(arena);
1388 return NULL;
1390 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1391 PyObject *result = PyAST_mod2obj(mod);
1392 PyArena_Free(arena);
1393 return result;
1395 co = PyAST_Compile(mod, filename, flags, arena);
1396 PyArena_Free(arena);
1397 return (PyObject *)co;
1400 struct symtable *
1401 Py_SymtableString(const char *str, const char *filename, int start)
1403 struct symtable *st;
1404 mod_ty mod;
1405 PyCompilerFlags flags;
1406 PyArena *arena = PyArena_New();
1407 if (arena == NULL)
1408 return NULL;
1410 flags.cf_flags = 0;
1412 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1413 if (mod == NULL) {
1414 PyArena_Free(arena);
1415 return NULL;
1417 st = PySymtable_Build(mod, filename, 0);
1418 PyArena_Free(arena);
1419 return st;
1422 /* Preferred access to parser is through AST. */
1423 mod_ty
1424 PyParser_ASTFromString(const char *s, const char *filename, int start,
1425 PyCompilerFlags *flags, PyArena *arena)
1427 mod_ty mod;
1428 PyCompilerFlags localflags;
1429 perrdetail err;
1430 int iflags = PARSER_FLAGS(flags);
1432 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1433 &_PyParser_Grammar, start, &err,
1434 &iflags);
1435 if (flags == NULL) {
1436 localflags.cf_flags = 0;
1437 flags = &localflags;
1439 if (n) {
1440 flags->cf_flags |= iflags & PyCF_MASK;
1441 mod = PyAST_FromNode(n, flags, filename, arena);
1442 PyNode_Free(n);
1443 return mod;
1445 else {
1446 err_input(&err);
1447 return NULL;
1451 mod_ty
1452 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1453 char *ps2, PyCompilerFlags *flags, int *errcode,
1454 PyArena *arena)
1456 mod_ty mod;
1457 PyCompilerFlags localflags;
1458 perrdetail err;
1459 int iflags = PARSER_FLAGS(flags);
1461 node *n = PyParser_ParseFileFlagsEx(fp, filename, &_PyParser_Grammar,
1462 start, ps1, ps2, &err, &iflags);
1463 if (flags == NULL) {
1464 localflags.cf_flags = 0;
1465 flags = &localflags;
1467 if (n) {
1468 flags->cf_flags |= iflags & PyCF_MASK;
1469 mod = PyAST_FromNode(n, flags, filename, arena);
1470 PyNode_Free(n);
1471 return mod;
1473 else {
1474 err_input(&err);
1475 if (errcode)
1476 *errcode = err.error;
1477 return NULL;
1481 /* Simplified interface to parsefile -- return node or set exception */
1483 node *
1484 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1486 perrdetail err;
1487 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1488 start, NULL, NULL, &err, flags);
1489 if (n == NULL)
1490 err_input(&err);
1492 return n;
1495 /* Simplified interface to parsestring -- return node or set exception */
1497 node *
1498 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1500 perrdetail err;
1501 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1502 start, &err, flags);
1503 if (n == NULL)
1504 err_input(&err);
1505 return n;
1508 node *
1509 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1510 int start, int flags)
1512 perrdetail err;
1513 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1514 &_PyParser_Grammar, start, &err, flags);
1515 if (n == NULL)
1516 err_input(&err);
1517 return n;
1520 node *
1521 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1523 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1526 /* May want to move a more generalized form of this to parsetok.c or
1527 even parser modules. */
1529 void
1530 PyParser_SetError(perrdetail *err)
1532 err_input(err);
1535 /* Set the error appropriate to the given input error code (see errcode.h) */
1537 static void
1538 err_input(perrdetail *err)
1540 PyObject *v, *w, *errtype;
1541 PyObject* u = NULL;
1542 char *msg = NULL;
1543 errtype = PyExc_SyntaxError;
1544 switch (err->error) {
1545 case E_SYNTAX:
1546 errtype = PyExc_IndentationError;
1547 if (err->expected == INDENT)
1548 msg = "expected an indented block";
1549 else if (err->token == INDENT)
1550 msg = "unexpected indent";
1551 else if (err->token == DEDENT)
1552 msg = "unexpected unindent";
1553 else {
1554 errtype = PyExc_SyntaxError;
1555 msg = "invalid syntax";
1557 break;
1558 case E_TOKEN:
1559 msg = "invalid token";
1560 break;
1561 case E_EOFS:
1562 msg = "EOF while scanning triple-quoted string literal";
1563 break;
1564 case E_EOLS:
1565 msg = "EOL while scanning string literal";
1566 break;
1567 case E_INTR:
1568 if (!PyErr_Occurred())
1569 PyErr_SetNone(PyExc_KeyboardInterrupt);
1570 goto cleanup;
1571 case E_NOMEM:
1572 PyErr_NoMemory();
1573 goto cleanup;
1574 case E_EOF:
1575 msg = "unexpected EOF while parsing";
1576 break;
1577 case E_TABSPACE:
1578 errtype = PyExc_TabError;
1579 msg = "inconsistent use of tabs and spaces in indentation";
1580 break;
1581 case E_OVERFLOW:
1582 msg = "expression too long";
1583 break;
1584 case E_DEDENT:
1585 errtype = PyExc_IndentationError;
1586 msg = "unindent does not match any outer indentation level";
1587 break;
1588 case E_TOODEEP:
1589 errtype = PyExc_IndentationError;
1590 msg = "too many levels of indentation";
1591 break;
1592 case E_DECODE: {
1593 PyObject *type, *value, *tb;
1594 PyErr_Fetch(&type, &value, &tb);
1595 if (value != NULL) {
1596 u = PyObject_Str(value);
1597 if (u != NULL) {
1598 msg = PyString_AsString(u);
1601 if (msg == NULL)
1602 msg = "unknown decode error";
1603 Py_XDECREF(type);
1604 Py_XDECREF(value);
1605 Py_XDECREF(tb);
1606 break;
1608 case E_LINECONT:
1609 msg = "unexpected character after line continuation character";
1610 break;
1611 default:
1612 fprintf(stderr, "error=%d\n", err->error);
1613 msg = "unknown parsing error";
1614 break;
1616 v = Py_BuildValue("(ziiz)", err->filename,
1617 err->lineno, err->offset, err->text);
1618 w = NULL;
1619 if (v != NULL)
1620 w = Py_BuildValue("(sO)", msg, v);
1621 Py_XDECREF(u);
1622 Py_XDECREF(v);
1623 PyErr_SetObject(errtype, w);
1624 Py_XDECREF(w);
1625 cleanup:
1626 if (err->text != NULL) {
1627 PyObject_FREE(err->text);
1628 err->text = NULL;
1632 /* Print fatal error message and abort */
1634 void
1635 Py_FatalError(const char *msg)
1637 fprintf(stderr, "Fatal Python error: %s\n", msg);
1638 fflush(stderr); /* it helps in Windows debug build */
1640 #ifdef MS_WINDOWS
1642 size_t len = strlen(msg);
1643 WCHAR* buffer;
1644 size_t i;
1646 /* Convert the message to wchar_t. This uses a simple one-to-one
1647 conversion, assuming that the this error message actually uses ASCII
1648 only. If this ceases to be true, we will have to convert. */
1649 buffer = alloca( (len+1) * (sizeof *buffer));
1650 for( i=0; i<=len; ++i)
1651 buffer[i] = msg[i];
1652 OutputDebugStringW(L"Fatal Python error: ");
1653 OutputDebugStringW(buffer);
1654 OutputDebugStringW(L"\n");
1656 #ifdef _DEBUG
1657 DebugBreak();
1658 #endif
1659 #endif /* MS_WINDOWS */
1660 abort();
1663 /* Clean up and exit */
1665 #ifdef WITH_THREAD
1666 #include "pythread.h"
1667 #endif
1669 #define NEXITFUNCS 32
1670 static void (*exitfuncs[NEXITFUNCS])(void);
1671 static int nexitfuncs = 0;
1673 int Py_AtExit(void (*func)(void))
1675 if (nexitfuncs >= NEXITFUNCS)
1676 return -1;
1677 exitfuncs[nexitfuncs++] = func;
1678 return 0;
1681 static void
1682 call_sys_exitfunc(void)
1684 PyObject *exitfunc = PySys_GetObject("exitfunc");
1686 if (exitfunc) {
1687 PyObject *res;
1688 Py_INCREF(exitfunc);
1689 PySys_SetObject("exitfunc", (PyObject *)NULL);
1690 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1691 if (res == NULL) {
1692 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1693 PySys_WriteStderr("Error in sys.exitfunc:\n");
1695 PyErr_Print();
1697 Py_DECREF(exitfunc);
1700 if (Py_FlushLine())
1701 PyErr_Clear();
1704 static void
1705 call_ll_exitfuncs(void)
1707 while (nexitfuncs > 0)
1708 (*exitfuncs[--nexitfuncs])();
1710 fflush(stdout);
1711 fflush(stderr);
1714 void
1715 Py_Exit(int sts)
1717 Py_Finalize();
1719 exit(sts);
1722 static void
1723 initsigs(void)
1725 #ifdef SIGPIPE
1726 PyOS_setsig(SIGPIPE, SIG_IGN);
1727 #endif
1728 #ifdef SIGXFZ
1729 PyOS_setsig(SIGXFZ, SIG_IGN);
1730 #endif
1731 #ifdef SIGXFSZ
1732 PyOS_setsig(SIGXFSZ, SIG_IGN);
1733 #endif
1734 PyOS_InitInterrupts(); /* May imply initsignal() */
1739 * The file descriptor fd is considered ``interactive'' if either
1740 * a) isatty(fd) is TRUE, or
1741 * b) the -i flag was given, and the filename associated with
1742 * the descriptor is NULL or "<stdin>" or "???".
1745 Py_FdIsInteractive(FILE *fp, const char *filename)
1747 if (isatty((int)fileno(fp)))
1748 return 1;
1749 if (!Py_InteractiveFlag)
1750 return 0;
1751 return (filename == NULL) ||
1752 (strcmp(filename, "<stdin>") == 0) ||
1753 (strcmp(filename, "???") == 0);
1757 #if defined(USE_STACKCHECK)
1758 #if defined(WIN32) && defined(_MSC_VER)
1760 /* Stack checking for Microsoft C */
1762 #include <malloc.h>
1763 #include <excpt.h>
1766 * Return non-zero when we run out of memory on the stack; zero otherwise.
1769 PyOS_CheckStack(void)
1771 __try {
1772 /* alloca throws a stack overflow exception if there's
1773 not enough space left on the stack */
1774 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1775 return 0;
1776 } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1777 EXCEPTION_EXECUTE_HANDLER :
1778 EXCEPTION_CONTINUE_SEARCH) {
1779 int errcode = _resetstkoflw();
1780 if (errcode == 0)
1782 Py_FatalError("Could not reset the stack!");
1785 return 1;
1788 #endif /* WIN32 && _MSC_VER */
1790 /* Alternate implementations can be added here... */
1792 #endif /* USE_STACKCHECK */
1795 /* Wrappers around sigaction() or signal(). */
1797 PyOS_sighandler_t
1798 PyOS_getsig(int sig)
1800 #ifdef HAVE_SIGACTION
1801 struct sigaction context;
1802 if (sigaction(sig, NULL, &context) == -1)
1803 return SIG_ERR;
1804 return context.sa_handler;
1805 #else
1806 PyOS_sighandler_t handler;
1807 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1808 #if defined(_MSC_VER) && _MSC_VER >= 1400
1809 switch (sig) {
1810 /* Only these signals are valid */
1811 case SIGINT:
1812 case SIGILL:
1813 case SIGFPE:
1814 case SIGSEGV:
1815 case SIGTERM:
1816 case SIGBREAK:
1817 case SIGABRT:
1818 break;
1819 /* Don't call signal() with other values or it will assert */
1820 default:
1821 return SIG_ERR;
1823 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1824 handler = signal(sig, SIG_IGN);
1825 if (handler != SIG_ERR)
1826 signal(sig, handler);
1827 return handler;
1828 #endif
1831 PyOS_sighandler_t
1832 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1834 #ifdef HAVE_SIGACTION
1835 struct sigaction context, ocontext;
1836 context.sa_handler = handler;
1837 sigemptyset(&context.sa_mask);
1838 context.sa_flags = 0;
1839 if (sigaction(sig, &context, &ocontext) == -1)
1840 return SIG_ERR;
1841 return ocontext.sa_handler;
1842 #else
1843 PyOS_sighandler_t oldhandler;
1844 oldhandler = signal(sig, handler);
1845 #ifdef HAVE_SIGINTERRUPT
1846 siginterrupt(sig, 1);
1847 #endif
1848 return oldhandler;
1849 #endif
1852 /* Deprecated C API functions still provided for binary compatiblity */
1854 #undef PyParser_SimpleParseFile
1855 PyAPI_FUNC(node *)
1856 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1858 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1861 #undef PyParser_SimpleParseString
1862 PyAPI_FUNC(node *)
1863 PyParser_SimpleParseString(const char *str, int start)
1865 return PyParser_SimpleParseStringFlags(str, start, 0);
1868 #undef PyRun_AnyFile
1869 PyAPI_FUNC(int)
1870 PyRun_AnyFile(FILE *fp, const char *name)
1872 return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1875 #undef PyRun_AnyFileEx
1876 PyAPI_FUNC(int)
1877 PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1879 return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1882 #undef PyRun_AnyFileFlags
1883 PyAPI_FUNC(int)
1884 PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1886 return PyRun_AnyFileExFlags(fp, name, 0, flags);
1889 #undef PyRun_File
1890 PyAPI_FUNC(PyObject *)
1891 PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1893 return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1896 #undef PyRun_FileEx
1897 PyAPI_FUNC(PyObject *)
1898 PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1900 return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1903 #undef PyRun_FileFlags
1904 PyAPI_FUNC(PyObject *)
1905 PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1906 PyCompilerFlags *flags)
1908 return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1911 #undef PyRun_SimpleFile
1912 PyAPI_FUNC(int)
1913 PyRun_SimpleFile(FILE *f, const char *p)
1915 return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1918 #undef PyRun_SimpleFileEx
1919 PyAPI_FUNC(int)
1920 PyRun_SimpleFileEx(FILE *f, const char *p, int c)
1922 return PyRun_SimpleFileExFlags(f, p, c, NULL);
1926 #undef PyRun_String
1927 PyAPI_FUNC(PyObject *)
1928 PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1930 return PyRun_StringFlags(str, s, g, l, NULL);
1933 #undef PyRun_SimpleString
1934 PyAPI_FUNC(int)
1935 PyRun_SimpleString(const char *s)
1937 return PyRun_SimpleStringFlags(s, NULL);
1940 #undef Py_CompileString
1941 PyAPI_FUNC(PyObject *)
1942 Py_CompileString(const char *str, const char *p, int s)
1944 return Py_CompileStringFlags(str, p, s, NULL);
1947 #undef PyRun_InteractiveOne
1948 PyAPI_FUNC(int)
1949 PyRun_InteractiveOne(FILE *f, const char *p)
1951 return PyRun_InteractiveOneFlags(f, p, NULL);
1954 #undef PyRun_InteractiveLoop
1955 PyAPI_FUNC(int)
1956 PyRun_InteractiveLoop(FILE *f, const char *p)
1958 return PyRun_InteractiveLoopFlags(f, p, NULL);
1961 #ifdef __cplusplus
1963 #endif