AddressList.__str__(): Get rid of useless, and broken method. Closes
[python/dscho.git] / Python / pythonrun.c
bloba8dedde7c11d877b2f93df684d0210d0c1094a6b
2 /* Python interpreter top-level routines, including init/exit */
4 #include "Python.h"
6 #include "grammar.h"
7 #include "node.h"
8 #include "token.h"
9 #include "parsetok.h"
10 #include "errcode.h"
11 #include "compile.h"
12 #include "symtable.h"
13 #include "eval.h"
14 #include "marshal.h"
16 #ifdef HAVE_SIGNAL_H
17 #include <signal.h>
18 #endif
20 #ifdef HAVE_LANGINFO_H
21 #include <locale.h>
22 #include <langinfo.h>
23 #endif
25 #ifdef MS_WINDOWS
26 #undef BYTE
27 #include "windows.h"
28 #endif
30 #ifdef macintosh
31 #include "macglue.h"
32 #endif
33 extern char *Py_GetPath(void);
35 extern grammar _PyParser_Grammar; /* From graminit.c */
37 /* Forward */
38 static void initmain(void);
39 static void initsite(void);
40 static PyObject *run_err_node(node *, const char *, PyObject *, PyObject *,
41 PyCompilerFlags *);
42 static PyObject *run_node(node *, const char *, PyObject *, PyObject *,
43 PyCompilerFlags *);
44 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
45 PyCompilerFlags *);
46 static void err_input(perrdetail *);
47 static void initsigs(void);
48 static void call_sys_exitfunc(void);
49 static void call_ll_exitfuncs(void);
50 extern void _PyUnicode_Init(void);
51 extern void _PyUnicode_Fini(void);
53 #ifdef WITH_THREAD
54 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
55 extern void _PyGILState_Fini(void);
56 #endif /* WITH_THREAD */
58 int Py_DebugFlag; /* Needed by parser.c */
59 int Py_VerboseFlag; /* Needed by import.c */
60 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
61 int Py_NoSiteFlag; /* Suppress 'import site' */
62 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
63 int Py_FrozenFlag; /* Needed by getpath.c */
64 int Py_UnicodeFlag = 0; /* Needed by compile.c */
65 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
66 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
67 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
68 true divisions (which they will be in 2.3). */
69 int _Py_QnewFlag = 0;
71 /* Reference to 'warnings' module, to avoid importing it
72 on the fly when the import lock may be held. See 683658
74 PyObject *PyModule_WarningsModule = NULL;
76 static int initialized = 0;
78 /* API to access the initialized flag -- useful for esoteric use */
80 int
81 Py_IsInitialized(void)
83 return initialized;
86 /* Global initializations. Can be undone by Py_Finalize(). Don't
87 call this twice without an intervening Py_Finalize() call. When
88 initializations fail, a fatal error is issued and the function does
89 not return. On return, the first thread and interpreter state have
90 been created.
92 Locking: you must hold the interpreter lock while calling this.
93 (If the lock has not yet been initialized, that's equivalent to
94 having the lock, but you cannot use multiple threads.)
98 static int
99 add_flag(int flag, const char *envs)
101 int env = atoi(envs);
102 if (flag < env)
103 flag = env;
104 if (flag < 1)
105 flag = 1;
106 return flag;
109 void
110 Py_Initialize(void)
112 PyInterpreterState *interp;
113 PyThreadState *tstate;
114 PyObject *bimod, *sysmod;
115 char *p;
116 extern void _Py_ReadyTypes(void);
118 if (initialized)
119 return;
120 initialized = 1;
122 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
123 Py_DebugFlag = add_flag(Py_DebugFlag, p);
124 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
125 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
126 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
127 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
129 interp = PyInterpreterState_New();
130 if (interp == NULL)
131 Py_FatalError("Py_Initialize: can't make first interpreter");
133 tstate = PyThreadState_New(interp);
134 if (tstate == NULL)
135 Py_FatalError("Py_Initialize: can't make first thread");
136 (void) PyThreadState_Swap(tstate);
138 _Py_ReadyTypes();
140 if (!_PyFrame_Init())
141 Py_FatalError("Py_Initialize: can't init frames");
143 if (!_PyInt_Init())
144 Py_FatalError("Py_Initialize: can't init ints");
146 interp->modules = PyDict_New();
147 if (interp->modules == NULL)
148 Py_FatalError("Py_Initialize: can't make modules dictionary");
150 #ifdef Py_USING_UNICODE
151 /* Init Unicode implementation; relies on the codec registry */
152 _PyUnicode_Init();
153 #endif
155 bimod = _PyBuiltin_Init();
156 if (bimod == NULL)
157 Py_FatalError("Py_Initialize: can't initialize __builtin__");
158 interp->builtins = PyModule_GetDict(bimod);
159 Py_INCREF(interp->builtins);
161 sysmod = _PySys_Init();
162 if (sysmod == NULL)
163 Py_FatalError("Py_Initialize: can't initialize sys");
164 interp->sysdict = PyModule_GetDict(sysmod);
165 Py_INCREF(interp->sysdict);
166 _PyImport_FixupExtension("sys", "sys");
167 PySys_SetPath(Py_GetPath());
168 PyDict_SetItemString(interp->sysdict, "modules",
169 interp->modules);
171 _PyImport_Init();
173 /* initialize builtin exceptions */
174 _PyExc_Init();
175 _PyImport_FixupExtension("exceptions", "exceptions");
177 /* phase 2 of builtins */
178 _PyImport_FixupExtension("__builtin__", "__builtin__");
180 _PyImportHooks_Init();
182 initsigs(); /* Signal handling stuff, including initintr() */
184 initmain(); /* Module __main__ */
185 if (!Py_NoSiteFlag)
186 initsite(); /* Module site */
188 /* auto-thread-state API, if available */
189 #ifdef WITH_THREAD
190 _PyGILState_Init(interp, tstate);
191 #endif /* WITH_THREAD */
193 PyModule_WarningsModule = PyImport_ImportModule("warnings");
195 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
196 /* On Unix, set the file system encoding according to the
197 user's preference, if the CODESET names a well-known
198 Python codec, and Py_FileSystemDefaultEncoding isn't
199 initialized by other means. */
200 if (!Py_FileSystemDefaultEncoding) {
201 char *saved_locale = setlocale(LC_CTYPE, NULL);
202 char *codeset;
203 setlocale(LC_CTYPE, "");
204 codeset = nl_langinfo(CODESET);
205 if (*codeset) {
206 PyObject *enc = PyCodec_Encoder(codeset);
207 if (enc) {
208 Py_FileSystemDefaultEncoding = strdup(codeset);
209 Py_DECREF(enc);
210 } else
211 PyErr_Clear();
213 setlocale(LC_CTYPE, saved_locale);
215 #endif
218 #ifdef COUNT_ALLOCS
219 extern void dump_counts(void);
220 #endif
222 /* Undo the effect of Py_Initialize().
224 Beware: if multiple interpreter and/or thread states exist, these
225 are not wiped out; only the current thread and interpreter state
226 are deleted. But since everything else is deleted, those other
227 interpreter and thread states should no longer be used.
229 (XXX We should do better, e.g. wipe out all interpreters and
230 threads.)
232 Locking: as above.
236 void
237 Py_Finalize(void)
239 PyInterpreterState *interp;
240 PyThreadState *tstate;
242 if (!initialized)
243 return;
245 /* The interpreter is still entirely intact at this point, and the
246 * exit funcs may be relying on that. In particular, if some thread
247 * or exit func is still waiting to do an import, the import machinery
248 * expects Py_IsInitialized() to return true. So don't say the
249 * interpreter is uninitialized until after the exit funcs have run.
250 * Note that Threading.py uses an exit func to do a join on all the
251 * threads created thru it, so this also protects pending imports in
252 * the threads created via Threading.
254 call_sys_exitfunc();
255 initialized = 0;
257 /* Get current thread state and interpreter pointer */
258 tstate = PyThreadState_Get();
259 interp = tstate->interp;
261 /* Disable signal handling */
262 PyOS_FiniInterrupts();
264 /* drop module references we saved */
265 Py_XDECREF(PyModule_WarningsModule);
266 PyModule_WarningsModule = NULL;
268 /* Collect garbage. This may call finalizers; it's nice to call these
269 before all modules are destroyed. */
270 PyGC_Collect();
272 /* Destroy all modules */
273 PyImport_Cleanup();
275 /* Collect final garbage. This disposes of cycles created by
276 new-style class definitions, for example. */
277 PyGC_Collect();
279 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
280 _PyImport_Fini();
282 /* Debugging stuff */
283 #ifdef COUNT_ALLOCS
284 dump_counts();
285 #endif
287 #ifdef Py_REF_DEBUG
288 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
289 #endif
291 #ifdef Py_TRACE_REFS
292 /* Display all objects still alive -- this can invoke arbitrary
293 * __repr__ overrides, so requires a mostly-intact interpreter.
294 * Alas, a lot of stuff may still be alive now that will be cleaned
295 * up later.
297 if (Py_GETENV("PYTHONDUMPREFS"))
298 _Py_PrintReferences(stderr);
299 #endif /* Py_TRACE_REFS */
301 /* Now we decref the exception classes. After this point nothing
302 can raise an exception. That's okay, because each Fini() method
303 below has been checked to make sure no exceptions are ever
304 raised.
306 _PyExc_Fini();
308 /* Cleanup auto-thread-state */
309 #ifdef WITH_THREAD
310 _PyGILState_Fini();
311 #endif /* WITH_THREAD */
313 /* Clear interpreter state */
314 PyInterpreterState_Clear(interp);
316 /* Delete current thread */
317 PyThreadState_Swap(NULL);
318 PyInterpreterState_Delete(interp);
320 /* Sundry finalizers */
321 PyMethod_Fini();
322 PyFrame_Fini();
323 PyCFunction_Fini();
324 PyTuple_Fini();
325 PyString_Fini();
326 PyInt_Fini();
327 PyFloat_Fini();
329 #ifdef Py_USING_UNICODE
330 /* Cleanup Unicode implementation */
331 _PyUnicode_Fini();
332 #endif
334 /* XXX Still allocated:
335 - various static ad-hoc pointers to interned strings
336 - int and float free list blocks
337 - whatever various modules and libraries allocate
340 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
342 #ifdef Py_TRACE_REFS
343 /* Display addresses (& refcnts) of all objects still alive.
344 * An address can be used to find the repr of the object, printed
345 * above by _Py_PrintReferences.
347 if (Py_GETENV("PYTHONDUMPREFS"))
348 _Py_PrintReferenceAddresses(stderr);
349 #endif /* Py_TRACE_REFS */
350 #ifdef PYMALLOC_DEBUG
351 if (Py_GETENV("PYTHONMALLOCSTATS"))
352 _PyObject_DebugMallocStats();
353 #endif
355 call_ll_exitfuncs();
358 /* Create and initialize a new interpreter and thread, and return the
359 new thread. This requires that Py_Initialize() has been called
360 first.
362 Unsuccessful initialization yields a NULL pointer. Note that *no*
363 exception information is available even in this case -- the
364 exception information is held in the thread, and there is no
365 thread.
367 Locking: as above.
371 PyThreadState *
372 Py_NewInterpreter(void)
374 PyInterpreterState *interp;
375 PyThreadState *tstate, *save_tstate;
376 PyObject *bimod, *sysmod;
378 if (!initialized)
379 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
381 interp = PyInterpreterState_New();
382 if (interp == NULL)
383 return NULL;
385 tstate = PyThreadState_New(interp);
386 if (tstate == NULL) {
387 PyInterpreterState_Delete(interp);
388 return NULL;
391 save_tstate = PyThreadState_Swap(tstate);
393 /* XXX The following is lax in error checking */
395 interp->modules = PyDict_New();
397 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
398 if (bimod != NULL) {
399 interp->builtins = PyModule_GetDict(bimod);
400 Py_INCREF(interp->builtins);
402 sysmod = _PyImport_FindExtension("sys", "sys");
403 if (bimod != NULL && sysmod != NULL) {
404 interp->sysdict = PyModule_GetDict(sysmod);
405 Py_INCREF(interp->sysdict);
406 PySys_SetPath(Py_GetPath());
407 PyDict_SetItemString(interp->sysdict, "modules",
408 interp->modules);
409 _PyImportHooks_Init();
410 initmain();
411 if (!Py_NoSiteFlag)
412 initsite();
415 if (!PyErr_Occurred())
416 return tstate;
418 /* Oops, it didn't work. Undo it all. */
420 PyErr_Print();
421 PyThreadState_Clear(tstate);
422 PyThreadState_Swap(save_tstate);
423 PyThreadState_Delete(tstate);
424 PyInterpreterState_Delete(interp);
426 return NULL;
429 /* Delete an interpreter and its last thread. This requires that the
430 given thread state is current, that the thread has no remaining
431 frames, and that it is its interpreter's only remaining thread.
432 It is a fatal error to violate these constraints.
434 (Py_Finalize() doesn't have these constraints -- it zaps
435 everything, regardless.)
437 Locking: as above.
441 void
442 Py_EndInterpreter(PyThreadState *tstate)
444 PyInterpreterState *interp = tstate->interp;
446 if (tstate != PyThreadState_Get())
447 Py_FatalError("Py_EndInterpreter: thread is not current");
448 if (tstate->frame != NULL)
449 Py_FatalError("Py_EndInterpreter: thread still has a frame");
450 if (tstate != interp->tstate_head || tstate->next != NULL)
451 Py_FatalError("Py_EndInterpreter: not the last thread");
453 PyImport_Cleanup();
454 PyInterpreterState_Clear(interp);
455 PyThreadState_Swap(NULL);
456 PyInterpreterState_Delete(interp);
459 static char *progname = "python";
461 void
462 Py_SetProgramName(char *pn)
464 if (pn && *pn)
465 progname = pn;
468 char *
469 Py_GetProgramName(void)
471 return progname;
474 static char *default_home = NULL;
476 void
477 Py_SetPythonHome(char *home)
479 default_home = home;
482 char *
483 Py_GetPythonHome(void)
485 char *home = default_home;
486 if (home == NULL && !Py_IgnoreEnvironmentFlag)
487 home = Py_GETENV("PYTHONHOME");
488 return home;
491 /* Create __main__ module */
493 static void
494 initmain(void)
496 PyObject *m, *d;
497 m = PyImport_AddModule("__main__");
498 if (m == NULL)
499 Py_FatalError("can't create __main__ module");
500 d = PyModule_GetDict(m);
501 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
502 PyObject *bimod = PyImport_ImportModule("__builtin__");
503 if (bimod == NULL ||
504 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
505 Py_FatalError("can't add __builtins__ to __main__");
506 Py_DECREF(bimod);
510 /* Import the site module (not into __main__ though) */
512 static void
513 initsite(void)
515 PyObject *m, *f;
516 m = PyImport_ImportModule("site");
517 if (m == NULL) {
518 f = PySys_GetObject("stderr");
519 if (Py_VerboseFlag) {
520 PyFile_WriteString(
521 "'import site' failed; traceback:\n", f);
522 PyErr_Print();
524 else {
525 PyFile_WriteString(
526 "'import site' failed; use -v for traceback\n", f);
527 PyErr_Clear();
530 else {
531 Py_DECREF(m);
535 /* Parse input from a file and execute it */
538 PyRun_AnyFile(FILE *fp, const char *filename)
540 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
544 PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
546 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
550 PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
552 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
556 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
557 PyCompilerFlags *flags)
559 if (filename == NULL)
560 filename = "???";
561 if (Py_FdIsInteractive(fp, filename)) {
562 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
563 if (closeit)
564 fclose(fp);
565 return err;
567 else
568 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
572 PyRun_InteractiveLoop(FILE *fp, const char *filename)
574 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
578 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
580 PyObject *v;
581 int ret;
582 PyCompilerFlags local_flags;
584 if (flags == NULL) {
585 flags = &local_flags;
586 local_flags.cf_flags = 0;
588 v = PySys_GetObject("ps1");
589 if (v == NULL) {
590 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
591 Py_XDECREF(v);
593 v = PySys_GetObject("ps2");
594 if (v == NULL) {
595 PySys_SetObject("ps2", v = PyString_FromString("... "));
596 Py_XDECREF(v);
598 for (;;) {
599 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
600 #ifdef Py_REF_DEBUG
601 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
602 #endif
603 if (ret == E_EOF)
604 return 0;
606 if (ret == E_NOMEM)
607 return -1;
613 PyRun_InteractiveOne(FILE *fp, const char *filename)
615 return PyRun_InteractiveOneFlags(fp, filename, NULL);
618 /* compute parser flags based on compiler flags */
619 #define PARSER_FLAGS(flags) \
620 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
621 PyPARSE_DONT_IMPLY_DEDENT : 0)
624 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
626 PyObject *m, *d, *v, *w;
627 node *n;
628 perrdetail err;
629 char *ps1 = "", *ps2 = "";
631 v = PySys_GetObject("ps1");
632 if (v != NULL) {
633 v = PyObject_Str(v);
634 if (v == NULL)
635 PyErr_Clear();
636 else if (PyString_Check(v))
637 ps1 = PyString_AsString(v);
639 w = PySys_GetObject("ps2");
640 if (w != NULL) {
641 w = PyObject_Str(w);
642 if (w == NULL)
643 PyErr_Clear();
644 else if (PyString_Check(w))
645 ps2 = PyString_AsString(w);
647 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
648 Py_single_input, ps1, ps2, &err,
649 PARSER_FLAGS(flags));
650 Py_XDECREF(v);
651 Py_XDECREF(w);
652 if (n == NULL) {
653 if (err.error == E_EOF) {
654 if (err.text)
655 PyMem_DEL(err.text);
656 return E_EOF;
658 err_input(&err);
659 PyErr_Print();
660 return err.error;
662 m = PyImport_AddModule("__main__");
663 if (m == NULL)
664 return -1;
665 d = PyModule_GetDict(m);
666 v = run_node(n, filename, d, d, flags);
667 if (v == NULL) {
668 PyErr_Print();
669 return -1;
671 Py_DECREF(v);
672 if (Py_FlushLine())
673 PyErr_Clear();
674 return 0;
678 PyRun_SimpleFile(FILE *fp, const char *filename)
680 return PyRun_SimpleFileEx(fp, filename, 0);
683 /* Check whether a file maybe a pyc file: Look at the extension,
684 the file type, and, if we may close it, at the first few bytes. */
686 static int
687 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
689 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
690 return 1;
692 #ifdef macintosh
693 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
694 if (PyMac_getfiletype((char *)filename) == 'PYC '
695 || PyMac_getfiletype((char *)filename) == 'APPL')
696 return 1;
697 #endif /* macintosh */
699 /* Only look into the file if we are allowed to close it, since
700 it then should also be seekable. */
701 if (closeit) {
702 /* Read only two bytes of the magic. If the file was opened in
703 text mode, the bytes 3 and 4 of the magic (\r\n) might not
704 be read as they are on disk. */
705 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
706 unsigned char buf[2];
707 /* Mess: In case of -x, the stream is NOT at its start now,
708 and ungetc() was used to push back the first newline,
709 which makes the current stream position formally undefined,
710 and a x-platform nightmare.
711 Unfortunately, we have no direct way to know whether -x
712 was specified. So we use a terrible hack: if the current
713 stream position is not 0, we assume -x was specified, and
714 give up. Bug 132850 on SourceForge spells out the
715 hopelessness of trying anything else (fseek and ftell
716 don't work predictably x-platform for text-mode files).
718 int ispyc = 0;
719 if (ftell(fp) == 0) {
720 if (fread(buf, 1, 2, fp) == 2 &&
721 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
722 ispyc = 1;
723 rewind(fp);
725 return ispyc;
727 return 0;
731 PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
733 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
737 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
738 PyCompilerFlags *flags)
740 PyObject *m, *d, *v;
741 const char *ext;
743 m = PyImport_AddModule("__main__");
744 if (m == NULL)
745 return -1;
746 d = PyModule_GetDict(m);
747 if (PyDict_GetItemString(d, "__file__") == NULL) {
748 PyObject *f = PyString_FromString(filename);
749 if (f == NULL)
750 return -1;
751 if (PyDict_SetItemString(d, "__file__", f) < 0) {
752 Py_DECREF(f);
753 return -1;
755 Py_DECREF(f);
757 ext = filename + strlen(filename) - 4;
758 if (maybe_pyc_file(fp, filename, ext, closeit)) {
759 /* Try to run a pyc file. First, re-open in binary */
760 if (closeit)
761 fclose(fp);
762 if ((fp = fopen(filename, "rb")) == NULL) {
763 fprintf(stderr, "python: Can't reopen .pyc file\n");
764 return -1;
766 /* Turn on optimization if a .pyo file is given */
767 if (strcmp(ext, ".pyo") == 0)
768 Py_OptimizeFlag = 1;
769 v = run_pyc_file(fp, filename, d, d, flags);
770 } else {
771 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
772 closeit, flags);
774 if (v == NULL) {
775 PyErr_Print();
776 return -1;
778 Py_DECREF(v);
779 if (Py_FlushLine())
780 PyErr_Clear();
781 return 0;
785 PyRun_SimpleString(const char *command)
787 return PyRun_SimpleStringFlags(command, NULL);
791 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
793 PyObject *m, *d, *v;
794 m = PyImport_AddModule("__main__");
795 if (m == NULL)
796 return -1;
797 d = PyModule_GetDict(m);
798 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
799 if (v == NULL) {
800 PyErr_Print();
801 return -1;
803 Py_DECREF(v);
804 if (Py_FlushLine())
805 PyErr_Clear();
806 return 0;
809 static int
810 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
811 int *lineno, int *offset, const char **text)
813 long hold;
814 PyObject *v;
816 /* old style errors */
817 if (PyTuple_Check(err))
818 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
819 lineno, offset, text);
821 /* new style errors. `err' is an instance */
823 if (! (v = PyObject_GetAttrString(err, "msg")))
824 goto finally;
825 *message = v;
827 if (!(v = PyObject_GetAttrString(err, "filename")))
828 goto finally;
829 if (v == Py_None)
830 *filename = NULL;
831 else if (! (*filename = PyString_AsString(v)))
832 goto finally;
834 Py_DECREF(v);
835 if (!(v = PyObject_GetAttrString(err, "lineno")))
836 goto finally;
837 hold = PyInt_AsLong(v);
838 Py_DECREF(v);
839 v = NULL;
840 if (hold < 0 && PyErr_Occurred())
841 goto finally;
842 *lineno = (int)hold;
844 if (!(v = PyObject_GetAttrString(err, "offset")))
845 goto finally;
846 if (v == Py_None) {
847 *offset = -1;
848 Py_DECREF(v);
849 v = NULL;
850 } else {
851 hold = PyInt_AsLong(v);
852 Py_DECREF(v);
853 v = NULL;
854 if (hold < 0 && PyErr_Occurred())
855 goto finally;
856 *offset = (int)hold;
859 if (!(v = PyObject_GetAttrString(err, "text")))
860 goto finally;
861 if (v == Py_None)
862 *text = NULL;
863 else if (! (*text = PyString_AsString(v)))
864 goto finally;
865 Py_DECREF(v);
866 return 1;
868 finally:
869 Py_XDECREF(v);
870 return 0;
873 void
874 PyErr_Print(void)
876 PyErr_PrintEx(1);
879 static void
880 print_error_text(PyObject *f, int offset, const char *text)
882 char *nl;
883 if (offset >= 0) {
884 if (offset > 0 && offset == (int)strlen(text))
885 offset--;
886 for (;;) {
887 nl = strchr(text, '\n');
888 if (nl == NULL || nl-text >= offset)
889 break;
890 offset -= (nl+1-text);
891 text = nl+1;
893 while (*text == ' ' || *text == '\t') {
894 text++;
895 offset--;
898 PyFile_WriteString(" ", f);
899 PyFile_WriteString(text, f);
900 if (*text == '\0' || text[strlen(text)-1] != '\n')
901 PyFile_WriteString("\n", f);
902 if (offset == -1)
903 return;
904 PyFile_WriteString(" ", f);
905 offset--;
906 while (offset > 0) {
907 PyFile_WriteString(" ", f);
908 offset--;
910 PyFile_WriteString("^\n", f);
913 static void
914 handle_system_exit(void)
916 PyObject *exception, *value, *tb;
917 int exitcode = 0;
919 PyErr_Fetch(&exception, &value, &tb);
920 if (Py_FlushLine())
921 PyErr_Clear();
922 fflush(stdout);
923 if (value == NULL || value == Py_None)
924 goto done;
925 if (PyInstance_Check(value)) {
926 /* The error code should be in the `code' attribute. */
927 PyObject *code = PyObject_GetAttrString(value, "code");
928 if (code) {
929 Py_DECREF(value);
930 value = code;
931 if (value == Py_None)
932 goto done;
934 /* If we failed to dig out the 'code' attribute,
935 just let the else clause below print the error. */
937 if (PyInt_Check(value))
938 exitcode = (int)PyInt_AsLong(value);
939 else {
940 PyObject_Print(value, stderr, Py_PRINT_RAW);
941 PySys_WriteStderr("\n");
942 exitcode = 1;
944 done:
945 /* Restore and clear the exception info, in order to properly decref
946 * the exception, value, and traceback. If we just exit instead,
947 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
948 * some finalizers from running.
950 PyErr_Restore(exception, value, tb);
951 PyErr_Clear();
952 Py_Exit(exitcode);
953 /* NOTREACHED */
956 void
957 PyErr_PrintEx(int set_sys_last_vars)
959 PyObject *exception, *v, *tb, *hook;
961 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
962 handle_system_exit();
964 PyErr_Fetch(&exception, &v, &tb);
965 PyErr_NormalizeException(&exception, &v, &tb);
966 if (exception == NULL)
967 return;
968 if (set_sys_last_vars) {
969 PySys_SetObject("last_type", exception);
970 PySys_SetObject("last_value", v);
971 PySys_SetObject("last_traceback", tb);
973 hook = PySys_GetObject("excepthook");
974 if (hook) {
975 PyObject *args = Py_BuildValue("(OOO)",
976 exception, v ? v : Py_None, tb ? tb : Py_None);
977 PyObject *result = PyEval_CallObject(hook, args);
978 if (result == NULL) {
979 PyObject *exception2, *v2, *tb2;
980 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
981 handle_system_exit();
983 PyErr_Fetch(&exception2, &v2, &tb2);
984 PyErr_NormalizeException(&exception2, &v2, &tb2);
985 if (Py_FlushLine())
986 PyErr_Clear();
987 fflush(stdout);
988 PySys_WriteStderr("Error in sys.excepthook:\n");
989 PyErr_Display(exception2, v2, tb2);
990 PySys_WriteStderr("\nOriginal exception was:\n");
991 PyErr_Display(exception, v, tb);
992 Py_XDECREF(exception2);
993 Py_XDECREF(v2);
994 Py_XDECREF(tb2);
996 Py_XDECREF(result);
997 Py_XDECREF(args);
998 } else {
999 PySys_WriteStderr("sys.excepthook is missing\n");
1000 PyErr_Display(exception, v, tb);
1002 Py_XDECREF(exception);
1003 Py_XDECREF(v);
1004 Py_XDECREF(tb);
1007 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1009 int err = 0;
1010 PyObject *v = value;
1011 PyObject *f = PySys_GetObject("stderr");
1012 if (f == NULL)
1013 fprintf(stderr, "lost sys.stderr\n");
1014 else {
1015 if (Py_FlushLine())
1016 PyErr_Clear();
1017 fflush(stdout);
1018 if (tb && tb != Py_None)
1019 err = PyTraceBack_Print(tb, f);
1020 if (err == 0 &&
1021 PyObject_HasAttrString(v, "print_file_and_line"))
1023 PyObject *message;
1024 const char *filename, *text;
1025 int lineno, offset;
1026 if (!parse_syntax_error(v, &message, &filename,
1027 &lineno, &offset, &text))
1028 PyErr_Clear();
1029 else {
1030 char buf[10];
1031 PyFile_WriteString(" File \"", f);
1032 if (filename == NULL)
1033 PyFile_WriteString("<string>", f);
1034 else
1035 PyFile_WriteString(filename, f);
1036 PyFile_WriteString("\", line ", f);
1037 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1038 PyFile_WriteString(buf, f);
1039 PyFile_WriteString("\n", f);
1040 if (text != NULL)
1041 print_error_text(f, offset, text);
1042 v = message;
1043 /* Can't be bothered to check all those
1044 PyFile_WriteString() calls */
1045 if (PyErr_Occurred())
1046 err = -1;
1049 if (err) {
1050 /* Don't do anything else */
1052 else if (PyClass_Check(exception)) {
1053 PyClassObject* exc = (PyClassObject*)exception;
1054 PyObject* className = exc->cl_name;
1055 PyObject* moduleName =
1056 PyDict_GetItemString(exc->cl_dict, "__module__");
1058 if (moduleName == NULL)
1059 err = PyFile_WriteString("<unknown>", f);
1060 else {
1061 char* modstr = PyString_AsString(moduleName);
1062 if (modstr && strcmp(modstr, "exceptions"))
1064 err = PyFile_WriteString(modstr, f);
1065 err += PyFile_WriteString(".", f);
1068 if (err == 0) {
1069 if (className == NULL)
1070 err = PyFile_WriteString("<unknown>", f);
1071 else
1072 err = PyFile_WriteObject(className, f,
1073 Py_PRINT_RAW);
1076 else
1077 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1078 if (err == 0) {
1079 if (v != NULL && v != Py_None) {
1080 PyObject *s = PyObject_Str(v);
1081 /* only print colon if the str() of the
1082 object is not the empty string
1084 if (s == NULL)
1085 err = -1;
1086 else if (!PyString_Check(s) ||
1087 PyString_GET_SIZE(s) != 0)
1088 err = PyFile_WriteString(": ", f);
1089 if (err == 0)
1090 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1091 Py_XDECREF(s);
1094 if (err == 0)
1095 err = PyFile_WriteString("\n", f);
1097 /* If an error happened here, don't show it.
1098 XXX This is wrong, but too many callers rely on this behavior. */
1099 if (err != 0)
1100 PyErr_Clear();
1103 PyObject *
1104 PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
1106 return run_err_node(PyParser_SimpleParseString(str, start),
1107 "<string>", globals, locals, NULL);
1110 PyObject *
1111 PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
1112 PyObject *locals)
1114 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
1117 PyObject *
1118 PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
1119 PyObject *locals, int closeit)
1121 node *n = PyParser_SimpleParseFile(fp, filename, start);
1122 if (closeit)
1123 fclose(fp);
1124 return run_err_node(n, filename, globals, locals, NULL);
1127 PyObject *
1128 PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
1129 PyCompilerFlags *flags)
1131 return run_err_node(PyParser_SimpleParseStringFlags(
1132 str, start, PARSER_FLAGS(flags)),
1133 "<string>", globals, locals, flags);
1136 PyObject *
1137 PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1138 PyObject *locals, PyCompilerFlags *flags)
1140 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1141 flags);
1144 PyObject *
1145 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1146 PyObject *locals, int closeit, PyCompilerFlags *flags)
1148 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
1149 PARSER_FLAGS(flags));
1150 if (closeit)
1151 fclose(fp);
1152 return run_err_node(n, filename, globals, locals, flags);
1155 static PyObject *
1156 run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
1157 PyCompilerFlags *flags)
1159 if (n == NULL)
1160 return NULL;
1161 return run_node(n, filename, globals, locals, flags);
1164 static PyObject *
1165 run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
1166 PyCompilerFlags *flags)
1168 PyCodeObject *co;
1169 PyObject *v;
1170 co = PyNode_CompileFlags(n, filename, flags);
1171 PyNode_Free(n);
1172 if (co == NULL)
1173 return NULL;
1174 v = PyEval_EvalCode(co, globals, locals);
1175 Py_DECREF(co);
1176 return v;
1179 static PyObject *
1180 run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
1181 PyCompilerFlags *flags)
1183 PyCodeObject *co;
1184 PyObject *v;
1185 long magic;
1186 long PyImport_GetMagicNumber(void);
1188 magic = PyMarshal_ReadLongFromFile(fp);
1189 if (magic != PyImport_GetMagicNumber()) {
1190 PyErr_SetString(PyExc_RuntimeError,
1191 "Bad magic number in .pyc file");
1192 return NULL;
1194 (void) PyMarshal_ReadLongFromFile(fp);
1195 v = PyMarshal_ReadLastObjectFromFile(fp);
1196 fclose(fp);
1197 if (v == NULL || !PyCode_Check(v)) {
1198 Py_XDECREF(v);
1199 PyErr_SetString(PyExc_RuntimeError,
1200 "Bad code object in .pyc file");
1201 return NULL;
1203 co = (PyCodeObject *)v;
1204 v = PyEval_EvalCode(co, globals, locals);
1205 if (v && flags)
1206 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1207 Py_DECREF(co);
1208 return v;
1211 PyObject *
1212 Py_CompileString(const char *str, const char *filename, int start)
1214 return Py_CompileStringFlags(str, filename, start, NULL);
1217 PyObject *
1218 Py_CompileStringFlags(const char *str, const char *filename, int start,
1219 PyCompilerFlags *flags)
1221 node *n;
1222 PyCodeObject *co;
1224 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1225 PARSER_FLAGS(flags));
1226 if (n == NULL)
1227 return NULL;
1228 co = PyNode_CompileFlags(n, filename, flags);
1229 PyNode_Free(n);
1230 return (PyObject *)co;
1233 struct symtable *
1234 Py_SymtableString(const char *str, const char *filename, int start)
1236 node *n;
1237 struct symtable *st;
1238 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1239 start, 0);
1240 if (n == NULL)
1241 return NULL;
1242 st = PyNode_CompileSymtable(n, filename);
1243 PyNode_Free(n);
1244 return st;
1247 /* Simplified interface to parsefile -- return node or set exception */
1249 node *
1250 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1252 node *n;
1253 perrdetail err;
1254 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1255 (char *)0, (char *)0, &err, flags);
1256 if (n == NULL)
1257 err_input(&err);
1258 return n;
1261 node *
1262 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1264 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1267 /* Simplified interface to parsestring -- return node or set exception */
1269 node *
1270 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1272 node *n;
1273 perrdetail err;
1274 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1275 flags);
1276 if (n == NULL)
1277 err_input(&err);
1278 return n;
1281 node *
1282 PyParser_SimpleParseString(const char *str, int start)
1284 return PyParser_SimpleParseStringFlags(str, start, 0);
1287 node *
1288 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1289 int start, int flags)
1291 node *n;
1292 perrdetail err;
1294 n = PyParser_ParseStringFlagsFilename(str, filename,
1295 &_PyParser_Grammar,
1296 start, &err, flags);
1297 if (n == NULL)
1298 err_input(&err);
1299 return n;
1302 node *
1303 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1305 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1306 start, 0);
1309 /* May want to move a more generalized form of this to parsetok.c or
1310 even parser modules. */
1312 void
1313 PyParser_SetError(perrdetail *err)
1315 err_input(err);
1318 /* Set the error appropriate to the given input error code (see errcode.h) */
1320 static void
1321 err_input(perrdetail *err)
1323 PyObject *v, *w, *errtype;
1324 PyObject* u = NULL;
1325 char *msg = NULL;
1326 errtype = PyExc_SyntaxError;
1327 v = Py_BuildValue("(ziiz)", err->filename,
1328 err->lineno, err->offset, err->text);
1329 if (err->text != NULL) {
1330 PyMem_DEL(err->text);
1331 err->text = NULL;
1333 switch (err->error) {
1334 case E_SYNTAX:
1335 errtype = PyExc_IndentationError;
1336 if (err->expected == INDENT)
1337 msg = "expected an indented block";
1338 else if (err->token == INDENT)
1339 msg = "unexpected indent";
1340 else if (err->token == DEDENT)
1341 msg = "unexpected unindent";
1342 else {
1343 errtype = PyExc_SyntaxError;
1344 msg = "invalid syntax";
1346 break;
1347 case E_TOKEN:
1348 msg = "invalid token";
1349 break;
1350 case E_EOFS:
1351 msg = "EOF while scanning triple-quoted string";
1352 break;
1353 case E_EOLS:
1354 msg = "EOL while scanning single-quoted string";
1355 break;
1356 case E_INTR:
1357 PyErr_SetNone(PyExc_KeyboardInterrupt);
1358 Py_XDECREF(v);
1359 return;
1360 case E_NOMEM:
1361 PyErr_NoMemory();
1362 Py_XDECREF(v);
1363 return;
1364 case E_EOF:
1365 msg = "unexpected EOF while parsing";
1366 break;
1367 case E_TABSPACE:
1368 errtype = PyExc_TabError;
1369 msg = "inconsistent use of tabs and spaces in indentation";
1370 break;
1371 case E_OVERFLOW:
1372 msg = "expression too long";
1373 break;
1374 case E_DEDENT:
1375 errtype = PyExc_IndentationError;
1376 msg = "unindent does not match any outer indentation level";
1377 break;
1378 case E_TOODEEP:
1379 errtype = PyExc_IndentationError;
1380 msg = "too many levels of indentation";
1381 break;
1382 case E_DECODE: { /* XXX */
1383 PyThreadState* tstate = PyThreadState_Get();
1384 PyObject* value = tstate->curexc_value;
1385 if (value != NULL) {
1386 u = PyObject_Repr(value);
1387 if (u != NULL) {
1388 msg = PyString_AsString(u);
1389 break;
1393 default:
1394 fprintf(stderr, "error=%d\n", err->error);
1395 msg = "unknown parsing error";
1396 break;
1398 w = Py_BuildValue("(sO)", msg, v);
1399 Py_XDECREF(u);
1400 Py_XDECREF(v);
1401 PyErr_SetObject(errtype, w);
1402 Py_XDECREF(w);
1405 /* Print fatal error message and abort */
1407 void
1408 Py_FatalError(const char *msg)
1410 fprintf(stderr, "Fatal Python error: %s\n", msg);
1411 #ifdef MS_WINDOWS
1412 OutputDebugString("Fatal Python error: ");
1413 OutputDebugString(msg);
1414 OutputDebugString("\n");
1415 #ifdef _DEBUG
1416 DebugBreak();
1417 #endif
1418 #endif /* MS_WINDOWS */
1419 abort();
1422 /* Clean up and exit */
1424 #ifdef WITH_THREAD
1425 #include "pythread.h"
1426 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
1427 #endif
1429 #define NEXITFUNCS 32
1430 static void (*exitfuncs[NEXITFUNCS])(void);
1431 static int nexitfuncs = 0;
1433 int Py_AtExit(void (*func)(void))
1435 if (nexitfuncs >= NEXITFUNCS)
1436 return -1;
1437 exitfuncs[nexitfuncs++] = func;
1438 return 0;
1441 static void
1442 call_sys_exitfunc(void)
1444 PyObject *exitfunc = PySys_GetObject("exitfunc");
1446 if (exitfunc) {
1447 PyObject *res;
1448 Py_INCREF(exitfunc);
1449 PySys_SetObject("exitfunc", (PyObject *)NULL);
1450 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1451 if (res == NULL) {
1452 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1453 PySys_WriteStderr("Error in sys.exitfunc:\n");
1455 PyErr_Print();
1457 Py_DECREF(exitfunc);
1460 if (Py_FlushLine())
1461 PyErr_Clear();
1464 static void
1465 call_ll_exitfuncs(void)
1467 while (nexitfuncs > 0)
1468 (*exitfuncs[--nexitfuncs])();
1470 fflush(stdout);
1471 fflush(stderr);
1474 void
1475 Py_Exit(int sts)
1477 Py_Finalize();
1479 #ifdef macintosh
1480 PyMac_Exit(sts);
1481 #else
1482 exit(sts);
1483 #endif
1486 static void
1487 initsigs(void)
1489 #ifdef HAVE_SIGNAL_H
1490 #ifdef SIGPIPE
1491 signal(SIGPIPE, SIG_IGN);
1492 #endif
1493 #ifdef SIGXFZ
1494 signal(SIGXFZ, SIG_IGN);
1495 #endif
1496 #ifdef SIGXFSZ
1497 signal(SIGXFSZ, SIG_IGN);
1498 #endif
1499 #endif /* HAVE_SIGNAL_H */
1500 PyOS_InitInterrupts(); /* May imply initsignal() */
1503 #ifdef MPW
1505 /* Check for file descriptor connected to interactive device.
1506 Pretend that stdin is always interactive, other files never. */
1509 isatty(int fd)
1511 return fd == fileno(stdin);
1514 #endif
1517 * The file descriptor fd is considered ``interactive'' if either
1518 * a) isatty(fd) is TRUE, or
1519 * b) the -i flag was given, and the filename associated with
1520 * the descriptor is NULL or "<stdin>" or "???".
1523 Py_FdIsInteractive(FILE *fp, const char *filename)
1525 if (isatty((int)fileno(fp)))
1526 return 1;
1527 if (!Py_InteractiveFlag)
1528 return 0;
1529 return (filename == NULL) ||
1530 (strcmp(filename, "<stdin>") == 0) ||
1531 (strcmp(filename, "???") == 0);
1535 #if defined(USE_STACKCHECK)
1536 #if defined(WIN32) && defined(_MSC_VER)
1538 /* Stack checking for Microsoft C */
1540 #include <malloc.h>
1541 #include <excpt.h>
1544 * Return non-zero when we run out of memory on the stack; zero otherwise.
1547 PyOS_CheckStack(void)
1549 __try {
1550 /* alloca throws a stack overflow exception if there's
1551 not enough space left on the stack */
1552 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1553 return 0;
1554 } __except (EXCEPTION_EXECUTE_HANDLER) {
1555 /* just ignore all errors */
1557 return 1;
1560 #endif /* WIN32 && _MSC_VER */
1562 /* Alternate implementations can be added here... */
1564 #endif /* USE_STACKCHECK */
1567 /* Wrappers around sigaction() or signal(). */
1569 PyOS_sighandler_t
1570 PyOS_getsig(int sig)
1572 #ifdef HAVE_SIGACTION
1573 struct sigaction context;
1574 /* Initialize context.sa_handler to SIG_ERR which makes about as
1575 * much sense as anything else. It should get overwritten if
1576 * sigaction actually succeeds and otherwise we avoid an
1577 * uninitialized memory read.
1579 context.sa_handler = SIG_ERR;
1580 sigaction(sig, NULL, &context);
1581 return context.sa_handler;
1582 #else
1583 PyOS_sighandler_t handler;
1584 handler = signal(sig, SIG_IGN);
1585 signal(sig, handler);
1586 return handler;
1587 #endif
1590 PyOS_sighandler_t
1591 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1593 #ifdef HAVE_SIGACTION
1594 struct sigaction context;
1595 PyOS_sighandler_t oldhandler;
1596 /* Initialize context.sa_handler to SIG_ERR which makes about as
1597 * much sense as anything else. It should get overwritten if
1598 * sigaction actually succeeds and otherwise we avoid an
1599 * uninitialized memory read.
1601 context.sa_handler = SIG_ERR;
1602 sigaction(sig, NULL, &context);
1603 oldhandler = context.sa_handler;
1604 context.sa_handler = handler;
1605 sigaction(sig, &context, NULL);
1606 return oldhandler;
1607 #else
1608 return signal(sig, handler);
1609 #endif