This commit was manufactured by cvs2svn to create tag 'r234c1'.
[python/dscho.git] / Python / pythonrun.c
blob3985cd3191f8ee35dd9e13af54821cb38310de65
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/771097
74 static PyObject *warnings_module = NULL;
76 /* Returns a borrowed reference to the 'warnings' module, or NULL.
77 If the module is returned, it is guaranteed to have been obtained
78 without acquiring the import lock
80 PyObject *PyModule_GetWarningsModule(void)
82 PyObject *typ, *val, *tb;
83 PyObject *all_modules;
84 /* If we managed to get the module at init time, just use it */
85 if (warnings_module)
86 return warnings_module;
87 /* If it wasn't available at init time, it may be available
88 now in sys.modules (common scenario is frozen apps: import
89 at init time fails, but the frozen init code sets up sys.path
90 correctly, then does an implicit import of warnings for us
92 /* Save and restore any exceptions */
93 PyErr_Fetch(&typ, &val, &tb);
95 all_modules = PySys_GetObject("modules");
96 if (all_modules) {
97 warnings_module = PyDict_GetItemString(all_modules, "warnings");
98 /* We keep a ref in the global */
99 Py_XINCREF(warnings_module);
101 PyErr_Restore(typ, val, tb);
102 return warnings_module;
105 static int initialized = 0;
107 /* API to access the initialized flag -- useful for esoteric use */
110 Py_IsInitialized(void)
112 return initialized;
115 /* Global initializations. Can be undone by Py_Finalize(). Don't
116 call this twice without an intervening Py_Finalize() call. When
117 initializations fail, a fatal error is issued and the function does
118 not return. On return, the first thread and interpreter state have
119 been created.
121 Locking: you must hold the interpreter lock while calling this.
122 (If the lock has not yet been initialized, that's equivalent to
123 having the lock, but you cannot use multiple threads.)
127 static int
128 add_flag(int flag, const char *envs)
130 int env = atoi(envs);
131 if (flag < env)
132 flag = env;
133 if (flag < 1)
134 flag = 1;
135 return flag;
138 void
139 Py_Initialize(void)
141 PyInterpreterState *interp;
142 PyThreadState *tstate;
143 PyObject *bimod, *sysmod;
144 char *p;
145 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
146 char *codeset;
147 char *saved_locale;
148 PyObject *sys_stream, *sys_isatty;
149 #endif
150 extern void _Py_ReadyTypes(void);
152 if (initialized)
153 return;
154 initialized = 1;
156 if ((p = Py_GETENV("PYTHONDEBUG")) && *p != '\0')
157 Py_DebugFlag = add_flag(Py_DebugFlag, p);
158 if ((p = Py_GETENV("PYTHONVERBOSE")) && *p != '\0')
159 Py_VerboseFlag = add_flag(Py_VerboseFlag, p);
160 if ((p = Py_GETENV("PYTHONOPTIMIZE")) && *p != '\0')
161 Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
163 interp = PyInterpreterState_New();
164 if (interp == NULL)
165 Py_FatalError("Py_Initialize: can't make first interpreter");
167 tstate = PyThreadState_New(interp);
168 if (tstate == NULL)
169 Py_FatalError("Py_Initialize: can't make first thread");
170 (void) PyThreadState_Swap(tstate);
172 _Py_ReadyTypes();
174 if (!_PyFrame_Init())
175 Py_FatalError("Py_Initialize: can't init frames");
177 if (!_PyInt_Init())
178 Py_FatalError("Py_Initialize: can't init ints");
180 interp->modules = PyDict_New();
181 if (interp->modules == NULL)
182 Py_FatalError("Py_Initialize: can't make modules dictionary");
184 #ifdef Py_USING_UNICODE
185 /* Init Unicode implementation; relies on the codec registry */
186 _PyUnicode_Init();
187 #endif
189 bimod = _PyBuiltin_Init();
190 if (bimod == NULL)
191 Py_FatalError("Py_Initialize: can't initialize __builtin__");
192 interp->builtins = PyModule_GetDict(bimod);
193 Py_INCREF(interp->builtins);
195 sysmod = _PySys_Init();
196 if (sysmod == NULL)
197 Py_FatalError("Py_Initialize: can't initialize sys");
198 interp->sysdict = PyModule_GetDict(sysmod);
199 Py_INCREF(interp->sysdict);
200 _PyImport_FixupExtension("sys", "sys");
201 PySys_SetPath(Py_GetPath());
202 PyDict_SetItemString(interp->sysdict, "modules",
203 interp->modules);
205 _PyImport_Init();
207 /* initialize builtin exceptions */
208 _PyExc_Init();
209 _PyImport_FixupExtension("exceptions", "exceptions");
211 /* phase 2 of builtins */
212 _PyImport_FixupExtension("__builtin__", "__builtin__");
214 _PyImportHooks_Init();
216 initsigs(); /* Signal handling stuff, including initintr() */
218 initmain(); /* Module __main__ */
219 if (!Py_NoSiteFlag)
220 initsite(); /* Module site */
222 /* auto-thread-state API, if available */
223 #ifdef WITH_THREAD
224 _PyGILState_Init(interp, tstate);
225 #endif /* WITH_THREAD */
227 warnings_module = PyImport_ImportModule("warnings");
228 if (!warnings_module)
229 PyErr_Clear();
231 #if defined(Py_USING_UNICODE) && defined(HAVE_LANGINFO_H) && defined(CODESET)
232 /* On Unix, set the file system encoding according to the
233 user's preference, if the CODESET names a well-known
234 Python codec, and Py_FileSystemDefaultEncoding isn't
235 initialized by other means. Also set the encoding of
236 stdin and stdout if these are terminals. */
238 saved_locale = strdup(setlocale(LC_CTYPE, NULL));
239 setlocale(LC_CTYPE, "");
240 codeset = nl_langinfo(CODESET);
241 if (codeset && *codeset) {
242 PyObject *enc = PyCodec_Encoder(codeset);
243 if (enc) {
244 codeset = strdup(codeset);
245 Py_DECREF(enc);
246 } else {
247 codeset = NULL;
248 PyErr_Clear();
250 } else
251 codeset = NULL;
252 setlocale(LC_CTYPE, saved_locale);
253 free(saved_locale);
255 if (codeset) {
256 sys_stream = PySys_GetObject("stdin");
257 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
258 if (!sys_isatty)
259 PyErr_Clear();
260 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
261 if (!PyFile_SetEncoding(sys_stream, codeset))
262 Py_FatalError("Cannot set codeset of stdin");
264 Py_XDECREF(sys_isatty);
266 sys_stream = PySys_GetObject("stdout");
267 sys_isatty = PyObject_CallMethod(sys_stream, "isatty", "");
268 if (!sys_isatty)
269 PyErr_Clear();
270 if(sys_isatty && PyObject_IsTrue(sys_isatty)) {
271 if (!PyFile_SetEncoding(sys_stream, codeset))
272 Py_FatalError("Cannot set codeset of stdout");
274 Py_XDECREF(sys_isatty);
276 if (!Py_FileSystemDefaultEncoding)
277 Py_FileSystemDefaultEncoding = codeset;
278 else
279 free(codeset);
281 #endif
284 #ifdef COUNT_ALLOCS
285 extern void dump_counts(void);
286 #endif
288 /* Undo the effect of Py_Initialize().
290 Beware: if multiple interpreter and/or thread states exist, these
291 are not wiped out; only the current thread and interpreter state
292 are deleted. But since everything else is deleted, those other
293 interpreter and thread states should no longer be used.
295 (XXX We should do better, e.g. wipe out all interpreters and
296 threads.)
298 Locking: as above.
302 void
303 Py_Finalize(void)
305 PyInterpreterState *interp;
306 PyThreadState *tstate;
308 if (!initialized)
309 return;
311 /* The interpreter is still entirely intact at this point, and the
312 * exit funcs may be relying on that. In particular, if some thread
313 * or exit func is still waiting to do an import, the import machinery
314 * expects Py_IsInitialized() to return true. So don't say the
315 * interpreter is uninitialized until after the exit funcs have run.
316 * Note that Threading.py uses an exit func to do a join on all the
317 * threads created thru it, so this also protects pending imports in
318 * the threads created via Threading.
320 call_sys_exitfunc();
321 initialized = 0;
323 /* Get current thread state and interpreter pointer */
324 tstate = PyThreadState_Get();
325 interp = tstate->interp;
327 /* Disable signal handling */
328 PyOS_FiniInterrupts();
330 /* drop module references we saved */
331 Py_XDECREF(warnings_module);
332 warnings_module = NULL;
334 /* Collect garbage. This may call finalizers; it's nice to call these
335 * before all modules are destroyed.
336 * XXX If a __del__ or weakref callback is triggered here, and tries to
337 * XXX import a module, bad things can happen, because Python no
338 * XXX longer believes it's initialized.
339 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
340 * XXX is easy to provoke that way. I've also seen, e.g.,
341 * XXX Exception exceptions.ImportError: 'No module named sha'
342 * XXX in <function callback at 0x008F5718> ignored
343 * XXX but I'm unclear on exactly how that one happens. In any case,
344 * XXX I haven't seen a real-life report of either of these.
346 PyGC_Collect();
348 /* Destroy all modules */
349 PyImport_Cleanup();
351 /* Collect final garbage. This disposes of cycles created by
352 * new-style class definitions, for example.
353 * XXX This is disabled because it caused too many problems. If
354 * XXX a __del__ or weakref callback triggers here, Python code has
355 * XXX a hard time running, because even the sys module has been
356 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
357 * XXX One symptom is a sequence of information-free messages
358 * XXX coming from threads (if a __del__ or callback is invoked,
359 * XXX other threads can execute too, and any exception they encounter
360 * XXX triggers a comedy of errors as subsystem after subsystem
361 * XXX fails to find what it *expects* to find in sys to help report
362 * XXX the exception and consequent unexpected failures). I've also
363 * XXX seen segfaults then, after adding print statements to the
364 * XXX Python code getting called.
366 #if 0
367 PyGC_Collect();
368 #endif
370 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
371 _PyImport_Fini();
373 /* Debugging stuff */
374 #ifdef COUNT_ALLOCS
375 dump_counts();
376 #endif
378 #ifdef Py_REF_DEBUG
379 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
380 #endif
382 #ifdef Py_TRACE_REFS
383 /* Display all objects still alive -- this can invoke arbitrary
384 * __repr__ overrides, so requires a mostly-intact interpreter.
385 * Alas, a lot of stuff may still be alive now that will be cleaned
386 * up later.
388 if (Py_GETENV("PYTHONDUMPREFS"))
389 _Py_PrintReferences(stderr);
390 #endif /* Py_TRACE_REFS */
392 /* Now we decref the exception classes. After this point nothing
393 can raise an exception. That's okay, because each Fini() method
394 below has been checked to make sure no exceptions are ever
395 raised.
397 _PyExc_Fini();
399 /* Cleanup auto-thread-state */
400 #ifdef WITH_THREAD
401 _PyGILState_Fini();
402 #endif /* WITH_THREAD */
404 /* Clear interpreter state */
405 PyInterpreterState_Clear(interp);
407 /* Delete current thread */
408 PyThreadState_Swap(NULL);
409 PyInterpreterState_Delete(interp);
411 /* Sundry finalizers */
412 PyMethod_Fini();
413 PyFrame_Fini();
414 PyCFunction_Fini();
415 PyTuple_Fini();
416 PyString_Fini();
417 PyInt_Fini();
418 PyFloat_Fini();
420 #ifdef Py_USING_UNICODE
421 /* Cleanup Unicode implementation */
422 _PyUnicode_Fini();
423 #endif
425 /* XXX Still allocated:
426 - various static ad-hoc pointers to interned strings
427 - int and float free list blocks
428 - whatever various modules and libraries allocate
431 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
433 #ifdef Py_TRACE_REFS
434 /* Display addresses (& refcnts) of all objects still alive.
435 * An address can be used to find the repr of the object, printed
436 * above by _Py_PrintReferences.
438 if (Py_GETENV("PYTHONDUMPREFS"))
439 _Py_PrintReferenceAddresses(stderr);
440 #endif /* Py_TRACE_REFS */
441 #ifdef PYMALLOC_DEBUG
442 if (Py_GETENV("PYTHONMALLOCSTATS"))
443 _PyObject_DebugMallocStats();
444 #endif
446 call_ll_exitfuncs();
449 /* Create and initialize a new interpreter and thread, and return the
450 new thread. This requires that Py_Initialize() has been called
451 first.
453 Unsuccessful initialization yields a NULL pointer. Note that *no*
454 exception information is available even in this case -- the
455 exception information is held in the thread, and there is no
456 thread.
458 Locking: as above.
462 PyThreadState *
463 Py_NewInterpreter(void)
465 PyInterpreterState *interp;
466 PyThreadState *tstate, *save_tstate;
467 PyObject *bimod, *sysmod;
469 if (!initialized)
470 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
472 interp = PyInterpreterState_New();
473 if (interp == NULL)
474 return NULL;
476 tstate = PyThreadState_New(interp);
477 if (tstate == NULL) {
478 PyInterpreterState_Delete(interp);
479 return NULL;
482 save_tstate = PyThreadState_Swap(tstate);
484 /* XXX The following is lax in error checking */
486 interp->modules = PyDict_New();
488 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
489 if (bimod != NULL) {
490 interp->builtins = PyModule_GetDict(bimod);
491 Py_INCREF(interp->builtins);
493 sysmod = _PyImport_FindExtension("sys", "sys");
494 if (bimod != NULL && sysmod != NULL) {
495 interp->sysdict = PyModule_GetDict(sysmod);
496 Py_INCREF(interp->sysdict);
497 PySys_SetPath(Py_GetPath());
498 PyDict_SetItemString(interp->sysdict, "modules",
499 interp->modules);
500 _PyImportHooks_Init();
501 initmain();
502 if (!Py_NoSiteFlag)
503 initsite();
506 if (!PyErr_Occurred())
507 return tstate;
509 /* Oops, it didn't work. Undo it all. */
511 PyErr_Print();
512 PyThreadState_Clear(tstate);
513 PyThreadState_Swap(save_tstate);
514 PyThreadState_Delete(tstate);
515 PyInterpreterState_Delete(interp);
517 return NULL;
520 /* Delete an interpreter and its last thread. This requires that the
521 given thread state is current, that the thread has no remaining
522 frames, and that it is its interpreter's only remaining thread.
523 It is a fatal error to violate these constraints.
525 (Py_Finalize() doesn't have these constraints -- it zaps
526 everything, regardless.)
528 Locking: as above.
532 void
533 Py_EndInterpreter(PyThreadState *tstate)
535 PyInterpreterState *interp = tstate->interp;
537 if (tstate != PyThreadState_Get())
538 Py_FatalError("Py_EndInterpreter: thread is not current");
539 if (tstate->frame != NULL)
540 Py_FatalError("Py_EndInterpreter: thread still has a frame");
541 if (tstate != interp->tstate_head || tstate->next != NULL)
542 Py_FatalError("Py_EndInterpreter: not the last thread");
544 PyImport_Cleanup();
545 PyInterpreterState_Clear(interp);
546 PyThreadState_Swap(NULL);
547 PyInterpreterState_Delete(interp);
550 static char *progname = "python";
552 void
553 Py_SetProgramName(char *pn)
555 if (pn && *pn)
556 progname = pn;
559 char *
560 Py_GetProgramName(void)
562 return progname;
565 static char *default_home = NULL;
567 void
568 Py_SetPythonHome(char *home)
570 default_home = home;
573 char *
574 Py_GetPythonHome(void)
576 char *home = default_home;
577 if (home == NULL && !Py_IgnoreEnvironmentFlag)
578 home = Py_GETENV("PYTHONHOME");
579 return home;
582 /* Create __main__ module */
584 static void
585 initmain(void)
587 PyObject *m, *d;
588 m = PyImport_AddModule("__main__");
589 if (m == NULL)
590 Py_FatalError("can't create __main__ module");
591 d = PyModule_GetDict(m);
592 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
593 PyObject *bimod = PyImport_ImportModule("__builtin__");
594 if (bimod == NULL ||
595 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
596 Py_FatalError("can't add __builtins__ to __main__");
597 Py_DECREF(bimod);
601 /* Import the site module (not into __main__ though) */
603 static void
604 initsite(void)
606 PyObject *m, *f;
607 m = PyImport_ImportModule("site");
608 if (m == NULL) {
609 f = PySys_GetObject("stderr");
610 if (Py_VerboseFlag) {
611 PyFile_WriteString(
612 "'import site' failed; traceback:\n", f);
613 PyErr_Print();
615 else {
616 PyFile_WriteString(
617 "'import site' failed; use -v for traceback\n", f);
618 PyErr_Clear();
621 else {
622 Py_DECREF(m);
626 /* Parse input from a file and execute it */
629 PyRun_AnyFile(FILE *fp, const char *filename)
631 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
635 PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
637 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
641 PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
643 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
647 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
648 PyCompilerFlags *flags)
650 if (filename == NULL)
651 filename = "???";
652 if (Py_FdIsInteractive(fp, filename)) {
653 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
654 if (closeit)
655 fclose(fp);
656 return err;
658 else
659 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
663 PyRun_InteractiveLoop(FILE *fp, const char *filename)
665 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
669 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
671 PyObject *v;
672 int ret;
673 PyCompilerFlags local_flags;
675 if (flags == NULL) {
676 flags = &local_flags;
677 local_flags.cf_flags = 0;
679 v = PySys_GetObject("ps1");
680 if (v == NULL) {
681 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
682 Py_XDECREF(v);
684 v = PySys_GetObject("ps2");
685 if (v == NULL) {
686 PySys_SetObject("ps2", v = PyString_FromString("... "));
687 Py_XDECREF(v);
689 for (;;) {
690 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
691 #ifdef Py_REF_DEBUG
692 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
693 #endif
694 if (ret == E_EOF)
695 return 0;
697 if (ret == E_NOMEM)
698 return -1;
704 PyRun_InteractiveOne(FILE *fp, const char *filename)
706 return PyRun_InteractiveOneFlags(fp, filename, NULL);
709 /* compute parser flags based on compiler flags */
710 #define PARSER_FLAGS(flags) \
711 (((flags) && (flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
712 PyPARSE_DONT_IMPLY_DEDENT : 0)
715 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
717 PyObject *m, *d, *v, *w;
718 node *n;
719 perrdetail err;
720 char *ps1 = "", *ps2 = "";
722 v = PySys_GetObject("ps1");
723 if (v != NULL) {
724 v = PyObject_Str(v);
725 if (v == NULL)
726 PyErr_Clear();
727 else if (PyString_Check(v))
728 ps1 = PyString_AsString(v);
730 w = PySys_GetObject("ps2");
731 if (w != NULL) {
732 w = PyObject_Str(w);
733 if (w == NULL)
734 PyErr_Clear();
735 else if (PyString_Check(w))
736 ps2 = PyString_AsString(w);
738 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
739 Py_single_input, ps1, ps2, &err,
740 PARSER_FLAGS(flags));
741 Py_XDECREF(v);
742 Py_XDECREF(w);
743 if (n == NULL) {
744 if (err.error == E_EOF) {
745 if (err.text)
746 PyMem_DEL(err.text);
747 return E_EOF;
749 err_input(&err);
750 PyErr_Print();
751 return err.error;
753 m = PyImport_AddModule("__main__");
754 if (m == NULL)
755 return -1;
756 d = PyModule_GetDict(m);
757 v = run_node(n, filename, d, d, flags);
758 if (v == NULL) {
759 PyErr_Print();
760 return -1;
762 Py_DECREF(v);
763 if (Py_FlushLine())
764 PyErr_Clear();
765 return 0;
769 PyRun_SimpleFile(FILE *fp, const char *filename)
771 return PyRun_SimpleFileEx(fp, filename, 0);
774 /* Check whether a file maybe a pyc file: Look at the extension,
775 the file type, and, if we may close it, at the first few bytes. */
777 static int
778 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
780 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
781 return 1;
783 #ifdef macintosh
784 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
785 if (PyMac_getfiletype((char *)filename) == 'PYC '
786 || PyMac_getfiletype((char *)filename) == 'APPL')
787 return 1;
788 #endif /* macintosh */
790 /* Only look into the file if we are allowed to close it, since
791 it then should also be seekable. */
792 if (closeit) {
793 /* Read only two bytes of the magic. If the file was opened in
794 text mode, the bytes 3 and 4 of the magic (\r\n) might not
795 be read as they are on disk. */
796 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
797 unsigned char buf[2];
798 /* Mess: In case of -x, the stream is NOT at its start now,
799 and ungetc() was used to push back the first newline,
800 which makes the current stream position formally undefined,
801 and a x-platform nightmare.
802 Unfortunately, we have no direct way to know whether -x
803 was specified. So we use a terrible hack: if the current
804 stream position is not 0, we assume -x was specified, and
805 give up. Bug 132850 on SourceForge spells out the
806 hopelessness of trying anything else (fseek and ftell
807 don't work predictably x-platform for text-mode files).
809 int ispyc = 0;
810 if (ftell(fp) == 0) {
811 if (fread(buf, 1, 2, fp) == 2 &&
812 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
813 ispyc = 1;
814 rewind(fp);
816 return ispyc;
818 return 0;
822 PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
824 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
828 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
829 PyCompilerFlags *flags)
831 PyObject *m, *d, *v;
832 const char *ext;
834 m = PyImport_AddModule("__main__");
835 if (m == NULL)
836 return -1;
837 d = PyModule_GetDict(m);
838 if (PyDict_GetItemString(d, "__file__") == NULL) {
839 PyObject *f = PyString_FromString(filename);
840 if (f == NULL)
841 return -1;
842 if (PyDict_SetItemString(d, "__file__", f) < 0) {
843 Py_DECREF(f);
844 return -1;
846 Py_DECREF(f);
848 ext = filename + strlen(filename) - 4;
849 if (maybe_pyc_file(fp, filename, ext, closeit)) {
850 /* Try to run a pyc file. First, re-open in binary */
851 if (closeit)
852 fclose(fp);
853 if ((fp = fopen(filename, "rb")) == NULL) {
854 fprintf(stderr, "python: Can't reopen .pyc file\n");
855 return -1;
857 /* Turn on optimization if a .pyo file is given */
858 if (strcmp(ext, ".pyo") == 0)
859 Py_OptimizeFlag = 1;
860 v = run_pyc_file(fp, filename, d, d, flags);
861 } else {
862 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
863 closeit, flags);
865 if (v == NULL) {
866 PyErr_Print();
867 return -1;
869 Py_DECREF(v);
870 if (Py_FlushLine())
871 PyErr_Clear();
872 return 0;
876 PyRun_SimpleString(const char *command)
878 return PyRun_SimpleStringFlags(command, NULL);
882 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
884 PyObject *m, *d, *v;
885 m = PyImport_AddModule("__main__");
886 if (m == NULL)
887 return -1;
888 d = PyModule_GetDict(m);
889 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
890 if (v == NULL) {
891 PyErr_Print();
892 return -1;
894 Py_DECREF(v);
895 if (Py_FlushLine())
896 PyErr_Clear();
897 return 0;
900 static int
901 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
902 int *lineno, int *offset, const char **text)
904 long hold;
905 PyObject *v;
907 /* old style errors */
908 if (PyTuple_Check(err))
909 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
910 lineno, offset, text);
912 /* new style errors. `err' is an instance */
914 if (! (v = PyObject_GetAttrString(err, "msg")))
915 goto finally;
916 *message = v;
918 if (!(v = PyObject_GetAttrString(err, "filename")))
919 goto finally;
920 if (v == Py_None)
921 *filename = NULL;
922 else if (! (*filename = PyString_AsString(v)))
923 goto finally;
925 Py_DECREF(v);
926 if (!(v = PyObject_GetAttrString(err, "lineno")))
927 goto finally;
928 hold = PyInt_AsLong(v);
929 Py_DECREF(v);
930 v = NULL;
931 if (hold < 0 && PyErr_Occurred())
932 goto finally;
933 *lineno = (int)hold;
935 if (!(v = PyObject_GetAttrString(err, "offset")))
936 goto finally;
937 if (v == Py_None) {
938 *offset = -1;
939 Py_DECREF(v);
940 v = NULL;
941 } else {
942 hold = PyInt_AsLong(v);
943 Py_DECREF(v);
944 v = NULL;
945 if (hold < 0 && PyErr_Occurred())
946 goto finally;
947 *offset = (int)hold;
950 if (!(v = PyObject_GetAttrString(err, "text")))
951 goto finally;
952 if (v == Py_None)
953 *text = NULL;
954 else if (! (*text = PyString_AsString(v)))
955 goto finally;
956 Py_DECREF(v);
957 return 1;
959 finally:
960 Py_XDECREF(v);
961 return 0;
964 void
965 PyErr_Print(void)
967 PyErr_PrintEx(1);
970 static void
971 print_error_text(PyObject *f, int offset, const char *text)
973 char *nl;
974 if (offset >= 0) {
975 if (offset > 0 && offset == (int)strlen(text))
976 offset--;
977 for (;;) {
978 nl = strchr(text, '\n');
979 if (nl == NULL || nl-text >= offset)
980 break;
981 offset -= (nl+1-text);
982 text = nl+1;
984 while (*text == ' ' || *text == '\t') {
985 text++;
986 offset--;
989 PyFile_WriteString(" ", f);
990 PyFile_WriteString(text, f);
991 if (*text == '\0' || text[strlen(text)-1] != '\n')
992 PyFile_WriteString("\n", f);
993 if (offset == -1)
994 return;
995 PyFile_WriteString(" ", f);
996 offset--;
997 while (offset > 0) {
998 PyFile_WriteString(" ", f);
999 offset--;
1001 PyFile_WriteString("^\n", f);
1004 static void
1005 handle_system_exit(void)
1007 PyObject *exception, *value, *tb;
1008 int exitcode = 0;
1010 PyErr_Fetch(&exception, &value, &tb);
1011 if (Py_FlushLine())
1012 PyErr_Clear();
1013 fflush(stdout);
1014 if (value == NULL || value == Py_None)
1015 goto done;
1016 if (PyInstance_Check(value)) {
1017 /* The error code should be in the `code' attribute. */
1018 PyObject *code = PyObject_GetAttrString(value, "code");
1019 if (code) {
1020 Py_DECREF(value);
1021 value = code;
1022 if (value == Py_None)
1023 goto done;
1025 /* If we failed to dig out the 'code' attribute,
1026 just let the else clause below print the error. */
1028 if (PyInt_Check(value))
1029 exitcode = (int)PyInt_AsLong(value);
1030 else {
1031 PyObject_Print(value, stderr, Py_PRINT_RAW);
1032 PySys_WriteStderr("\n");
1033 exitcode = 1;
1035 done:
1036 /* Restore and clear the exception info, in order to properly decref
1037 * the exception, value, and traceback. If we just exit instead,
1038 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1039 * some finalizers from running.
1041 PyErr_Restore(exception, value, tb);
1042 PyErr_Clear();
1043 Py_Exit(exitcode);
1044 /* NOTREACHED */
1047 void
1048 PyErr_PrintEx(int set_sys_last_vars)
1050 PyObject *exception, *v, *tb, *hook;
1052 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1053 handle_system_exit();
1055 PyErr_Fetch(&exception, &v, &tb);
1056 PyErr_NormalizeException(&exception, &v, &tb);
1057 if (exception == NULL)
1058 return;
1059 if (set_sys_last_vars) {
1060 PySys_SetObject("last_type", exception);
1061 PySys_SetObject("last_value", v);
1062 PySys_SetObject("last_traceback", tb);
1064 hook = PySys_GetObject("excepthook");
1065 if (hook) {
1066 PyObject *args = Py_BuildValue("(OOO)",
1067 exception, v ? v : Py_None, tb ? tb : Py_None);
1068 PyObject *result = PyEval_CallObject(hook, args);
1069 if (result == NULL) {
1070 PyObject *exception2, *v2, *tb2;
1071 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1072 handle_system_exit();
1074 PyErr_Fetch(&exception2, &v2, &tb2);
1075 PyErr_NormalizeException(&exception2, &v2, &tb2);
1076 if (Py_FlushLine())
1077 PyErr_Clear();
1078 fflush(stdout);
1079 PySys_WriteStderr("Error in sys.excepthook:\n");
1080 PyErr_Display(exception2, v2, tb2);
1081 PySys_WriteStderr("\nOriginal exception was:\n");
1082 PyErr_Display(exception, v, tb);
1083 Py_XDECREF(exception2);
1084 Py_XDECREF(v2);
1085 Py_XDECREF(tb2);
1087 Py_XDECREF(result);
1088 Py_XDECREF(args);
1089 } else {
1090 PySys_WriteStderr("sys.excepthook is missing\n");
1091 PyErr_Display(exception, v, tb);
1093 Py_XDECREF(exception);
1094 Py_XDECREF(v);
1095 Py_XDECREF(tb);
1098 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1100 int err = 0;
1101 PyObject *f = PySys_GetObject("stderr");
1102 Py_INCREF(value);
1103 if (f == NULL)
1104 fprintf(stderr, "lost sys.stderr\n");
1105 else {
1106 if (Py_FlushLine())
1107 PyErr_Clear();
1108 fflush(stdout);
1109 if (tb && tb != Py_None)
1110 err = PyTraceBack_Print(tb, f);
1111 if (err == 0 &&
1112 PyObject_HasAttrString(value, "print_file_and_line"))
1114 PyObject *message;
1115 const char *filename, *text;
1116 int lineno, offset;
1117 if (!parse_syntax_error(value, &message, &filename,
1118 &lineno, &offset, &text))
1119 PyErr_Clear();
1120 else {
1121 char buf[10];
1122 PyFile_WriteString(" File \"", f);
1123 if (filename == NULL)
1124 PyFile_WriteString("<string>", f);
1125 else
1126 PyFile_WriteString(filename, f);
1127 PyFile_WriteString("\", line ", f);
1128 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1129 PyFile_WriteString(buf, f);
1130 PyFile_WriteString("\n", f);
1131 if (text != NULL)
1132 print_error_text(f, offset, text);
1133 Py_DECREF(value);
1134 value = message;
1135 /* Can't be bothered to check all those
1136 PyFile_WriteString() calls */
1137 if (PyErr_Occurred())
1138 err = -1;
1141 if (err) {
1142 /* Don't do anything else */
1144 else if (PyClass_Check(exception)) {
1145 PyClassObject* exc = (PyClassObject*)exception;
1146 PyObject* className = exc->cl_name;
1147 PyObject* moduleName =
1148 PyDict_GetItemString(exc->cl_dict, "__module__");
1150 if (moduleName == NULL)
1151 err = PyFile_WriteString("<unknown>", f);
1152 else {
1153 char* modstr = PyString_AsString(moduleName);
1154 if (modstr && strcmp(modstr, "exceptions"))
1156 err = PyFile_WriteString(modstr, f);
1157 err += PyFile_WriteString(".", f);
1160 if (err == 0) {
1161 if (className == NULL)
1162 err = PyFile_WriteString("<unknown>", f);
1163 else
1164 err = PyFile_WriteObject(className, f,
1165 Py_PRINT_RAW);
1168 else
1169 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
1170 if (err == 0) {
1171 if (value != Py_None) {
1172 PyObject *s = PyObject_Str(value);
1173 /* only print colon if the str() of the
1174 object is not the empty string
1176 if (s == NULL)
1177 err = -1;
1178 else if (!PyString_Check(s) ||
1179 PyString_GET_SIZE(s) != 0)
1180 err = PyFile_WriteString(": ", f);
1181 if (err == 0)
1182 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1183 Py_XDECREF(s);
1186 if (err == 0)
1187 err = PyFile_WriteString("\n", f);
1189 Py_DECREF(value);
1190 /* If an error happened here, don't show it.
1191 XXX This is wrong, but too many callers rely on this behavior. */
1192 if (err != 0)
1193 PyErr_Clear();
1196 PyObject *
1197 PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
1199 return run_err_node(PyParser_SimpleParseString(str, start),
1200 "<string>", globals, locals, NULL);
1203 PyObject *
1204 PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals,
1205 PyObject *locals)
1207 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
1210 PyObject *
1211 PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals,
1212 PyObject *locals, int closeit)
1214 node *n = PyParser_SimpleParseFile(fp, filename, start);
1215 if (closeit)
1216 fclose(fp);
1217 return run_err_node(n, filename, globals, locals, NULL);
1220 PyObject *
1221 PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals,
1222 PyCompilerFlags *flags)
1224 return run_err_node(PyParser_SimpleParseStringFlags(
1225 str, start, PARSER_FLAGS(flags)),
1226 "<string>", globals, locals, flags);
1229 PyObject *
1230 PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1231 PyObject *locals, PyCompilerFlags *flags)
1233 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
1234 flags);
1237 PyObject *
1238 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1239 PyObject *locals, int closeit, PyCompilerFlags *flags)
1241 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
1242 PARSER_FLAGS(flags));
1243 if (closeit)
1244 fclose(fp);
1245 return run_err_node(n, filename, globals, locals, flags);
1248 static PyObject *
1249 run_err_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
1250 PyCompilerFlags *flags)
1252 if (n == NULL)
1253 return NULL;
1254 return run_node(n, filename, globals, locals, flags);
1257 static PyObject *
1258 run_node(node *n, const char *filename, PyObject *globals, PyObject *locals,
1259 PyCompilerFlags *flags)
1261 PyCodeObject *co;
1262 PyObject *v;
1263 co = PyNode_CompileFlags(n, filename, flags);
1264 PyNode_Free(n);
1265 if (co == NULL)
1266 return NULL;
1267 v = PyEval_EvalCode(co, globals, locals);
1268 Py_DECREF(co);
1269 return v;
1272 static PyObject *
1273 run_pyc_file(FILE *fp, const char *filename, PyObject *globals, PyObject *locals,
1274 PyCompilerFlags *flags)
1276 PyCodeObject *co;
1277 PyObject *v;
1278 long magic;
1279 long PyImport_GetMagicNumber(void);
1281 magic = PyMarshal_ReadLongFromFile(fp);
1282 if (magic != PyImport_GetMagicNumber()) {
1283 PyErr_SetString(PyExc_RuntimeError,
1284 "Bad magic number in .pyc file");
1285 return NULL;
1287 (void) PyMarshal_ReadLongFromFile(fp);
1288 v = PyMarshal_ReadLastObjectFromFile(fp);
1289 fclose(fp);
1290 if (v == NULL || !PyCode_Check(v)) {
1291 Py_XDECREF(v);
1292 PyErr_SetString(PyExc_RuntimeError,
1293 "Bad code object in .pyc file");
1294 return NULL;
1296 co = (PyCodeObject *)v;
1297 v = PyEval_EvalCode(co, globals, locals);
1298 if (v && flags)
1299 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1300 Py_DECREF(co);
1301 return v;
1304 PyObject *
1305 Py_CompileString(const char *str, const char *filename, int start)
1307 return Py_CompileStringFlags(str, filename, start, NULL);
1310 PyObject *
1311 Py_CompileStringFlags(const char *str, const char *filename, int start,
1312 PyCompilerFlags *flags)
1314 node *n;
1315 PyCodeObject *co;
1317 n = PyParser_SimpleParseStringFlagsFilename(str, filename, start,
1318 PARSER_FLAGS(flags));
1319 if (n == NULL)
1320 return NULL;
1321 co = PyNode_CompileFlags(n, filename, flags);
1322 PyNode_Free(n);
1323 return (PyObject *)co;
1326 struct symtable *
1327 Py_SymtableString(const char *str, const char *filename, int start)
1329 node *n;
1330 struct symtable *st;
1331 n = PyParser_SimpleParseStringFlagsFilename(str, filename,
1332 start, 0);
1333 if (n == NULL)
1334 return NULL;
1335 st = PyNode_CompileSymtable(n, filename);
1336 PyNode_Free(n);
1337 return st;
1340 /* Simplified interface to parsefile -- return node or set exception */
1342 node *
1343 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1345 node *n;
1346 perrdetail err;
1347 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
1348 (char *)0, (char *)0, &err, flags);
1349 if (n == NULL)
1350 err_input(&err);
1351 return n;
1354 node *
1355 PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1357 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1360 /* Simplified interface to parsestring -- return node or set exception */
1362 node *
1363 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1365 node *n;
1366 perrdetail err;
1367 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
1368 flags);
1369 if (n == NULL)
1370 err_input(&err);
1371 return n;
1374 node *
1375 PyParser_SimpleParseString(const char *str, int start)
1377 return PyParser_SimpleParseStringFlags(str, start, 0);
1380 node *
1381 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1382 int start, int flags)
1384 node *n;
1385 perrdetail err;
1387 n = PyParser_ParseStringFlagsFilename(str, filename,
1388 &_PyParser_Grammar,
1389 start, &err, flags);
1390 if (n == NULL)
1391 err_input(&err);
1392 return n;
1395 node *
1396 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1398 return PyParser_SimpleParseStringFlagsFilename(str, filename,
1399 start, 0);
1402 /* May want to move a more generalized form of this to parsetok.c or
1403 even parser modules. */
1405 void
1406 PyParser_SetError(perrdetail *err)
1408 err_input(err);
1411 /* Set the error appropriate to the given input error code (see errcode.h) */
1413 static void
1414 err_input(perrdetail *err)
1416 PyObject *v, *w, *errtype;
1417 PyObject* u = NULL;
1418 char *msg = NULL;
1419 errtype = PyExc_SyntaxError;
1420 v = Py_BuildValue("(ziiz)", err->filename,
1421 err->lineno, err->offset, err->text);
1422 if (err->text != NULL) {
1423 PyMem_DEL(err->text);
1424 err->text = NULL;
1426 switch (err->error) {
1427 case E_SYNTAX:
1428 errtype = PyExc_IndentationError;
1429 if (err->expected == INDENT)
1430 msg = "expected an indented block";
1431 else if (err->token == INDENT)
1432 msg = "unexpected indent";
1433 else if (err->token == DEDENT)
1434 msg = "unexpected unindent";
1435 else {
1436 errtype = PyExc_SyntaxError;
1437 msg = "invalid syntax";
1439 break;
1440 case E_TOKEN:
1441 msg = "invalid token";
1442 break;
1443 case E_EOFS:
1444 msg = "EOF while scanning triple-quoted string";
1445 break;
1446 case E_EOLS:
1447 msg = "EOL while scanning single-quoted string";
1448 break;
1449 case E_INTR:
1450 PyErr_SetNone(PyExc_KeyboardInterrupt);
1451 Py_XDECREF(v);
1452 return;
1453 case E_NOMEM:
1454 PyErr_NoMemory();
1455 Py_XDECREF(v);
1456 return;
1457 case E_EOF:
1458 msg = "unexpected EOF while parsing";
1459 break;
1460 case E_TABSPACE:
1461 errtype = PyExc_TabError;
1462 msg = "inconsistent use of tabs and spaces in indentation";
1463 break;
1464 case E_OVERFLOW:
1465 msg = "expression too long";
1466 break;
1467 case E_DEDENT:
1468 errtype = PyExc_IndentationError;
1469 msg = "unindent does not match any outer indentation level";
1470 break;
1471 case E_TOODEEP:
1472 errtype = PyExc_IndentationError;
1473 msg = "too many levels of indentation";
1474 break;
1475 case E_DECODE: { /* XXX */
1476 PyThreadState* tstate = PyThreadState_Get();
1477 PyObject* value = tstate->curexc_value;
1478 if (value != NULL) {
1479 u = PyObject_Repr(value);
1480 if (u != NULL) {
1481 msg = PyString_AsString(u);
1482 break;
1486 default:
1487 fprintf(stderr, "error=%d\n", err->error);
1488 msg = "unknown parsing error";
1489 break;
1491 w = Py_BuildValue("(sO)", msg, v);
1492 Py_XDECREF(u);
1493 Py_XDECREF(v);
1494 PyErr_SetObject(errtype, w);
1495 Py_XDECREF(w);
1498 /* Print fatal error message and abort */
1500 void
1501 Py_FatalError(const char *msg)
1503 fprintf(stderr, "Fatal Python error: %s\n", msg);
1504 #ifdef MS_WINDOWS
1505 OutputDebugString("Fatal Python error: ");
1506 OutputDebugString(msg);
1507 OutputDebugString("\n");
1508 #ifdef _DEBUG
1509 DebugBreak();
1510 #endif
1511 #endif /* MS_WINDOWS */
1512 abort();
1515 /* Clean up and exit */
1517 #ifdef WITH_THREAD
1518 #include "pythread.h"
1519 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
1520 #endif
1522 #define NEXITFUNCS 32
1523 static void (*exitfuncs[NEXITFUNCS])(void);
1524 static int nexitfuncs = 0;
1526 int Py_AtExit(void (*func)(void))
1528 if (nexitfuncs >= NEXITFUNCS)
1529 return -1;
1530 exitfuncs[nexitfuncs++] = func;
1531 return 0;
1534 static void
1535 call_sys_exitfunc(void)
1537 PyObject *exitfunc = PySys_GetObject("exitfunc");
1539 if (exitfunc) {
1540 PyObject *res;
1541 Py_INCREF(exitfunc);
1542 PySys_SetObject("exitfunc", (PyObject *)NULL);
1543 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1544 if (res == NULL) {
1545 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
1546 PySys_WriteStderr("Error in sys.exitfunc:\n");
1548 PyErr_Print();
1550 Py_DECREF(exitfunc);
1553 if (Py_FlushLine())
1554 PyErr_Clear();
1557 static void
1558 call_ll_exitfuncs(void)
1560 while (nexitfuncs > 0)
1561 (*exitfuncs[--nexitfuncs])();
1563 fflush(stdout);
1564 fflush(stderr);
1567 void
1568 Py_Exit(int sts)
1570 Py_Finalize();
1572 #ifdef macintosh
1573 PyMac_Exit(sts);
1574 #else
1575 exit(sts);
1576 #endif
1579 static void
1580 initsigs(void)
1582 #ifdef HAVE_SIGNAL_H
1583 #ifdef SIGPIPE
1584 signal(SIGPIPE, SIG_IGN);
1585 #endif
1586 #ifdef SIGXFZ
1587 signal(SIGXFZ, SIG_IGN);
1588 #endif
1589 #ifdef SIGXFSZ
1590 signal(SIGXFSZ, SIG_IGN);
1591 #endif
1592 #endif /* HAVE_SIGNAL_H */
1593 PyOS_InitInterrupts(); /* May imply initsignal() */
1596 #ifdef MPW
1598 /* Check for file descriptor connected to interactive device.
1599 Pretend that stdin is always interactive, other files never. */
1602 isatty(int fd)
1604 return fd == fileno(stdin);
1607 #endif
1610 * The file descriptor fd is considered ``interactive'' if either
1611 * a) isatty(fd) is TRUE, or
1612 * b) the -i flag was given, and the filename associated with
1613 * the descriptor is NULL or "<stdin>" or "???".
1616 Py_FdIsInteractive(FILE *fp, const char *filename)
1618 if (isatty((int)fileno(fp)))
1619 return 1;
1620 if (!Py_InteractiveFlag)
1621 return 0;
1622 return (filename == NULL) ||
1623 (strcmp(filename, "<stdin>") == 0) ||
1624 (strcmp(filename, "???") == 0);
1628 #if defined(USE_STACKCHECK)
1629 #if defined(WIN32) && defined(_MSC_VER)
1631 /* Stack checking for Microsoft C */
1633 #include <malloc.h>
1634 #include <excpt.h>
1637 * Return non-zero when we run out of memory on the stack; zero otherwise.
1640 PyOS_CheckStack(void)
1642 __try {
1643 /* alloca throws a stack overflow exception if there's
1644 not enough space left on the stack */
1645 alloca(PYOS_STACK_MARGIN * sizeof(void*));
1646 return 0;
1647 } __except (EXCEPTION_EXECUTE_HANDLER) {
1648 /* just ignore all errors */
1650 return 1;
1653 #endif /* WIN32 && _MSC_VER */
1655 /* Alternate implementations can be added here... */
1657 #endif /* USE_STACKCHECK */
1660 /* Wrappers around sigaction() or signal(). */
1662 PyOS_sighandler_t
1663 PyOS_getsig(int sig)
1665 #ifdef HAVE_SIGACTION
1666 struct sigaction context;
1667 /* Initialize context.sa_handler to SIG_ERR which makes about as
1668 * much sense as anything else. It should get overwritten if
1669 * sigaction actually succeeds and otherwise we avoid an
1670 * uninitialized memory read.
1672 context.sa_handler = SIG_ERR;
1673 sigaction(sig, NULL, &context);
1674 return context.sa_handler;
1675 #else
1676 PyOS_sighandler_t handler;
1677 handler = signal(sig, SIG_IGN);
1678 signal(sig, handler);
1679 return handler;
1680 #endif
1683 PyOS_sighandler_t
1684 PyOS_setsig(int sig, PyOS_sighandler_t handler)
1686 #ifdef HAVE_SIGACTION
1687 struct sigaction context;
1688 PyOS_sighandler_t oldhandler;
1689 /* Initialize context.sa_handler to SIG_ERR which makes about as
1690 * much sense as anything else. It should get overwritten if
1691 * sigaction actually succeeds and otherwise we avoid an
1692 * uninitialized memory read.
1694 context.sa_handler = SIG_ERR;
1695 sigaction(sig, NULL, &context);
1696 oldhandler = context.sa_handler;
1697 context.sa_handler = handler;
1698 sigaction(sig, &context, NULL);
1699 return oldhandler;
1700 #else
1701 return signal(sig, handler);
1702 #endif