Catch situations where currentframe() returns None. See SF patch #1447410, this is...
[python.git] / Python / pythonrun.c
blob7b1f26439bc0188517c92cf5f2946fe4a1bc07d4
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 #ifndef Py_REF_DEBUG
33 # define PRINT_TOTAL_REFS()
34 #else /* Py_REF_DEBUG */
35 # if defined(MS_WIN64)
36 # define PRINT_TOTAL_REFS() fprintf(stderr, "[%Id refs]\n", _Py_RefTotal);
37 # else /* ! MS_WIN64 */
38 # define PRINT_TOTAL_REFS() fprintf(stderr, "[%ld refs]\n", \
39 Py_SAFE_DOWNCAST(_Py_RefTotal, Py_ssize_t, long));
40 # endif /* MS_WIN64 */
41 #endif
43 extern char *Py_GetPath(void);
45 extern grammar _PyParser_Grammar; /* From graminit.c */
47 /* Forward */
48 static void initmain(void);
49 static void initsite(void);
50 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
51 PyCompilerFlags *, PyArena *);
52 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
53 PyCompilerFlags *);
54 static void err_input(perrdetail *);
55 static void initsigs(void);
56 static void call_sys_exitfunc(void);
57 static void call_ll_exitfuncs(void);
58 extern void _PyUnicode_Init(void);
59 extern void _PyUnicode_Fini(void);
61 #ifdef WITH_THREAD
62 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
63 extern void _PyGILState_Fini(void);
64 #endif /* WITH_THREAD */
66 int Py_DebugFlag; /* Needed by parser.c */
67 int Py_VerboseFlag; /* Needed by import.c */
68 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
69 int Py_NoSiteFlag; /* Suppress 'import site' */
70 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
71 int Py_FrozenFlag; /* Needed by getpath.c */
72 int Py_UnicodeFlag = 0; /* Needed by compile.c */
73 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
74 /* _XXX Py_QnewFlag should go away in 2.3. It's true iff -Qnew is passed,
75 on the command line, and is used in 2.2 by ceval.c to make all "/" divisions
76 true divisions (which they will be in 2.3). */
77 int _Py_QnewFlag = 0;
79 /* Reference to 'warnings' module, to avoid importing it
80 on the fly when the import lock may be held. See 683658/771097
82 static PyObject *warnings_module = NULL;
84 /* Returns a borrowed reference to the 'warnings' module, or NULL.
85 If the module is returned, it is guaranteed to have been obtained
86 without acquiring the import lock
88 PyObject *PyModule_GetWarningsModule(void)
90 PyObject *typ, *val, *tb;
91 PyObject *all_modules;
92 /* If we managed to get the module at init time, just use it */
93 if (warnings_module)
94 return warnings_module;
95 /* If it wasn't available at init time, it may be available
96 now in sys.modules (common scenario is frozen apps: import
97 at init time fails, but the frozen init code sets up sys.path
98 correctly, then does an implicit import of warnings for us
100 /* Save and restore any exceptions */
101 PyErr_Fetch(&typ, &val, &tb);
103 all_modules = PySys_GetObject("modules");
104 if (all_modules) {
105 warnings_module = PyDict_GetItemString(all_modules, "warnings");
106 /* We keep a ref in the global */
107 Py_XINCREF(warnings_module);
109 PyErr_Restore(typ, val, tb);
110 return warnings_module;
113 static int initialized = 0;
115 /* API to access the initialized flag -- useful for esoteric use */
118 Py_IsInitialized(void)
120 return initialized;
123 /* Global initializations. Can be undone by Py_Finalize(). Don't
124 call this twice without an intervening Py_Finalize() call. When
125 initializations fail, a fatal error is issued and the function does
126 not return. On return, the first thread and interpreter state have
127 been created.
129 Locking: you must hold the interpreter lock while calling this.
130 (If the lock has not yet been initialized, that's equivalent to
131 having the lock, but you cannot use multiple threads.)
135 static int
136 add_flag(int flag, const char *envs)
138 int env = atoi(envs);
139 if (flag < env)
140 flag = env;
141 if (flag < 1)
142 flag = 1;
143 return flag;
146 void
147 Py_InitializeEx(int install_sigs)
149 PyInterpreterState *interp;
150 PyThreadState *tstate;
151 PyObject *bimod, *sysmod;
152 char *p;
153 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
154 char *codeset;
155 char *saved_locale;
156 PyObject *sys_stream, *sys_isatty;
157 #endif
158 extern void _Py_ReadyTypes(void);
160 if (initialized)
161 return;
162 initialized = 1;
164 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
165 Py_DebugFlag = add_flag(Py_DebugFlag, p);
166 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
167 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
168 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
169 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
171 interp = PyInterpreterState_New();
172 if (interp == NULL)
173 Py_FatalError("Py_Initialize: can't make first interpreter");
175 tstate = PyThreadState_New(interp);
176 if (tstate == NULL)
177 Py_FatalError("Py_Initialize: can't make first thread");
178 (void) PyThreadState_Swap(tstate);
180 _Py_ReadyTypes();
182 if (!_PyFrame_Init())
183 Py_FatalError("Py_Initialize: can't init frames");
185 if (!_PyInt_Init())
186 Py_FatalError("Py_Initialize: can't init ints");
188 _PyFloat_Init();
190 interp->modules = PyDict_New();
191 if (interp->modules == NULL)
192 Py_FatalError("Py_Initialize: can't make modules dictionary");
194 #ifdef Py_USING_UNICODE
195 /* Init Unicode implementation; relies on the codec registry */
196 _PyUnicode_Init();
197 #endif
199 bimod = _PyBuiltin_Init();
200 if (bimod == NULL)
201 Py_FatalError("Py_Initialize: can't initialize __builtin__");
202 interp->builtins = PyModule_GetDict(bimod);
203 Py_INCREF(interp->builtins);
205 sysmod = _PySys_Init();
206 if (sysmod == NULL)
207 Py_FatalError("Py_Initialize: can't initialize sys");
208 interp->sysdict = PyModule_GetDict(sysmod);
209 Py_INCREF(interp->sysdict);
210 _PyImport_FixupExtension("sys", "sys");
211 PySys_SetPath(Py_GetPath());
212 PyDict_SetItemString(interp->sysdict, "modules",
213 interp->modules);
215 _PyImport_Init();
217 /* initialize builtin exceptions */
218 _PyExc_Init();
219 _PyImport_FixupExtension("exceptions", "exceptions");
221 /* phase 2 of builtins */
222 _PyImport_FixupExtension("__builtin__", "__builtin__");
224 _PyImportHooks_Init();
226 if (install_sigs)
227 initsigs(); /* Signal handling stuff, including initintr() */
229 initmain(); /* Module __main__ */
230 if (!Py_NoSiteFlag)
231 initsite(); /* Module site */
233 /* auto-thread-state API, if available */
234 #ifdef WITH_THREAD
235 _PyGILState_Init(interp, tstate);
236 #endif /* WITH_THREAD */
238 warnings_module = PyImport_ImportModule("warnings");
239 if (!warnings_module)
240 PyErr_Clear();
242 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
243 /* On Unix, set the file system encoding according to the
244 user's preference, if the CODESET names a well-known
245 Python codec, and Py_FileSystemDefaultEncoding isn't
246 initialized by other means. Also set the encoding of
247 stdin and stdout if these are terminals. */
249 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
250 setlocale(LC_CTYPE, "");
251 codeset = nl_langinfo(CODESET);
252 if (codeset && *codeset) {
253 PyObject *enc = PyCodec_Encoder(codeset);
254 if (enc) {
255 codeset = strdup(codeset);
256 Py_DECREF(enc);
257 } else {
258 codeset = NULL;
259 PyErr_Clear();
261 } else
262 codeset = NULL;
263 setlocale(LC_CTYPE, saved_locale);
264 free(saved_locale);
266 if (codeset) {
267 sys_stream = PySys_GetObject("stdin");
268 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
269 if (!sys_isatty)
270 PyErr_Clear();
271 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
272 if (!PyFile_SetEncoding(sys_stream, codeset))
273 Py_FatalError("Cannot set codeset of stdin");
275 Py_XDECREF(sys_isatty);
277 sys_stream = PySys_GetObject("stdout");
278 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
279 if (!sys_isatty)
280 PyErr_Clear();
281 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
282 if (!PyFile_SetEncoding(sys_stream, codeset))
283 Py_FatalError("Cannot set codeset of stdout");
285 Py_XDECREF(sys_isatty);
287 if (!Py_FileSystemDefaultEncoding)
288 Py_FileSystemDefaultEncoding = codeset;
289 else
290 free(codeset);
292 #endif
295 void
296 Py_Initialize(void)
298 Py_InitializeEx(1);
302 #ifdef COUNT_ALLOCS
303 extern void dump_counts(void);
304 #endif
306 /* Undo the effect of Py_Initialize().
308 Beware: if multiple interpreter and/or thread states exist, these
309 are not wiped out; only the current thread and interpreter state
310 are deleted. But since everything else is deleted, those other
311 interpreter and thread states should no longer be used.
313 (XXX We should do better, e.g. wipe out all interpreters and
314 threads.)
316 Locking: as above.
320 void
321 Py_Finalize(void)
323 PyInterpreterState *interp;
324 PyThreadState *tstate;
326 if (!initialized)
327 return;
329 /* The interpreter is still entirely intact at this point, and the
330 * exit funcs may be relying on that. In particular, if some thread
331 * or exit func is still waiting to do an import, the import machinery
332 * expects Py_IsInitialized() to return true. So don't say the
333 * interpreter is uninitialized until after the exit funcs have run.
334 * Note that Threading.py uses an exit func to do a join on all the
335 * threads created thru it, so this also protects pending imports in
336 * the threads created via Threading.
338 call_sys_exitfunc();
339 initialized = 0;
341 /* Get current thread state and interpreter pointer */
342 tstate = PyThreadState_GET();
343 interp = tstate->interp;
345 /* Disable signal handling */
346 PyOS_FiniInterrupts();
348 /* drop module references we saved */
349 Py_XDECREF(warnings_module);
350 warnings_module = NULL;
352 /* Collect garbage. This may call finalizers; it's nice to call these
353 * before all modules are destroyed.
354 * XXX If a __del__ or weakref callback is triggered here, and tries to
355 * XXX import a module, bad things can happen, because Python no
356 * XXX longer believes it's initialized.
357 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
358 * XXX is easy to provoke that way. I've also seen, e.g.,
359 * XXX Exception exceptions.ImportError: 'No module named sha'
360 * XXX in <function callback at 0x008F5718> ignored
361 * XXX but I'm unclear on exactly how that one happens. In any case,
362 * XXX I haven't seen a real-life report of either of these.
364 PyGC_Collect();
366 /* Destroy all modules */
367 PyImport_Cleanup();
369 /* Collect final garbage. This disposes of cycles created by
370 * new-style class definitions, for example.
371 * XXX This is disabled because it caused too many problems. If
372 * XXX a __del__ or weakref callback triggers here, Python code has
373 * XXX a hard time running, because even the sys module has been
374 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
375 * XXX One symptom is a sequence of information-free messages
376 * XXX coming from threads (if a __del__ or callback is invoked,
377 * XXX other threads can execute too, and any exception they encounter
378 * XXX triggers a comedy of errors as subsystem after subsystem
379 * XXX fails to find what it *expects* to find in sys to help report
380 * XXX the exception and consequent unexpected failures). I've also
381 * XXX seen segfaults then, after adding print statements to the
382 * XXX Python code getting called.
384 #if 0
385 PyGC_Collect();
386 #endif
388 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
389 _PyImport_Fini();
391 /* Debugging stuff */
392 #ifdef COUNT_ALLOCS
393 dump_counts();
394 #endif
396 PRINT_TOTAL_REFS()
398 #ifdef Py_TRACE_REFS
399 /* Display all objects still alive -- this can invoke arbitrary
400 * __repr__ overrides, so requires a mostly-intact interpreter.
401 * Alas, a lot of stuff may still be alive now that will be cleaned
402 * up later.
404 if (Py_GETENV("PYTHONDUMPREFS"))
405 _Py_PrintReferences(stderr);
406 #endif /* Py_TRACE_REFS */
408 /* Cleanup auto-thread-state */
409 #ifdef WITH_THREAD
410 _PyGILState_Fini();
411 #endif /* WITH_THREAD */
413 /* Clear interpreter state */
414 PyInterpreterState_Clear(interp);
416 /* Now we decref the exception classes. After this point nothing
417 can raise an exception. That's okay, because each Fini() method
418 below has been checked to make sure no exceptions are ever
419 raised.
422 _PyExc_Fini();
424 /* Delete current thread */
425 PyThreadState_Swap(NULL);
426 PyInterpreterState_Delete(interp);
428 /* Sundry finalizers */
429 PyMethod_Fini();
430 PyFrame_Fini();
431 PyCFunction_Fini();
432 PyTuple_Fini();
433 PyList_Fini();
434 PySet_Fini();
435 PyString_Fini();
436 PyInt_Fini();
437 PyFloat_Fini();
439 #ifdef Py_USING_UNICODE
440 /* Cleanup Unicode implementation */
441 _PyUnicode_Fini();
442 #endif
444 /* XXX Still allocated:
445 - various static ad-hoc pointers to interned strings
446 - int and float free list blocks
447 - whatever various modules and libraries allocate
450 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
452 #ifdef Py_TRACE_REFS
453 /* Display addresses (& refcnts) of all objects still alive.
454 * An address can be used to find the repr of the object, printed
455 * above by _Py_PrintReferences.
457 if (Py_GETENV("PYTHONDUMPREFS"))
458 _Py_PrintReferenceAddresses(stderr);
459 #endif /* Py_TRACE_REFS */
460 #ifdef PYMALLOC_DEBUG
461 if (Py_GETENV("PYTHONMALLOCSTATS"))
462 _PyObject_DebugMallocStats();
463 #endif
465 call_ll_exitfuncs();
468 /* Create and initialize a new interpreter and thread, and return the
469 new thread. This requires that Py_Initialize() has been called
470 first.
472 Unsuccessful initialization yields a NULL pointer. Note that *no*
473 exception information is available even in this case -- the
474 exception information is held in the thread, and there is no
475 thread.
477 Locking: as above.
481 PyThreadState *
482 Py_NewInterpreter(void)
484 PyInterpreterState *interp;
485 PyThreadState *tstate, *save_tstate;
486 PyObject *bimod, *sysmod;
488 if (!initialized)
489 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
491 interp = PyInterpreterState_New();
492 if (interp == NULL)
493 return NULL;
495 tstate = PyThreadState_New(interp);
496 if (tstate == NULL) {
497 PyInterpreterState_Delete(interp);
498 return NULL;
501 save_tstate = PyThreadState_Swap(tstate);
503 /* XXX The following is lax in error checking */
505 interp->modules = PyDict_New();
507 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
508 if (bimod != NULL) {
509 interp->builtins = PyModule_GetDict(bimod);
510 Py_INCREF(interp->builtins);
512 sysmod = _PyImport_FindExtension("sys", "sys");
513 if (bimod != NULL && sysmod != NULL) {
514 interp->sysdict = PyModule_GetDict(sysmod);
515 Py_INCREF(interp->sysdict);
516 PySys_SetPath(Py_GetPath());
517 PyDict_SetItemString(interp->sysdict, "modules",
518 interp->modules);
519 _PyImportHooks_Init();
520 initmain();
521 if (!Py_NoSiteFlag)
522 initsite();
525 if (!PyErr_Occurred())
526 return tstate;
528 /* Oops, it didn't work. Undo it all. */
530 PyErr_Print();
531 PyThreadState_Clear(tstate);
532 PyThreadState_Swap(save_tstate);
533 PyThreadState_Delete(tstate);
534 PyInterpreterState_Delete(interp);
536 return NULL;
539 /* Delete an interpreter and its last thread. This requires that the
540 given thread state is current, that the thread has no remaining
541 frames, and that it is its interpreter's only remaining thread.
542 It is a fatal error to violate these constraints.
544 (Py_Finalize() doesn't have these constraints -- it zaps
545 everything, regardless.)
547 Locking: as above.
551 void
552 Py_EndInterpreter(PyThreadState *tstate)
554 PyInterpreterState *interp = tstate->interp;
556 if (tstate != PyThreadState_GET())
557 Py_FatalError("Py_EndInterpreter: thread is not current");
558 if (tstate->frame != NULL)
559 Py_FatalError("Py_EndInterpreter: thread still has a frame");
560 if (tstate != interp->tstate_head || tstate->next != NULL)
561 Py_FatalError("Py_EndInterpreter: not the last thread");
563 PyImport_Cleanup();
564 PyInterpreterState_Clear(interp);
565 PyThreadState_Swap(NULL);
566 PyInterpreterState_Delete(interp);
569 static char *progname = "python";
571 void
572 Py_SetProgramName(char *pn)
574 if (pn && *pn)
575 progname = pn;
578 char *
579 Py_GetProgramName(void)
581 return progname;
584 static char *default_home = NULL;
586 void
587 Py_SetPythonHome(char *home)
589 default_home = home;
592 char *
593 Py_GetPythonHome(void)
595 char *home = default_home;
596 if (home == NULL && !Py_IgnoreEnvironmentFlag)
597 home = Py_GETENV("PYTHONHOME");
598 return home;
601 /* Create __main__ module */
603 static void
604 initmain(void)
606 PyObject *m, *d;
607 m = PyImport_AddModule("__main__");
608 if (m == NULL)
609 Py_FatalError("can't create __main__ module");
610 d = PyModule_GetDict(m);
611 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
612 PyObject *bimod = PyImport_ImportModule("__builtin__");
613 if (bimod == NULL ||
614 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
615 Py_FatalError("can't add __builtins__ to __main__");
616 Py_DECREF(bimod);
620 /* Import the site module (not into __main__ though) */
622 static void
623 initsite(void)
625 PyObject *m, *f;
626 m = PyImport_ImportModule("site");
627 if (m == NULL) {
628 f = PySys_GetObject("stderr");
629 if (Py_VerboseFlag) {
630 PyFile_WriteString(
631 "'import site' failed; traceback:\n", f);
632 PyErr_Print();
634 else {
635 PyFile_WriteString(
636 "'import site' failed; use -v for traceback\n", f);
637 PyErr_Clear();
640 else {
641 Py_DECREF(m);
645 /* Parse input from a file and execute it */
648 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
649 PyCompilerFlags *flags)
651 if (filename == NULL)
652 filename = "???";
653 if (Py_FdIsInteractive(fp, filename)) {
654 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
655 if (closeit)
656 fclose(fp);
657 return err;
659 else
660 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
664 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
666 PyObject *v;
667 int ret;
668 PyCompilerFlags local_flags;
670 if (flags == NULL) {
671 flags = &local_flags;
672 local_flags.cf_flags = 0;
674 v = PySys_GetObject("ps1");
675 if (v == NULL) {
676 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
677 Py_XDECREF(v);
679 v = PySys_GetObject("ps2");
680 if (v == NULL) {
681 PySys_SetObject("ps2", v = PyString_FromString("... "));
682 Py_XDECREF(v);
684 for (;;) {
685 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
686 PRINT_TOTAL_REFS()
687 if (ret == E_EOF)
688 return 0;
690 if (ret == E_NOMEM)
691 return -1;
696 /* compute parser flags based on compiler flags */
697 #define PARSER_FLAGS(flags) \
698 ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
699 PyPARSE_DONT_IMPLY_DEDENT : 0) \
700 | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
701 PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
704 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
706 PyObject *m, *d, *v, *w;
707 mod_ty mod;
708 PyArena *arena;
709 char *ps1 = "", *ps2 = "";
710 int errcode = 0;
712 v = PySys_GetObject("ps1");
713 if (v != NULL) {
714 v = PyObject_Str(v);
715 if (v == NULL)
716 PyErr_Clear();
717 else if (PyString_Check(v))
718 ps1 = PyString_AsString(v);
720 w = PySys_GetObject("ps2");
721 if (w != NULL) {
722 w = PyObject_Str(w);
723 if (w == NULL)
724 PyErr_Clear();
725 else if (PyString_Check(w))
726 ps2 = PyString_AsString(w);
728 arena = PyArena_New();
729 mod = PyParser_ASTFromFile(fp, filename,
730 Py_single_input, ps1, ps2,
731 flags, &errcode, arena);
732 Py_XDECREF(v);
733 Py_XDECREF(w);
734 if (mod == NULL) {
735 PyArena_Free(arena);
736 if (errcode == E_EOF) {
737 PyErr_Clear();
738 return E_EOF;
740 PyErr_Print();
741 return -1;
743 m = PyImport_AddModule("__main__");
744 if (m == NULL) {
745 PyArena_Free(arena);
746 return -1;
748 d = PyModule_GetDict(m);
749 v = run_mod(mod, filename, d, d, flags, arena);
750 PyArena_Free(arena);
751 if (v == NULL) {
752 PyErr_Print();
753 return -1;
755 Py_DECREF(v);
756 if (Py_FlushLine())
757 PyErr_Clear();
758 return 0;
761 /* Check whether a file maybe a pyc file: Look at the extension,
762 the file type, and, if we may close it, at the first few bytes. */
764 static int
765 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
767 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
768 return 1;
770 /* Only look into the file if we are allowed to close it, since
771 it then should also be seekable. */
772 if (closeit) {
773 /* Read only two bytes of the magic. If the file was opened in
774 text mode, the bytes 3 and 4 of the magic (\r\n) might not
775 be read as they are on disk. */
776 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
777 unsigned char buf[2];
778 /* Mess: In case of -x, the stream is NOT at its start now,
779 and ungetc() was used to push back the first newline,
780 which makes the current stream position formally undefined,
781 and a x-platform nightmare.
782 Unfortunately, we have no direct way to know whether -x
783 was specified. So we use a terrible hack: if the current
784 stream position is not 0, we assume -x was specified, and
785 give up. Bug 132850 on SourceForge spells out the
786 hopelessness of trying anything else (fseek and ftell
787 don't work predictably x-platform for text-mode files).
789 int ispyc = 0;
790 if (ftell(fp) == 0) {
791 if (fread(buf, 1, 2, fp) == 2 &&
792 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
793 ispyc = 1;
794 rewind(fp);
796 return ispyc;
798 return 0;
802 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
803 PyCompilerFlags *flags)
805 PyObject *m, *d, *v;
806 const char *ext;
808 m = PyImport_AddModule("__main__");
809 if (m == NULL)
810 return -1;
811 d = PyModule_GetDict(m);
812 if (PyDict_GetItemString(d, "__file__") == NULL) {
813 PyObject *f = PyString_FromString(filename);
814 if (f == NULL)
815 return -1;
816 if (PyDict_SetItemString(d, "__file__", f) < 0) {
817 Py_DECREF(f);
818 return -1;
820 Py_DECREF(f);
822 ext = filename + strlen(filename) - 4;
823 if (maybe_pyc_file(fp, filename, ext, closeit)) {
824 /* Try to run a pyc file. First, re-open in binary */
825 if (closeit)
826 fclose(fp);
827 if ((fp = fopen(filename, "rb")) == NULL) {
828 fprintf(stderr, "python: Can't reopen .pyc file\n");
829 return -1;
831 /* Turn on optimization if a .pyo file is given */
832 if (strcmp(ext, ".pyo") == 0)
833 Py_OptimizeFlag = 1;
834 v = run_pyc_file(fp, filename, d, d, flags);
835 } else {
836 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
837 closeit, flags);
839 if (v == NULL) {
840 PyErr_Print();
841 return -1;
843 Py_DECREF(v);
844 if (Py_FlushLine())
845 PyErr_Clear();
846 return 0;
850 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
852 PyObject *m, *d, *v;
853 m = PyImport_AddModule("__main__");
854 if (m == NULL)
855 return -1;
856 d = PyModule_GetDict(m);
857 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
858 if (v == NULL) {
859 PyErr_Print();
860 return -1;
862 Py_DECREF(v);
863 if (Py_FlushLine())
864 PyErr_Clear();
865 return 0;
868 static int
869 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
870 int *lineno, int *offset, const char **text)
872 long hold;
873 PyObject *v;
875 /* old style errors */
876 if (PyTuple_Check(err))
877 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
878 lineno, offset, text);
880 /* new style errors. `err' is an instance */
882 if (! (v = PyObject_GetAttrString(err, "msg")))
883 goto finally;
884 *message = v;
886 if (!(v = PyObject_GetAttrString(err, "filename")))
887 goto finally;
888 if (v == Py_None)
889 *filename = NULL;
890 else if (! (*filename = PyString_AsString(v)))
891 goto finally;
893 Py_DECREF(v);
894 if (!(v = PyObject_GetAttrString(err, "lineno")))
895 goto finally;
896 hold = PyInt_AsLong(v);
897 Py_DECREF(v);
898 v = NULL;
899 if (hold < 0 && PyErr_Occurred())
900 goto finally;
901 *lineno = (int)hold;
903 if (!(v = PyObject_GetAttrString(err, "offset")))
904 goto finally;
905 if (v == Py_None) {
906 *offset = -1;
907 Py_DECREF(v);
908 v = NULL;
909 } else {
910 hold = PyInt_AsLong(v);
911 Py_DECREF(v);
912 v = NULL;
913 if (hold < 0 && PyErr_Occurred())
914 goto finally;
915 *offset = (int)hold;
918 if (!(v = PyObject_GetAttrString(err, "text")))
919 goto finally;
920 if (v == Py_None)
921 *text = NULL;
922 else if (! (*text = PyString_AsString(v)))
923 goto finally;
924 Py_DECREF(v);
925 return 1;
927 finally:
928 Py_XDECREF(v);
929 return 0;
932 void
933 PyErr_Print(void)
935 PyErr_PrintEx(1);
938 static void
939 print_error_text(PyObject *f, int offset, const char *text)
941 char *nl;
942 if (offset >= 0) {
943 if (offset > 0 && offset == (int)strlen(text))
944 offset--;
945 for (;;) {
946 nl = strchr(text, '\n');
947 if (nl == NULL || nl-text >= offset)
948 break;
949 offset -= (int)(nl+1-text);
950 text = nl+1;
952 while (*text == ' ' || *text == '\t') {
953 text++;
954 offset--;
957 PyFile_WriteString(" ", f);
958 PyFile_WriteString(text, f);
959 if (*text == '\0' || text[strlen(text)-1] != '\n')
960 PyFile_WriteString("\n", f);
961 if (offset == -1)
962 return;
963 PyFile_WriteString(" ", f);
964 offset--;
965 while (offset > 0) {
966 PyFile_WriteString(" ", f);
967 offset--;
969 PyFile_WriteString("^\n", f);
972 static void
973 handle_system_exit(void)
975 PyObject *exception, *value, *tb;
976 int exitcode = 0;
978 PyErr_Fetch(&exception, &value, &tb);
979 if (Py_FlushLine())
980 PyErr_Clear();
981 fflush(stdout);
982 if (value == NULL || value == Py_None)
983 goto done;
984 if (PyExceptionInstance_Check(value)) {
985 /* The error code should be in the `code' attribute. */
986 PyObject *code = PyObject_GetAttrString(value, "code");
987 if (code) {
988 Py_DECREF(value);
989 value = code;
990 if (value == Py_None)
991 goto done;
993 /* If we failed to dig out the 'code' attribute,
994 just let the else clause below print the error. */
996 if (PyInt_Check(value))
997 exitcode = (int)PyInt_AsLong(value);
998 else {
999 PyObject_Print(value, stderr, Py_PRINT_RAW);
1000 PySys_WriteStderr("\n");
1001 exitcode = 1;
1003 done:
1004 /* Restore and clear the exception info, in order to properly decref
1005 * the exception, value, and traceback. If we just exit instead,
1006 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1007 * some finalizers from running.
1009 PyErr_Restore(exception, value, tb);
1010 PyErr_Clear();
1011 Py_Exit(exitcode);
1012 /* NOTREACHED */
1015 void
1016 PyErr_PrintEx(int set_sys_last_vars)
1018 PyObject *exception, *v, *tb, *hook;
1020 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1021 handle_system_exit();
1023 PyErr_Fetch(&exception, &v, &tb);
1024 if (exception == NULL)
1025 return;
1026 PyErr_NormalizeException(&exception, &v, &tb);
1027 if (exception == NULL)
1028 return;
1029 /* Now we know v != NULL too */
1030 if (set_sys_last_vars) {
1031 PySys_SetObject("last_type", exception);
1032 PySys_SetObject("last_value", v);
1033 PySys_SetObject("last_traceback", tb);
1035 hook = PySys_GetObject("excepthook");
1036 if (hook) {
1037 PyObject *args = PyTuple_Pack(3,
1038 exception, v, tb ? tb : Py_None);
1039 PyObject *result = PyEval_CallObject(hook, args);
1040 if (result == NULL) {
1041 PyObject *exception2, *v2, *tb2;
1042 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1043 handle_system_exit();
1045 PyErr_Fetch(&exception2, &v2, &tb2);
1046 PyErr_NormalizeException(&exception2, &v2, &tb2);
1047 if (Py_FlushLine())
1048 PyErr_Clear();
1049 fflush(stdout);
1050 PySys_WriteStderr("Error in sys.excepthook:\n");
1051 PyErr_Display(exception2, v2, tb2);
1052 PySys_WriteStderr("\nOriginal exception was:\n");
1053 PyErr_Display(exception, v, tb);
1054 Py_XDECREF(exception2);
1055 Py_XDECREF(v2);
1056 Py_XDECREF(tb2);
1058 Py_XDECREF(result);
1059 Py_XDECREF(args);
1060 } else {
1061 PySys_WriteStderr("sys.excepthook is missing\n");
1062 PyErr_Display(exception, v, tb);
1064 Py_XDECREF(exception);
1065 Py_XDECREF(v);
1066 Py_XDECREF(tb);
1069 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1071 int err = 0;
1072 PyObject *f = PySys_GetObject("stderr");
1073 Py_INCREF(value);
1074 if (f == NULL)
1075 fprintf(stderr, "lost sys.stderr\n");
1076 else {
1077 if (Py_FlushLine())
1078 PyErr_Clear();
1079 fflush(stdout);
1080 if (tb && tb != Py_None)
1081 err = PyTraceBack_Print(tb, f);
1082 if (err == 0 &&
1083 PyObject_HasAttrString(value, "print_file_and_line"))
1085 PyObject *message;
1086 const char *filename, *text;
1087 int lineno, offset;
1088 if (!parse_syntax_error(value, &message, &filename,
1089 &lineno, &offset, &text))
1090 PyErr_Clear();
1091 else {
1092 char buf[10];
1093 PyFile_WriteString(" File \"", f);
1094 if (filename == NULL)
1095 PyFile_WriteString("<string>", f);
1096 else
1097 PyFile_WriteString(filename, f);
1098 PyFile_WriteString("\", line ", f);
1099 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1100 PyFile_WriteString(buf, f);
1101 PyFile_WriteString("\n", f);
1102 if (text != NULL)
1103 print_error_text(f, offset, text);
1104 Py_DECREF(value);
1105 value = message;
1106 /* Can't be bothered to check all those
1107 PyFile_WriteString() calls */
1108 if (PyErr_Occurred())
1109 err = -1;
1112 if (err) {
1113 /* Don't do anything else */
1115 else if (PyExceptionClass_Check(exception)) {
1116 char* className = PyExceptionClass_Name(exception);
1117 PyObject* moduleName =
1118 PyObject_GetAttrString(exception, "__module__");
1120 if (moduleName == NULL)
1121 err = PyFile_WriteString("<unknown>", f);
1122 else {
1123 char* modstr = PyString_AsString(moduleName);
1124 Py_DECREF(moduleName);
1125 if (modstr && strcmp(modstr, "exceptions"))
1127 err = PyFile_WriteString(modstr, f);
1128 err += PyFile_WriteString(".", f);
1131 if (err == 0) {
1132 if (className == NULL)
1133 err = PyFile_WriteString("<unknown>", f);
1134 else
1135 err = PyFile_WriteString(className, f);
1138 else
1139 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1140 if (err == 0 && (value != Py_None)) {
1141 PyObject *s = PyObject_Str(value);
1142 /* only print colon if the str() of the
1143 object is not the empty string
1145 if (s == NULL)
1146 err = -1;
1147 else if (!PyString_Check(s) ||
1148 PyString_GET_SIZE(s) != 0)
1149 err = PyFile_WriteString(": ", f);
1150 if (err == 0)
1151 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1152 Py_XDECREF(s);
1154 if (err == 0)
1155 err = PyFile_WriteString("\n", f);
1157 Py_DECREF(value);
1158 /* If an error happened here, don't show it.
1159 XXX This is wrong, but too many callers rely on this behavior. */
1160 if (err != 0)
1161 PyErr_Clear();
1164 PyObject *
1165 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1166 PyObject *locals, PyCompilerFlags *flags)
1168 PyObject *ret = NULL;
1169 PyArena *arena = PyArena_New();
1170 mod_ty mod = PyParser_ASTFromString(str, "<string>", start, flags,
1171 arena);
1172 if (mod != NULL)
1173 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1174 PyArena_Free(arena);
1175 return ret;
1178 PyObject *
1179 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1180 PyObject *locals, int closeit, PyCompilerFlags *flags)
1182 PyObject *ret;
1183 PyArena *arena = PyArena_New();
1184 mod_ty mod = PyParser_ASTFromFile(fp, filename, start, 0, 0,
1185 flags, NULL, arena);
1186 if (mod == NULL) {
1187 PyArena_Free(arena);
1188 return NULL;
1190 if (closeit)
1191 fclose(fp);
1192 ret = run_mod(mod, filename, globals, locals, flags, arena);
1193 PyArena_Free(arena);
1194 return ret;
1197 static PyObject *
1198 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1199 PyCompilerFlags *flags, PyArena *arena)
1201 PyCodeObject *co;
1202 PyObject *v;
1203 co = PyAST_Compile(mod, filename, flags, arena);
1204 if (co == NULL)
1205 return NULL;
1206 v = PyEval_EvalCode(co, globals, locals);
1207 Py_DECREF(co);
1208 return v;
1211 static PyObject *
1212 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1213 PyObject *locals, PyCompilerFlags *flags)
1215 PyCodeObject *co;
1216 PyObject *v;
1217 long magic;
1218 long PyImport_GetMagicNumber(void);
1220 magic = PyMarshal_ReadLongFromFile(fp);
1221 if (magic != PyImport_GetMagicNumber()) {
1222 PyErr_SetString(PyExc_RuntimeError,
1223 "Bad magic number in .pyc file");
1224 return NULL;
1226 (void) PyMarshal_ReadLongFromFile(fp);
1227 v = PyMarshal_ReadLastObjectFromFile(fp);
1228 fclose(fp);
1229 if (v == NULL || !PyCode_Check(v)) {
1230 Py_XDECREF(v);
1231 PyErr_SetString(PyExc_RuntimeError,
1232 "Bad code object in .pyc file");
1233 return NULL;
1235 co = (PyCodeObject *)v;
1236 v = PyEval_EvalCode(co, globals, locals);
1237 if (v && flags)
1238 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1239 Py_DECREF(co);
1240 return v;
1243 PyObject *
1244 Py_CompileStringFlags(const char *str, const char *filename, int start,
1245 PyCompilerFlags *flags)
1247 PyCodeObject *co;
1248 PyArena *arena = PyArena_New();
1249 mod_ty mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1250 if (mod == NULL) {
1251 PyArena_Free(arena);
1252 return NULL;
1254 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1255 PyObject *result = PyAST_mod2obj(mod);
1256 PyArena_Free(arena);
1257 return result;
1259 co = PyAST_Compile(mod, filename, flags, arena);
1260 PyArena_Free(arena);
1261 return (PyObject *)co;
1264 struct symtable *
1265 Py_SymtableString(const char *str, const char *filename, int start)
1267 struct symtable *st;
1268 PyArena *arena = PyArena_New();
1269 mod_ty mod = PyParser_ASTFromString(str, filename, start, NULL, arena);
1270 if (mod == NULL) {
1271 PyArena_Free(arena);
1272 return NULL;
1274 st = PySymtable_Build(mod, filename, 0);
1275 PyArena_Free(arena);
1276 return st;
1279 /* Preferred access to parser is through AST. */
1280 mod_ty
1281 PyParser_ASTFromString(const char *s, const char *filename, int start,
1282 PyCompilerFlags *flags, PyArena *arena)
1284 mod_ty mod;
1285 perrdetail err;
1286 node *n = PyParser_ParseStringFlagsFilename(s, filename,
1287 &_PyParser_Grammar, start, &err,
1288 PARSER_FLAGS(flags));
1289 if (n) {
1290 mod = PyAST_FromNode(n, flags, filename, arena);
1291 PyNode_Free(n);
1292 return mod;
1294 else {
1295 err_input(&err);
1296 return NULL;
1300 mod_ty
1301 PyParser_ASTFromFile(FILE *fp, const char *filename, int start, char *ps1,
1302 char *ps2, PyCompilerFlags *flags, int *errcode,
1303 PyArena *arena)
1305 mod_ty mod;
1306 perrdetail err;
1307 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1308 start, ps1, ps2, &err, PARSER_FLAGS(flags));
1309 if (n) {
1310 mod = PyAST_FromNode(n, flags, filename, arena);
1311 PyNode_Free(n);
1312 return mod;
1314 else {
1315 err_input(&err);
1316 if (errcode)
1317 *errcode = err.error;
1318 return NULL;
1322 /* Simplified interface to parsefile -- return node or set exception */
1324 node *
1325 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1327 perrdetail err;
1328 node *n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
1329 start, NULL, NULL, &err, flags);
1330 if (n == NULL)
1331 err_input(&err);
1333 return n;
1336 /* Simplified interface to parsestring -- return node or set exception */
1338 node *
1339 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1341 perrdetail err;
1342 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1343 start, &err, flags);
1344 if (n == NULL)
1345 err_input(&err);
1346 return n;
1349 node *
1350 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1351 int start, int flags)
1353 perrdetail err;
1354 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1355 &_PyParser_Grammar, start, &err, flags);
1356 if (n == NULL)
1357 err_input(&err);
1358 return n;
1361 node *
1362 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1364 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1367 /* May want to move a more generalized form of this to parsetok.c or
1368 even parser modules. */
1370 void
1371 PyParser_SetError(perrdetail *err)
1373 err_input(err);
1376 /* Set the error appropriate to the given input error code (see errcode.h) */
1378 static void
1379 err_input(perrdetail *err)
1381 PyObject *v, *w, *errtype;
1382 PyObject* u = NULL;
1383 char *msg = NULL;
1384 errtype = PyExc_SyntaxError;
1385 switch (err->error) {
1386 case E_SYNTAX:
1387 errtype = PyExc_IndentationError;
1388 if (err->expected == INDENT)
1389 msg = "expected an indented block";
1390 else if (err->token == INDENT)
1391 msg = "unexpected indent";
1392 else if (err->token == DEDENT)
1393 msg = "unexpected unindent";
1394 else {
1395 errtype = PyExc_SyntaxError;
1396 msg = "invalid syntax";
1398 break;
1399 case E_TOKEN:
1400 msg = "invalid token";
1401 break;
1402 case E_EOFS:
1403 msg = "EOF while scanning triple-quoted string";
1404 break;
1405 case E_EOLS:
1406 msg = "EOL while scanning single-quoted string";
1407 break;
1408 case E_INTR:
1409 if (!PyErr_Occurred())
1410 PyErr_SetNone(PyExc_KeyboardInterrupt);
1411 return;
1412 case E_NOMEM:
1413 PyErr_NoMemory();
1414 return;
1415 case E_EOF:
1416 msg = "unexpected EOF while parsing";
1417 break;
1418 case E_TABSPACE:
1419 errtype = PyExc_TabError;
1420 msg = "inconsistent use of tabs and spaces in indentation";
1421 break;
1422 case E_OVERFLOW:
1423 msg = "expression too long";
1424 break;
1425 case E_DEDENT:
1426 errtype = PyExc_IndentationError;
1427 msg = "unindent does not match any outer indentation level";
1428 break;
1429 case E_TOODEEP:
1430 errtype = PyExc_IndentationError;
1431 msg = "too many levels of indentation";
1432 break;
1433 case E_DECODE: {
1434 PyObject *type, *value, *tb;
1435 PyErr_Fetch(&type, &value, &tb);
1436 if (value != NULL) {
1437 u = PyObject_Str(value);
1438 if (u != NULL) {
1439 msg = PyString_AsString(u);
1442 if (msg == NULL)
1443 msg = "unknown decode error";
1444 Py_XDECREF(type);
1445 Py_XDECREF(value);
1446 Py_XDECREF(tb);
1447 break;
1449 case E_LINECONT:
1450 msg = "unexpected character after line continuation character";
1451 break;
1452 default:
1453 fprintf(stderr, "error=%d\n", err->error);
1454 msg = "unknown parsing error";
1455 break;
1457 v = Py_BuildValue("(ziiz)", err->filename,
1458 err->lineno, err->offset, err->text);
1459 if (err->text != NULL) {
1460 PyMem_DEL(err->text);
1461 err->text = NULL;
1463 w = NULL;
1464 if (v != NULL)
1465 w = Py_BuildValue("(sO)", msg, v);
1466 Py_XDECREF(u);
1467 Py_XDECREF(v);
1468 PyErr_SetObject(errtype, w);
1469 Py_XDECREF(w);
1472 /* Print fatal error message and abort */
1474 void
1475 Py_FatalError(const char *msg)
1477 fprintf(stderr, "Fatal Python error: %s\n", msg);
1478 #ifdef MS_WINDOWS
1479 OutputDebugString("Fatal Python error: ");
1480 OutputDebugString(msg);
1481 OutputDebugString("\n");
1482 #ifdef _DEBUG
1483 DebugBreak();
1484 #endif
1485 #endif /* MS_WINDOWS */
1486 abort();
1489 /* Clean up and exit */
1491 #ifdef WITH_THREAD
1492 #include "pythread.h"
1493 #endif
1495 #define NEXITFUNCS 32
1496 static void (*exitfuncs[NEXITFUNCS])(void);
1497 static int nexitfuncs = 0;
1499 int Py_AtExit(void (*func)(void))
1501 if (nexitfuncs >= NEXITFUNCS)
1502 return -1;
1503 exitfuncs[nexitfuncs++] = func;
1504 return 0;
1507 static void
1508 call_sys_exitfunc(void)
1510 PyObject *exitfunc = PySys_GetObject("exitfunc");
1512 if (exitfunc) {
1513 PyObject *res;
1514 Py_INCREF(exitfunc);
1515 PySys_SetObject("exitfunc", (PyObject *)NULL);
1516 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1517 if (res == NULL) {
1518 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1519 PySys_WriteStderr("Error in sys.exitfunc:\n");
1521 PyErr_Print();
1523 Py_DECREF(exitfunc);
1526 if (Py_FlushLine())
1527 PyErr_Clear();
1530 static void
1531 call_ll_exitfuncs(void)
1533 while (nexitfuncs > 0)
1534 (*exitfuncs[--nexitfuncs])();
1536 fflush(stdout);
1537 fflush(stderr);
1540 void
1541 Py_Exit(int sts)
1543 Py_Finalize();
1545 exit(sts);
1548 static void
1549 initsigs(void)
1551 #ifdef SIGPIPE
1552 PyOS_setsig(SIGPIPE, SIG_IGN);
1553 #endif
1554 #ifdef SIGXFZ
1555 PyOS_setsig(SIGXFZ, SIG_IGN);
1556 #endif
1557 #ifdef SIGXFSZ
1558 PyOS_setsig(SIGXFSZ, SIG_IGN);
1559 #endif
1560 PyOS_InitInterrupts(); /* May imply initsignal() */
1565 * The file descriptor fd is considered ``interactive'' if either
1566 * a) isatty(fd) is TRUE, or
1567 * b) the -i flag was given, and the filename associated with
1568 * the descriptor is NULL or "<stdin>" or "???".
1571 Py_FdIsInteractive(FILE *fp, const char *filename)
1573 if (isatty((int)fileno(fp)))
1574 return 1;
1575 if (!Py_InteractiveFlag)
1576 return 0;
1577 return (filename == NULL) ||
1578 (strcmp(filename, "<stdin>") == 0) ||
1579 (strcmp(filename, "???") == 0);
1583 #if defined(USE_STACKCHECK)
1584 #if defined(WIN32) && defined(_MSC_VER)
1586 /* Stack checking for Microsoft C */
1588 #include <malloc.h>
1589 #include <excpt.h>
1592 * Return non-zero when we run out of memory on the stack; zero otherwise.
1595 PyOS_CheckStack(void)
1597 __try {
1598 /* alloca throws a stack overflow exception if there's
1599 not enough space left on the stack */
1600 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1601 return 0;
1602 } __except (EXCEPTION_EXECUTE_HANDLER) {
1603 /* just ignore all errors */
1605 return 1;
1608 #endif /* WIN32 && _MSC_VER */
1610 /* Alternate implementations can be added here... */
1612 #endif /* USE_STACKCHECK */
1615 /* Wrappers around sigaction() or signal(). */
1617 PyOS_sighandler_t
1618 PyOS_getsig(int sig)
1620 #ifdef HAVE_SIGACTION
1621 struct sigaction context;
1622 if (sigaction(sig, NULL, &context) == -1)
1623 return SIG_ERR;
1624 return context.sa_handler;
1625 #else
1626 PyOS_sighandler_t handler;
1627 /* Special signal handling for the secure CRT in Visual Studio 2005 */
1628 #if defined(_MSC_VER) && _MSC_VER >= 1400
1629 switch (sig) {
1630 /* Only these signals are valid */
1631 case SIGINT:
1632 case SIGILL:
1633 case SIGFPE:
1634 case SIGSEGV:
1635 case SIGTERM:
1636 case SIGBREAK:
1637 case SIGABRT:
1638 break;
1639 /* Don't call signal() with other values or it will assert */
1640 default:
1641 return SIG_ERR;
1643 #endif /* _MSC_VER && _MSC_VER >= 1400 */
1644 handler = signal(sig, SIG_IGN);
1645 if (handler != SIG_ERR)
1646 signal(sig, handler);
1647 return handler;
1648 #endif
1651 PyOS_sighandler_t
1652 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1654 #ifdef HAVE_SIGACTION
1655 struct sigaction context, ocontext;
1656 context.sa_handler = handler;
1657 sigemptyset(&context.sa_mask);
1658 context.sa_flags = 0;
1659 if (sigaction(sig, &context, &ocontext) == -1)
1660 return SIG_ERR;
1661 return ocontext.sa_handler;
1662 #else
1663 PyOS_sighandler_t oldhandler;
1664 oldhandler = signal(sig, handler);
1665 #ifdef HAVE_SIGINTERRUPT
1666 siginterrupt(sig, 1);
1667 #endif
1668 return oldhandler;
1669 #endif
1672 /* Deprecated C API functions still provided for binary compatiblity */
1674 #undef PyParser_SimpleParseFile
1675 #undef PyParser_SimpleParseString
1677 node *
1678 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1680 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1683 node *
1684 PyParser_SimpleParseString(const char *str, int start)
1686 return PyParser_SimpleParseStringFlags(str, start, 0);