Set eol-style to native -- doesn't appear to be any reason
[python.git] / Python / pythonrun.c
blob1b2b829c3d61cb0d044f334368e9a3c7ea0b6bab
2 /* Python interpreter top-level routines, including init/exit */
4 #include "Python.h"
6 #include "Python-ast.h"
7 #include "grammar.h"
8 #include "node.h"
9 #include "token.h"
10 #include "parsetok.h"
11 #include "errcode.h"
12 #include "code.h"
13 #include "compile.h"
14 #include "symtable.h"
15 #include "pyarena.h"
16 #include "ast.h"
17 #include "eval.h"
18 #include "marshal.h"
20 #include <signal.h>
22 #ifdef HAVE_LANGINFO_H
23 #include <locale.h>
24 #include <langinfo.h>
25 #endif
27 #ifdef MS_WINDOWS
28 #undef BYTE
29 #include "windows.h"
30 #endif
32 extern char *Py_GetPath(void);
34 extern grammar _PyParser_Grammar; /* From graminit.c */
36 /* Forward */
37 static void initmain(void);
38 static void initsite(void);
39 static PyObject *run_err_mod(mod_ty, const char *, PyObject *, PyObject *,
40 PyCompilerFlags *, PyArena *arena);
41 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
42 PyCompilerFlags *, PyArena *);
43 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
44 PyCompilerFlags *);
45 static void err_input(perrdetail *);
46 static void initsigs(void);
47 static void call_sys_exitfunc(void);
48 static void call_ll_exitfuncs(void);
49 extern void _PyUnicode_Init(void);
50 extern void _PyUnicode_Fini(void);
52 #ifdef WITH_THREAD
53 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
54 extern void _PyGILState_Fini(void);
55 #endif /* WITH_THREAD */
57 int Py_DebugFlag; /* Needed by parser.c */
58 int Py_VerboseFlag; /* Needed by import.c */
59 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
60 int Py_NoSiteFlag; /* Suppress 'import site' */
61 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
62 int Py_FrozenFlag; /* Needed by getpath.c */
63 int Py_UnicodeFlag = 0; /* Needed by compile.c */
64 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
65 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
66 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
67 true divisions (which they will be in 2.3). */
68 int _Py_QnewFlag = 0;
70 /* Reference to 'warnings' module, to avoid importing it
71 on the fly when the import lock may be held. See 683658/771097
73 static PyObject *warnings_module = NULL;
75 /* Returns a borrowed reference to the 'warnings' module, or NULL.
76 If the module is returned, it is guaranteed to have been obtained
77 without acquiring the import lock
79 PyObject *PyModule_GetWarningsModule(void)
81 PyObject *typ, *val, *tb;
82 PyObject *all_modules;
83 /* If we managed to get the module at init time, just use it */
84 if (warnings_module)
85 return warnings_module;
86 /* If it wasn't available at init time, it may be available
87 now in sys.modules (common scenario is frozen apps: import
88 at init time fails, but the frozen init code sets up sys.path
89 correctly, then does an implicit import of warnings for us
91 /* Save and restore any exceptions */
92 PyErr_Fetch(&typ, &val, &tb);
94 all_modules = PySys_GetObject("modules");
95 if (all_modules) {
96 warnings_module = PyDict_GetItemString(all_modules, "warnings");
97 /* We keep a ref in the global */
98 Py_XINCREF(warnings_module);
100 PyErr_Restore(typ, val, tb);
101 return warnings_module;
104 static int initialized = 0;
106 /* API to access the initialized flag -- useful for esoteric use */
109 Py_IsInitialized(void)
111 return initialized;
114 /* Global initializations. Can be undone by Py_Finalize(). Don't
115 call this twice without an intervening Py_Finalize() call. When
116 initializations fail, a fatal error is issued and the function does
117 not return. On return, the first thread and interpreter state have
118 been created.
120 Locking: you must hold the interpreter lock while calling this.
121 (If the lock has not yet been initialized, that's equivalent to
122 having the lock, but you cannot use multiple threads.)
126 static int
127 add_flag(int flag, const char *envs)
129 int env = atoi(envs);
130 if (flag < env)
131 flag = env;
132 if (flag < 1)
133 flag = 1;
134 return flag;
137 void
138 Py_InitializeEx(int install_sigs)
140 PyInterpreterState *interp;
141 PyThreadState *tstate;
142 PyObject *bimod, *sysmod;
143 char *p;
144 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
145 char *codeset;
146 char *saved_locale;
147 PyObject *sys_stream, *sys_isatty;
148 #endif
149 extern void _Py_ReadyTypes(void);
151 if (initialized)
152 return;
153 initialized = 1;
155 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
156 Py_DebugFlag = add_flag(Py_DebugFlag, p);
157 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
158 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
159 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
160 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
162 interp = PyInterpreterState_New();
163 if (interp == NULL)
164 Py_FatalError("Py_Initialize: can't make first interpreter");
166 tstate = PyThreadState_New(interp);
167 if (tstate == NULL)
168 Py_FatalError("Py_Initialize: can't make first thread");
169 (void) PyThreadState_Swap(tstate);
171 _Py_ReadyTypes();
173 if (!_PyFrame_Init())
174 Py_FatalError("Py_Initialize: can't init frames");
176 if (!_PyInt_Init())
177 Py_FatalError("Py_Initialize: can't init ints");
179 _PyFloat_Init();
181 interp->modules = PyDict_New();
182 if (interp->modules == NULL)
183 Py_FatalError("Py_Initialize: can't make modules dictionary");
185 #ifdef Py_USING_UNICODE
186 /* Init Unicode implementation; relies on the codec registry */
187 _PyUnicode_Init();
188 #endif
190 bimod = _PyBuiltin_Init();
191 if (bimod == NULL)
192 Py_FatalError("Py_Initialize: can't initialize __builtin__");
193 interp->builtins = PyModule_GetDict(bimod);
194 Py_INCREF(interp->builtins);
196 sysmod = _PySys_Init();
197 if (sysmod == NULL)
198 Py_FatalError("Py_Initialize: can't initialize sys");
199 interp->sysdict = PyModule_GetDict(sysmod);
200 Py_INCREF(interp->sysdict);
201 _PyImport_FixupExtension("sys", "sys");
202 PySys_SetPath(Py_GetPath());
203 PyDict_SetItemString(interp->sysdict, "modules",
204 interp->modules);
206 _PyImport_Init();
208 /* initialize builtin exceptions */
209 _PyExc_Init();
210 _PyImport_FixupExtension("exceptions", "exceptions");
212 /* phase 2 of builtins */
213 _PyImport_FixupExtension("__builtin__", "__builtin__");
215 _PyImportHooks_Init();
217 if (install_sigs)
218 initsigs(); /* Signal handling stuff, including initintr() */
220 initmain(); /* Module __main__ */
221 if (!Py_NoSiteFlag)
222 initsite(); /* Module site */
224 /* auto-thread-state API, if available */
225 #ifdef WITH_THREAD
226 _PyGILState_Init(interp, tstate);
227 #endif /* WITH_THREAD */
229 warnings_module = PyImport_ImportModule("warnings");
230 if (!warnings_module)
231 PyErr_Clear();
233 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
234 /* On Unix, set the file system encoding according to the
235 user's preference, if the CODESET names a well-known
236 Python codec, and Py_FileSystemDefaultEncoding isn't
237 initialized by other means. Also set the encoding of
238 stdin and stdout if these are terminals. */
240 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
241 setlocale(LC_CTYPE, "");
242 codeset = nl_langinfo(CODESET);
243 if (codeset && *codeset) {
244 PyObject *enc = PyCodec_Encoder(codeset);
245 if (enc) {
246 codeset = strdup(codeset);
247 Py_DECREF(enc);
248 } else {
249 codeset = NULL;
250 PyErr_Clear();
252 } else
253 codeset = NULL;
254 setlocale(LC_CTYPE, saved_locale);
255 free(saved_locale);
257 if (codeset) {
258 sys_stream = PySys_GetObject("stdin");
259 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
260 if (!sys_isatty)
261 PyErr_Clear();
262 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
263 if (!PyFile_SetEncoding(sys_stream, codeset))
264 Py_FatalError("Cannot set codeset of stdin");
266 Py_XDECREF(sys_isatty);
268 sys_stream = PySys_GetObject("stdout");
269 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
270 if (!sys_isatty)
271 PyErr_Clear();
272 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
273 if (!PyFile_SetEncoding(sys_stream, codeset))
274 Py_FatalError("Cannot set codeset of stdout");
276 Py_XDECREF(sys_isatty);
278 if (!Py_FileSystemDefaultEncoding)
279 Py_FileSystemDefaultEncoding = codeset;
280 else
281 free(codeset);
283 #endif
286 void
287 Py_Initialize(void)
289 Py_InitializeEx(1);
293 #ifdef COUNT_ALLOCS
294 extern void dump_counts(void);
295 #endif
297 /* Undo the effect of Py_Initialize().
299 Beware: if multiple interpreter and/or thread states exist, these
300 are not wiped out; only the current thread and interpreter state
301 are deleted. But since everything else is deleted, those other
302 interpreter and thread states should no longer be used.
304 (XXX We should do better, e.g. wipe out all interpreters and
305 threads.)
307 Locking: as above.
311 void
312 Py_Finalize(void)
314 PyInterpreterState *interp;
315 PyThreadState *tstate;
317 if (!initialized)
318 return;
320 /* The interpreter is still entirely intact at this point, and the
321 * exit funcs may be relying on that. In particular, if some thread
322 * or exit func is still waiting to do an import, the import machinery
323 * expects Py_IsInitialized() to return true. So don't say the
324 * interpreter is uninitialized until after the exit funcs have run.
325 * Note that Threading.py uses an exit func to do a join on all the
326 * threads created thru it, so this also protects pending imports in
327 * the threads created via Threading.
329 call_sys_exitfunc();
330 initialized = 0;
332 /* Get current thread state and interpreter pointer */
333 tstate = PyThreadState_GET();
334 interp = tstate->interp;
336 /* Disable signal handling */
337 PyOS_FiniInterrupts();
339 /* drop module references we saved */
340 Py_XDECREF(warnings_module);
341 warnings_module = NULL;
343 /* Collect garbage. This may call finalizers; it's nice to call these
344 * before all modules are destroyed.
345 * XXX If a __del__ or weakref callback is triggered here, and tries to
346 * XXX import a module, bad things can happen, because Python no
347 * XXX longer believes it's initialized.
348 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
349 * XXX is easy to provoke that way. I've also seen, e.g.,
350 * XXX Exception exceptions.ImportError: 'No module named sha'
351 * XXX in <function callback at 0x008F5718> ignored
352 * XXX but I'm unclear on exactly how that one happens. In any case,
353 * XXX I haven't seen a real-life report of either of these.
355 PyGC_Collect();
357 /* Destroy all modules */
358 PyImport_Cleanup();
360 /* Collect final garbage. This disposes of cycles created by
361 * new-style class definitions, for example.
362 * XXX This is disabled because it caused too many problems. If
363 * XXX a __del__ or weakref callback triggers here, Python code has
364 * XXX a hard time running, because even the sys module has been
365 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
366 * XXX One symptom is a sequence of information-free messages
367 * XXX coming from threads (if a __del__ or callback is invoked,
368 * XXX other threads can execute too, and any exception they encounter
369 * XXX triggers a comedy of errors as subsystem after subsystem
370 * XXX fails to find what it *expects* to find in sys to help report
371 * XXX the exception and consequent unexpected failures). I've also
372 * XXX seen segfaults then, after adding print statements to the
373 * XXX Python code getting called.
375 #if 0
376 PyGC_Collect();
377 #endif
379 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
380 _PyImport_Fini();
382 /* Debugging stuff */
383 #ifdef COUNT_ALLOCS
384 dump_counts();
385 #endif
387 #ifdef Py_REF_DEBUG
388 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
389 #endif
391 #ifdef Py_TRACE_REFS
392 /* Display all objects still alive -- this can invoke arbitrary
393 * __repr__ overrides, so requires a mostly-intact interpreter.
394 * Alas, a lot of stuff may still be alive now that will be cleaned
395 * up later.
397 if (Py_GETENV("PYTHONDUMPREFS"))
398 _Py_PrintReferences(stderr);
399 #endif /* Py_TRACE_REFS */
401 /* Cleanup auto-thread-state */
402 #ifdef WITH_THREAD
403 _PyGILState_Fini();
404 #endif /* WITH_THREAD */
406 /* Clear interpreter state */
407 PyInterpreterState_Clear(interp);
409 /* Now we decref the exception classes. After this point nothing
410 can raise an exception. That's okay, because each Fini() method
411 below has been checked to make sure no exceptions are ever
412 raised.
415 _PyExc_Fini();
417 /* Delete current thread */
418 PyThreadState_Swap(NULL);
419 PyInterpreterState_Delete(interp);
421 /* Sundry finalizers */
422 PyMethod_Fini();
423 PyFrame_Fini();
424 PyCFunction_Fini();
425 PyTuple_Fini();
426 PyList_Fini();
427 PySet_Fini();
428 PyString_Fini();
429 PyInt_Fini();
430 PyFloat_Fini();
432 #ifdef Py_USING_UNICODE
433 /* Cleanup Unicode implementation */
434 _PyUnicode_Fini();
435 #endif
437 /* XXX Still allocated:
438 - various static ad-hoc pointers to interned strings
439 - int and float free list blocks
440 - whatever various modules and libraries allocate
443 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
445 #ifdef Py_TRACE_REFS
446 /* Display addresses (& refcnts) of all objects still alive.
447 * An address can be used to find the repr of the object, printed
448 * above by _Py_PrintReferences.
450 if (Py_GETENV("PYTHONDUMPREFS"))
451 _Py_PrintReferenceAddresses(stderr);
452 #endif /* Py_TRACE_REFS */
453 #ifdef PYMALLOC_DEBUG
454 if (Py_GETENV("PYTHONMALLOCSTATS"))
455 _PyObject_DebugMallocStats();
456 #endif
458 call_ll_exitfuncs();
461 /* Create and initialize a new interpreter and thread, and return the
462 new thread. This requires that Py_Initialize() has been called
463 first.
465 Unsuccessful initialization yields a NULL pointer. Note that *no*
466 exception information is available even in this case -- the
467 exception information is held in the thread, and there is no
468 thread.
470 Locking: as above.
474 PyThreadState *
475 Py_NewInterpreter(void)
477 PyInterpreterState *interp;
478 PyThreadState *tstate, *save_tstate;
479 PyObject *bimod, *sysmod;
481 if (!initialized)
482 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
484 interp = PyInterpreterState_New();
485 if (interp == NULL)
486 return NULL;
488 tstate = PyThreadState_New(interp);
489 if (tstate == NULL) {
490 PyInterpreterState_Delete(interp);
491 return NULL;
494 save_tstate = PyThreadState_Swap(tstate);
496 /* XXX The following is lax in error checking */
498 interp->modules = PyDict_New();
500 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
501 if (bimod != NULL) {
502 interp->builtins = PyModule_GetDict(bimod);
503 Py_INCREF(interp->builtins);
505 sysmod = _PyImport_FindExtension("sys", "sys");
506 if (bimod != NULL && sysmod != NULL) {
507 interp->sysdict = PyModule_GetDict(sysmod);
508 Py_INCREF(interp->sysdict);
509 PySys_SetPath(Py_GetPath());
510 PyDict_SetItemString(interp->sysdict, "modules",
511 interp->modules);
512 _PyImportHooks_Init();
513 initmain();
514 if (!Py_NoSiteFlag)
515 initsite();
518 if (!PyErr_Occurred())
519 return tstate;
521 /* Oops, it didn't work. Undo it all. */
523 PyErr_Print();
524 PyThreadState_Clear(tstate);
525 PyThreadState_Swap(save_tstate);
526 PyThreadState_Delete(tstate);
527 PyInterpreterState_Delete(interp);
529 return NULL;
532 /* Delete an interpreter and its last thread. This requires that the
533 given thread state is current, that the thread has no remaining
534 frames, and that it is its interpreter's only remaining thread.
535 It is a fatal error to violate these constraints.
537 (Py_Finalize() doesn't have these constraints -- it zaps
538 everything, regardless.)
540 Locking: as above.
544 void
545 Py_EndInterpreter(PyThreadState *tstate)
547 PyInterpreterState *interp = tstate->interp;
549 if (tstate != PyThreadState_GET())
550 Py_FatalError("Py_EndInterpreter: thread is not current");
551 if (tstate->frame != NULL)
552 Py_FatalError("Py_EndInterpreter: thread still has a frame");
553 if (tstate != interp->tstate_head || tstate->next != NULL)
554 Py_FatalError("Py_EndInterpreter: not the last thread");
556 PyImport_Cleanup();
557 PyInterpreterState_Clear(interp);
558 PyThreadState_Swap(NULL);
559 PyInterpreterState_Delete(interp);
562 static char *progname = "python";
564 void
565 Py_SetProgramName(char *pn)
567 if (pn && *pn)
568 progname = pn;
571 char *
572 Py_GetProgramName(void)
574 return progname;
577 static char *default_home = NULL;
579 void
580 Py_SetPythonHome(char *home)
582 default_home = home;
585 char *
586 Py_GetPythonHome(void)
588 char *home = default_home;
589 if (home == NULL && !Py_IgnoreEnvironmentFlag)
590 home = Py_GETENV("PYTHONHOME");
591 return home;
594 /* Create __main__ module */
596 static void
597 initmain(void)
599 PyObject *m, *d;
600 m = PyImport_AddModule("__main__");
601 if (m == NULL)
602 Py_FatalError("can't create __main__ module");
603 d = PyModule_GetDict(m);
604 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
605 PyObject *bimod = PyImport_ImportModule("__builtin__");
606 if (bimod == NULL ||
607 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
608 Py_FatalError("can't add __builtins__ to __main__");
609 Py_DECREF(bimod);
613 /* Import the site module (not into __main__ though) */
615 static void
616 initsite(void)
618 PyObject *m, *f;
619 m = PyImport_ImportModule("site");
620 if (m == NULL) {
621 f = PySys_GetObject("stderr");
622 if (Py_VerboseFlag) {
623 PyFile_WriteString(
624 "'import site' failed; traceback:\n", f);
625 PyErr_Print();
627 else {
628 PyFile_WriteString(
629 "'import site' failed; use -v for traceback\n", f);
630 PyErr_Clear();
633 else {
634 Py_DECREF(m);
638 /* Parse input from a file and execute it */
641 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
642 PyCompilerFlags *flags)
644 if (filename == NULL)
645 filename = "???";
646 if (Py_FdIsInteractive(fp, filename)) {
647 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
648 if (closeit)
649 fclose(fp);
650 return err;
652 else
653 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
657 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
659 PyObject *v;
660 int ret;
661 PyCompilerFlags local_flags;
663 if (flags == NULL) {
664 flags = &local_flags;
665 local_flags.cf_flags = 0;
667 v = PySys_GetObject("ps1");
668 if (v == NULL) {
669 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
670 Py_XDECREF(v);
672 v = PySys_GetObject("ps2");
673 if (v == NULL) {
674 PySys_SetObject("ps2", v = PyString_FromString("... "));
675 Py_XDECREF(v);
677 for (;;) {
678 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
679 #ifdef Py_REF_DEBUG
680 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
681 #endif
682 if (ret == E_EOF)
683 return 0;
685 if (ret == E_NOMEM)
686 return -1;
691 /* compute parser flags based on compiler flags */
692 #define PARSER_FLAGS(flags) \
693 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
694 PyPARSE_DONT_IMPLY_DEDENT : 0) \
695 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
696 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
699 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
701 PyObject *m, *d, *v, *w;
702 mod_ty mod;
703 PyArena *arena;
704 char *ps1 = "", *ps2 = "";
705 int errcode = 0;
707 v = PySys_GetObject("ps1");
708 if (v != NULL) {
709 v = PyObject_Str(v);
710 if (v == NULL)
711 PyErr_Clear();
712 else if (PyString_Check(v))
713 ps1 = PyString_AsString(v);
715 w = PySys_GetObject("ps2");
716 if (w != NULL) {
717 w = PyObject_Str(w);
718 if (w == NULL)
719 PyErr_Clear();
720 else if (PyString_Check(w))
721 ps2 = PyString_AsString(w);
723 arena = PyArena_New();
724 mod = PyParser_ASTFromFile(fp, filename,
725 Py_single_input, ps1, ps2,
726 flags, &errcode, arena);
727 Py_XDECREF(v);
728 Py_XDECREF(w);
729 if (mod == NULL) {
730 PyArena_Free(arena);
731 if (errcode == E_EOF) {
732 PyErr_Clear();
733 return E_EOF;
735 PyErr_Print();
736 return -1;
738 m = PyImport_AddModule("__main__");
739 if (m == NULL) {
740 PyArena_Free(arena);
741 return -1;
743 d = PyModule_GetDict(m);
744 v = run_mod(mod, filename, d, d, flags, arena);
745 PyArena_Free(arena);
746 if (v == NULL) {
747 PyErr_Print();
748 return -1;
750 Py_DECREF(v);
751 if (Py_FlushLine())
752 PyErr_Clear();
753 return 0;
756 /* Check whether a file maybe a pyc file: Look at the extension,
757 the file type, and, if we may close it, at the first few bytes. */
759 static int
760 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
762 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
763 return 1;
765 /* Only look into the file if we are allowed to close it, since
766 it then should also be seekable. */
767 if (closeit) {
768 /* Read only two bytes of the magic. If the file was opened in
769 text mode, the bytes 3 and 4 of the magic (\r\n) might not
770 be read as they are on disk. */
771 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
772 unsigned char buf[2];
773 /* Mess: In case of -x, the stream is NOT at its start now,
774 and ungetc() was used to push back the first newline,
775 which makes the current stream position formally undefined,
776 and a x-platform nightmare.
777 Unfortunately, we have no direct way to know whether -x
778 was specified. So we use a terrible hack: if the current
779 stream position is not 0, we assume -x was specified, and
780 give up. Bug 132850 on SourceForge spells out the
781 hopelessness of trying anything else (fseek and ftell
782 don't work predictably x-platform for text-mode files).
784 int ispyc = 0;
785 if (ftell(fp) == 0) {
786 if (fread(buf, 1, 2, fp) == 2 &&
787 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
788 ispyc = 1;
789 rewind(fp);
791 return ispyc;
793 return 0;
797 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
798 PyCompilerFlags *flags)
800 PyObject *m, *d, *v;
801 const char *ext;
803 m = PyImport_AddModule("__main__");
804 if (m == NULL)
805 return -1;
806 d = PyModule_GetDict(m);
807 if (PyDict_GetItemString(d, "__file__") == NULL) {
808 PyObject *f = PyString_FromString(filename);
809 if (f == NULL)
810 return -1;
811 if (PyDict_SetItemString(d, "__file__", f) < 0) {
812 Py_DECREF(f);
813 return -1;
815 Py_DECREF(f);
817 ext = filename + strlen(filename) - 4;
818 if (maybe_pyc_file(fp, filename, ext, closeit)) {
819 /* Try to run a pyc file. First, re-open in binary */
820 if (closeit)
821 fclose(fp);
822 if ((fp = fopen(filename, "rb")) == NULL) {
823 fprintf(stderr, "python: Can't reopen .pyc file\n");
824 return -1;
826 /* Turn on optimization if a .pyo file is given */
827 if (strcmp(ext, ".pyo") == 0)
828 Py_OptimizeFlag = 1;
829 v = run_pyc_file(fp, filename, d, d, flags);
830 } else {
831 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
832 closeit, flags);
834 if (v == NULL) {
835 PyErr_Print();
836 return -1;
838 Py_DECREF(v);
839 if (Py_FlushLine())
840 PyErr_Clear();
841 return 0;
845 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
847 PyObject *m, *d, *v;
848 m = PyImport_AddModule("__main__");
849 if (m == NULL)
850 return -1;
851 d = PyModule_GetDict(m);
852 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
853 if (v == NULL) {
854 PyErr_Print();
855 return -1;
857 Py_DECREF(v);
858 if (Py_FlushLine())
859 PyErr_Clear();
860 return 0;
863 static int
864 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
865 int *lineno, int *offset, const char **text)
867 long hold;
868 PyObject *v;
870 /* old style errors */
871 if (PyTuple_Check(err))
872 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
873 lineno, offset, text);
875 /* new style errors. `err' is an instance */
877 if (! (v = PyObject_GetAttrString(err, "msg")))
878 goto finally;
879 *message = v;
881 if (!(v = PyObject_GetAttrString(err, "filename")))
882 goto finally;
883 if (v == Py_None)
884 *filename = NULL;
885 else if (! (*filename = PyString_AsString(v)))
886 goto finally;
888 Py_DECREF(v);
889 if (!(v = PyObject_GetAttrString(err, "lineno")))
890 goto finally;
891 hold = PyInt_AsLong(v);
892 Py_DECREF(v);
893 v = NULL;
894 if (hold < 0 && PyErr_Occurred())
895 goto finally;
896 *lineno = (int)hold;
898 if (!(v = PyObject_GetAttrString(err, "offset")))
899 goto finally;
900 if (v == Py_None) {
901 *offset = -1;
902 Py_DECREF(v);
903 v = NULL;
904 } else {
905 hold = PyInt_AsLong(v);
906 Py_DECREF(v);
907 v = NULL;
908 if (hold < 0 && PyErr_Occurred())
909 goto finally;
910 *offset = (int)hold;
913 if (!(v = PyObject_GetAttrString(err, "text")))
914 goto finally;
915 if (v == Py_None)
916 *text = NULL;
917 else if (! (*text = PyString_AsString(v)))
918 goto finally;
919 Py_DECREF(v);
920 return 1;
922 finally:
923 Py_XDECREF(v);
924 return 0;
927 void
928 PyErr_Print(void)
930 PyErr_PrintEx(1);
933 static void
934 print_error_text(PyObject *f, int offset, const char *text)
936 char *nl;
937 if (offset >= 0) {
938 if (offset > 0 && offset == (int)strlen(text))
939 offset--;
940 for (;;) {
941 nl = strchr(text, '\n');
942 if (nl == NULL || nl-text >= offset)
943 break;
944 offset -= (int)(nl+1-text);
945 text = nl+1;
947 while (*text == ' ' || *text == '\t') {
948 text++;
949 offset--;
952 PyFile_WriteString(" ", f);
953 PyFile_WriteString(text, f);
954 if (*text == '\0' || text[strlen(text)-1] != '\n')
955 PyFile_WriteString("\n", f);
956 if (offset == -1)
957 return;
958 PyFile_WriteString(" ", f);
959 offset--;
960 while (offset > 0) {
961 PyFile_WriteString(" ", f);
962 offset--;
964 PyFile_WriteString("^\n", f);
967 static void
968 handle_system_exit(void)
970 PyObject *exception, *value, *tb;
971 int exitcode = 0;
973 PyErr_Fetch(&exception, &value, &tb);
974 if (Py_FlushLine())
975 PyErr_Clear();
976 fflush(stdout);
977 if (value == NULL || value == Py_None)
978 goto done;
979 if (PyExceptionInstance_Check(value)) {
980 /* The error code should be in the `code' attribute. */
981 PyObject *code = PyObject_GetAttrString(value, "code");
982 if (code) {
983 Py_DECREF(value);
984 value = code;
985 if (value == Py_None)
986 goto done;
988 /* If we failed to dig out the 'code' attribute,
989 just let the else clause below print the error. */
991 if (PyInt_Check(value))
992 exitcode = (int)PyInt_AsLong(value);
993 else {
994 PyObject_Print(value, stderr, Py_PRINT_RAW);
995 PySys_WriteStderr("\n");
996 exitcode = 1;
998 done:
999 /* Restore and clear the exception info, in order to properly decref
1000 * the exception, value, and traceback. If we just exit instead,
1001 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1002 * some finalizers from running.
1004 PyErr_Restore(exception, value, tb);
1005 PyErr_Clear();
1006 Py_Exit(exitcode);
1007 /* NOTREACHED */
1010 void
1011 PyErr_PrintEx(int set_sys_last_vars)
1013 PyObject *exception, *v, *tb, *hook;
1015 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1016 handle_system_exit();
1018 PyErr_Fetch(&exception, &v, &tb);
1019 if (exception == NULL)
1020 return;
1021 PyErr_NormalizeException(&exception, &v, &tb);
1022 if (exception == NULL)
1023 return;
1024 if (set_sys_last_vars) {
1025 PySys_SetObject("last_type", exception);
1026 PySys_SetObject("last_value", v);
1027 PySys_SetObject("last_traceback", tb);
1029 hook = PySys_GetObject("excepthook");
1030 if (hook) {
1031 PyObject *args = PyTuple_Pack(3,
1032 exception, v ? v : Py_None, tb ? tb : Py_None);
1033 PyObject *result = PyEval_CallObject(hook, args);
1034 if (result == NULL) {
1035 PyObject *exception2, *v2, *tb2;
1036 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1037 handle_system_exit();
1039 PyErr_Fetch(&exception2, &v2, &tb2);
1040 PyErr_NormalizeException(&exception2, &v2, &tb2);
1041 if (Py_FlushLine())
1042 PyErr_Clear();
1043 fflush(stdout);
1044 PySys_WriteStderr("Error in sys.excepthook:\n");
1045 PyErr_Display(exception2, v2, tb2);
1046 PySys_WriteStderr("\nOriginal exception was:\n");
1047 PyErr_Display(exception, v, tb);
1048 Py_XDECREF(exception2);
1049 Py_XDECREF(v2);
1050 Py_XDECREF(tb2);
1052 Py_XDECREF(result);
1053 Py_XDECREF(args);
1054 } else {
1055 PySys_WriteStderr("sys.excepthook is missing\n");
1056 PyErr_Display(exception, v, tb);
1058 Py_XDECREF(exception);
1059 Py_XDECREF(v);
1060 Py_XDECREF(tb);
1063 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1065 int err = 0;
1066 PyObject *f = PySys_GetObject("stderr");
1067 Py_INCREF(value);
1068 if (f == NULL)
1069 fprintf(stderr, "lost sys.stderr\n");
1070 else {
1071 if (Py_FlushLine())
1072 PyErr_Clear();
1073 fflush(stdout);
1074 if (tb && tb != Py_None)
1075 err = PyTraceBack_Print(tb, f);
1076 if (err == 0 &&
1077 PyObject_HasAttrString(value, "print_file_and_line"))
1079 PyObject *message;
1080 const char *filename, *text;
1081 int lineno, offset;
1082 if (!parse_syntax_error(value, &message, &filename,
1083 &lineno, &offset, &text))
1084 PyErr_Clear();
1085 else {
1086 char buf[10];
1087 PyFile_WriteString(" File \"", f);
1088 if (filename == NULL)
1089 PyFile_WriteString("<string>", f);
1090 else
1091 PyFile_WriteString(filename, f);
1092 PyFile_WriteString("\", line ", f);
1093 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1094 PyFile_WriteString(buf, f);
1095 PyFile_WriteString("\n", f);
1096 if (text != NULL)
1097 print_error_text(f, offset, text);
1098 Py_DECREF(value);
1099 value = message;
1100 /* Can't be bothered to check all those
1101 PyFile_WriteString() calls */
1102 if (PyErr_Occurred())
1103 err = -1;
1106 if (err) {
1107 /* Don't do anything else */
1109 else if (PyExceptionClass_Check(exception)) {
1110 char* className = PyExceptionClass_Name(exception);
1111 PyObject* moduleName =
1112 PyObject_GetAttrString(exception, "__module__");
1114 if (moduleName == NULL)
1115 err = PyFile_WriteString("<unknown>", f);
1116 else {
1117 char* modstr = PyString_AsString(moduleName);
1118 Py_DECREF(moduleName);
1119 if (modstr && strcmp(modstr, "exceptions"))
1121 err = PyFile_WriteString(modstr, f);
1122 err += PyFile_WriteString(".", f);
1125 if (err == 0) {
1126 if (className == NULL)
1127 err = PyFile_WriteString("<unknown>", f);
1128 else
1129 err = PyFile_WriteString(className, f);
1132 else
1133 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1134 if (err == 0 && (value != Py_None)) {
1135 PyObject *s = PyObject_Str(value);
1136 /* only print colon if the str() of the
1137 object is not the empty string
1139 if (s == NULL)
1140 err = -1;
1141 else if (!PyString_Check(s) ||
1142 PyString_GET_SIZE(s) != 0)
1143 err = PyFile_WriteString(": ", f);
1144 if (err == 0)
1145 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1146 Py_XDECREF(s);
1148 if (err == 0)
1149 err = PyFile_WriteString("\n", f);
1151 Py_DECREF(value);
1152 /* If an error happened here, don't show it.
1153 XXX This is wrong, but too many callers rely on this behavior. */
1154 if (err != 0)
1155 PyErr_Clear();
1158 PyObject *
1159 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1160 PyObject *locals, PyCompilerFlags *flags)
1162 PyObject *ret;
1163 PyArena *arena = PyArena_New();
1164 mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags,
1165 arena);
1166 ret = run_err_mod(mod, "<string>", globals, locals, flags, arena);
1167 PyArena_Free(arena);
1168 return ret;
1171 PyObject *
1172 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1173 PyObject *locals, int closeit, PyCompilerFlags *flags)
1175 PyObject *ret;
1176 PyArena *arena = PyArena_New();
1177 mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1178 flags, NULL, arena);
1179 if (mod == NULL) {
1180 PyArena_Free(arena);
1181 return NULL;
1183 if (closeit)
1184 fclose(fp);
1185 ret = run_err_mod(mod, filename, globals, locals, flags, arena);
1186 PyArena_Free(arena);
1187 return ret;
1190 static PyObject *
1191 run_err_mod(mod_ty mod, const char *filename, PyObject *globals,
1192 PyObject *locals, PyCompilerFlags *flags, PyArena *arena)
1194 if (mod == NULL)
1195 return NULL;
1196 return run_mod(mod, filename, globals, locals, flags, arena);
1199 static PyObject *
1200 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1201 PyCompilerFlags *flags, PyArena *arena)
1203 PyCodeObject *co;
1204 PyObject *v;
1205 co = PyAST_Compile(mod, filename, flags, arena);
1206 if (co == NULL)
1207 return NULL;
1208 v = PyEval_EvalCode(co, globals, locals);
1209 Py_DECREF(co);
1210 return v;
1213 static PyObject *
1214 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1215 PyObject *locals, PyCompilerFlags *flags)
1217 PyCodeObject *co;
1218 PyObject *v;
1219 long magic;
1220 long PyImport_GetMagicNumber(void);
1222 magic = PyMarshal_ReadLongFromFile(fp);
1223 if (magic != PyImport_GetMagicNumber()) {
1224 PyErr_SetString(PyExc_RuntimeError,
1225 "Bad magic number in .pyc file");
1226 return NULL;
1228 (void) PyMarshal_ReadLongFromFile(fp);
1229 v = PyMarshal_ReadLastObjectFromFile(fp);
1230 fclose(fp);
1231 if (v == NULL || !PyCode_Check(v)) {
1232 Py_XDECREF(v);
1233 PyErr_SetString(PyExc_RuntimeError,
1234 "Bad code object in .pyc file");
1235 return NULL;
1237 co = (PyCodeObject *)v;
1238 v = PyEval_EvalCode(co, globals, locals);
1239 if (v && flags)
1240 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1241 Py_DECREF(co);
1242 return v;
1245 PyObject *
1246 Py_CompileStringFlags(const char *str, const char *filename, int start,
1247 PyCompilerFlags *flags)
1249 PyCodeObject *co;
1250 PyArena *arena = PyArena_New();
1251 mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1252 if (mod == NULL) {
1253 PyArena_Free(arena);
1254 return NULL;
1256 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1257 PyObject *result = PyAST_mod2obj(mod);
1258 PyArena_Free(arena);
1259 return result;
1261 co = PyAST_Compile(mod, filename, flags, arena);
1262 PyArena_Free(arena);
1263 return (PyObject *)co;
1266 struct symtable *
1267 Py_SymtableString(const char *str, const char *filename, int start)
1269 struct symtable *st;
1270 PyArena *arena = PyArena_New();
1271 mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
1272 if (mod == NULL) {
1273 PyArena_Free(arena);
1274 return NULL;
1276 st = PySymtable_Build(mod, filename, 0);
1277 PyArena_Free(arena);
1278 return st;
1281 /* Preferred access to parser is through AST. */
1282 mod_ty
1283 PyParser_ASTFromString(const char *s, const char *filename, int start,
1284 PyCompilerFlags *flags, PyArena *arena)
1286 mod_ty mod;
1287 perrdetail err;
1288 node *n = PyParser_ParseStringFlagsFilename(s, filename,
1289 &_PyParser_Grammar, start, &err,
1290 PARSER_FLAGS(flags));
1291 if (n) {
1292 mod = PyAST_FromNode(n, flags, filename, arena);
1293 PyNode_Free(n);
1294 return mod;
1296 else {
1297 err_input(&err);
1298 return NULL;
1302 mod_ty
1303 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1304 char *ps2, PyCompilerFlags *flags, int *errcode,
1305 PyArena *arena)
1307 mod_ty mod;
1308 perrdetail err;
1309 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1310 start, ps1, ps2, &err, PARSER_FLAGS(flags));
1311 if (n) {
1312 mod = PyAST_FromNode(n, flags, filename, arena);
1313 PyNode_Free(n);
1314 return mod;
1316 else {
1317 err_input(&err);
1318 if (errcode)
1319 *errcode = err.error;
1320 return NULL;
1324 /* Simplified interface to parsefile -- return node or set exception */
1326 node *
1327 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1329 perrdetail err;
1330 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1331 start, NULL, NULL, &err, flags);
1332 if (n == NULL)
1333 err_input(&err);
1335 return n;
1338 /* Simplified interface to parsestring -- return node or set exception */
1340 node *
1341 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1343 perrdetail err;
1344 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1345 start, &err, flags);
1346 if (n == NULL)
1347 err_input(&err);
1348 return n;
1351 node *
1352 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1353 int start, int flags)
1355 perrdetail err;
1356 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1357 &_PyParser_Grammar, start, &err, flags);
1358 if (n == NULL)
1359 err_input(&err);
1360 return n;
1363 node *
1364 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1366 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1369 /* May want to move a more generalized form of this to parsetok.c or
1370 even parser modules. */
1372 void
1373 PyParser_SetError(perrdetail *err)
1375 err_input(err);
1378 /* Set the error appropriate to the given input error code (see errcode.h) */
1380 static void
1381 err_input(perrdetail *err)
1383 PyObject *v, *w, *errtype;
1384 PyObject* u = NULL;
1385 char *msg = NULL;
1386 errtype = PyExc_SyntaxError;
1387 switch (err->error) {
1388 case E_SYNTAX:
1389 errtype = PyExc_IndentationError;
1390 if (err->expected == INDENT)
1391 msg = "expected an indented block";
1392 else if (err->token == INDENT)
1393 msg = "unexpected indent";
1394 else if (err->token == DEDENT)
1395 msg = "unexpected unindent";
1396 else {
1397 errtype = PyExc_SyntaxError;
1398 msg = "invalid syntax";
1400 break;
1401 case E_TOKEN:
1402 msg = "invalid token";
1403 break;
1404 case E_EOFS:
1405 msg = "EOF while scanning triple-quoted string";
1406 break;
1407 case E_EOLS:
1408 msg = "EOL while scanning single-quoted string";
1409 break;
1410 case E_INTR:
1411 if (!PyErr_Occurred())
1412 PyErr_SetNone(PyExc_KeyboardInterrupt);
1413 return;
1414 case E_NOMEM:
1415 PyErr_NoMemory();
1416 return;
1417 case E_EOF:
1418 msg = "unexpected EOF while parsing";
1419 break;
1420 case E_TABSPACE:
1421 errtype = PyExc_TabError;
1422 msg = "inconsistent use of tabs and spaces in indentation";
1423 break;
1424 case E_OVERFLOW:
1425 msg = "expression too long";
1426 break;
1427 case E_DEDENT:
1428 errtype = PyExc_IndentationError;
1429 msg = "unindent does not match any outer indentation level";
1430 break;
1431 case E_TOODEEP:
1432 errtype = PyExc_IndentationError;
1433 msg = "too many levels of indentation";
1434 break;
1435 case E_DECODE: {
1436 PyObject *type, *value, *tb;
1437 PyErr_Fetch(&type, &value, &tb);
1438 if (value != NULL) {
1439 u = PyObject_Str(value);
1440 if (u != NULL) {
1441 msg = PyString_AsString(u);
1444 if (msg == NULL)
1445 msg = "unknown decode error";
1446 Py_XDECREF(type);
1447 Py_XDECREF(value);
1448 Py_XDECREF(tb);
1449 break;
1451 case E_LINECONT:
1452 msg = "unexpected character after line continuation character";
1453 break;
1454 default:
1455 fprintf(stderr, "error=%d\n", err->error);
1456 msg = "unknown parsing error";
1457 break;
1459 v = Py_BuildValue("(ziiz)", err->filename,
1460 err->lineno, err->offset, err->text);
1461 if (err->text != NULL) {
1462 PyMem_DEL(err->text);
1463 err->text = NULL;
1465 w = NULL;
1466 if (v != NULL)
1467 w = Py_BuildValue("(sO)", msg, v);
1468 Py_XDECREF(u);
1469 Py_XDECREF(v);
1470 PyErr_SetObject(errtype, w);
1471 Py_XDECREF(w);
1474 /* Print fatal error message and abort */
1476 void
1477 Py_FatalError(const char *msg)
1479 fprintf(stderr, "Fatal Python error: %s\n", msg);
1480 #ifdef MS_WINDOWS
1481 OutputDebugString("Fatal Python error: ");
1482 OutputDebugString(msg);
1483 OutputDebugString("\n");
1484 #ifdef _DEBUG
1485 DebugBreak();
1486 #endif
1487 #endif /* MS_WINDOWS */
1488 abort();
1491 /* Clean up and exit */
1493 #ifdef WITH_THREAD
1494 #include "pythread.h"
1495 #endif
1497 #define NEXITFUNCS 32
1498 static void (*exitfuncs[NEXITFUNCS])(void);
1499 static int nexitfuncs = 0;
1501 int Py_AtExit(void (*func)(void))
1503 if (nexitfuncs >= NEXITFUNCS)
1504 return -1;
1505 exitfuncs[nexitfuncs++] = func;
1506 return 0;
1509 static void
1510 call_sys_exitfunc(void)
1512 PyObject *exitfunc = PySys_GetObject("exitfunc");
1514 if (exitfunc) {
1515 PyObject *res;
1516 Py_INCREF(exitfunc);
1517 PySys_SetObject("exitfunc", (PyObject *)NULL);
1518 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1519 if (res == NULL) {
1520 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1521 PySys_WriteStderr("Error in sys.exitfunc:\n");
1523 PyErr_Print();
1525 Py_DECREF(exitfunc);
1528 if (Py_FlushLine())
1529 PyErr_Clear();
1532 static void
1533 call_ll_exitfuncs(void)
1535 while (nexitfuncs > 0)
1536 (*exitfuncs[--nexitfuncs])();
1538 fflush(stdout);
1539 fflush(stderr);
1542 void
1543 Py_Exit(int sts)
1545 Py_Finalize();
1547 exit(sts);
1550 static void
1551 initsigs(void)
1553 #ifdef SIGPIPE
1554 PyOS_setsig(SIGPIPE, SIG_IGN);
1555 #endif
1556 #ifdef SIGXFZ
1557 PyOS_setsig(SIGXFZ, SIG_IGN);
1558 #endif
1559 #ifdef SIGXFSZ
1560 PyOS_setsig(SIGXFSZ, SIG_IGN);
1561 #endif
1562 PyOS_InitInterrupts(); /* May imply initsignal() */
1567 * The file descriptor fd is considered ``interactive'' if either
1568 * a) isatty(fd) is TRUE, or
1569 * b) the -i flag was given, and the filename associated with
1570 * the descriptor is NULL or "<stdin>" or "???".
1573 Py_FdIsInteractive(FILE *fp, const char *filename)
1575 if (isatty((int)fileno(fp)))
1576 return 1;
1577 if (!Py_InteractiveFlag)
1578 return 0;
1579 return (filename == NULL) ||
1580 (strcmp(filename, "<stdin>") == 0) ||
1581 (strcmp(filename, "???") == 0);
1585 #if defined(USE_STACKCHECK)
1586 #if defined(WIN32) && defined(_MSC_VER)
1588 /* Stack checking for Microsoft C */
1590 #include <malloc.h>
1591 #include <excpt.h>
1594 * Return non-zero when we run out of memory on the stack; zero otherwise.
1597 PyOS_CheckStack(void)
1599 __try {
1600 /* alloca throws a stack overflow exception if there's
1601 not enough space left on the stack */
1602 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1603 return 0;
1604 } __except (EXCEPTION_EXECUTE_HANDLER) {
1605 /* just ignore all errors */
1607 return 1;
1610 #endif /* WIN32 && _MSC_VER */
1612 /* Alternate implementations can be added here... */
1614 #endif /* USE_STACKCHECK */
1617 /* Wrappers around sigaction() or signal(). */
1619 PyOS_sighandler_t
1620 PyOS_getsig(int sig)
1622 #ifdef HAVE_SIGACTION
1623 struct sigaction context;
1624 if (sigaction(sig, NULL, &context) == -1)
1625 return SIG_ERR;
1626 return context.sa_handler;
1627 #else
1628 PyOS_sighandler_t handler;
1629 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1630 #if defined(_MSC_VER) && _MSC_VER >= 1400
1631 switch (sig) {
1632 /* Only these signals are valid */
1633 case SIGINT:
1634 case SIGILL:
1635 case SIGFPE:
1636 case SIGSEGV:
1637 case SIGTERM:
1638 case SIGBREAK:
1639 case SIGABRT:
1640 break;
1641 /* Don't call signal() with other values or it will assert */
1642 default:
1643 return SIG_ERR;
1645 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1646 handler = signal(sig, SIG_IGN);
1647 if (handler != SIG_ERR)
1648 signal(sig, handler);
1649 return handler;
1650 #endif
1653 PyOS_sighandler_t
1654 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1656 #ifdef HAVE_SIGACTION
1657 struct sigaction context, ocontext;
1658 context.sa_handler = handler;
1659 sigemptyset(&context.sa_mask);
1660 context.sa_flags = 0;
1661 if (sigaction(sig, &context, &ocontext) == -1)
1662 return SIG_ERR;
1663 return ocontext.sa_handler;
1664 #else
1665 PyOS_sighandler_t oldhandler;
1666 oldhandler = signal(sig, handler);
1667 #ifdef HAVE_SIGINTERRUPT
1668 siginterrupt(sig, 1);
1669 #endif
1670 return oldhandler;
1671 #endif
1674 /* Deprecated C API functions still provided for binary compatiblity */
1676 #undef PyParser_SimpleParseFile
1677 #undef PyParser_SimpleParseString
1679 node *
1680 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1682 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1685 node *
1686 PyParser_SimpleParseString(const char *str, int start)
1688 return PyParser_SimpleParseStringFlags(str, start, 0);