comparison cos/python/Python/pythonrun.c @ 27:7f74363f4c82

Added some files for the python port
author windel
date Tue, 27 Dec 2011 18:59:02 +0100
parents
children
comparison
equal deleted inserted replaced
26:dcce92b1efbc 27:7f74363f4c82
1
2 /* Python interpreter top-level routines, including init/exit */
3
4 #include "Python.h"
5
6 #include "Python-ast.h"
7 #undef Yield /* undefine macro conflicting with winbase.h */
8 #include "grammar.h"
9 #include "node.h"
10 #include "token.h"
11 #include "parsetok.h"
12 #include "errcode.h"
13 #include "code.h"
14 #include "symtable.h"
15 #include "ast.h"
16 #include "marshal.h"
17 #include "osdefs.h"
18
19 #define PRINT_TOTAL_REFS()
20
21 extern wchar_t *Py_GetPath(void);
22
23 extern grammar _PyParser_Grammar; /* From graminit.c */
24
25 /* Forward */
26 static void initmain(void);
27 static int initfsencoding(PyInterpreterState *interp);
28 static void initsite(void);
29 static int initstdio(void);
30 static void flush_io(void);
31 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
32 PyCompilerFlags *, PyArena *);
33 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
34 PyCompilerFlags *);
35 static void err_input(perrdetail *);
36 static void err_free(perrdetail *);
37 static void initsigs(void);
38 static void call_py_exitfuncs(void);
39 static void wait_for_thread_shutdown(void);
40 static void call_ll_exitfuncs(void);
41 extern int _PyUnicode_Init(void);
42 extern void _PyUnicode_Fini(void);
43 extern int _PyLong_Init(void);
44 extern void PyLong_Fini(void);
45 extern int _PyFaulthandler_Init(void);
46 extern void _PyFaulthandler_Fini(void);
47
48 #ifdef WITH_THREAD
49 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
50 extern void _PyGILState_Fini(void);
51 #endif /* WITH_THREAD */
52
53 int Py_DebugFlag; /* Needed by parser.c */
54 int Py_VerboseFlag; /* Needed by import.c */
55 int Py_QuietFlag; /* Needed by sysmodule.c */
56 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
57 int Py_InspectFlag; /* Needed to determine whether to exit at SystemExit */
58 int Py_NoSiteFlag; /* Suppress 'import site' */
59 int Py_BytesWarningFlag; /* Warn on str(bytes) and str(buffer) */
60 int Py_DontWriteBytecodeFlag; /* Suppress writing bytecode files (*.py[co]) */
61 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
62 int Py_FrozenFlag; /* Needed by getpath.c */
63 int Py_IgnoreEnvironmentFlag; /* e.g. PYTHONPATH, PYTHONHOME */
64 int Py_NoUserSiteDirectory = 0; /* for -s and site.py */
65 int Py_UnbufferedStdioFlag = 0; /* Unbuffered binary std{in,out,err} */
66
67 PyThreadState *_Py_Finalizing = NULL;
68
69 /* PyModule_GetWarningsModule is no longer necessary as of 2.6
70 since _warnings is builtin. This API should not be used. */
71 PyObject *
72 PyModule_GetWarningsModule(void)
73 {
74 return PyImport_ImportModule("warnings");
75 }
76
77 static int initialized = 0;
78
79 /* API to access the initialized flag -- useful for esoteric use */
80
81 int
82 Py_IsInitialized(void)
83 {
84 return initialized;
85 }
86
87 /* Global initializations. Can be undone by Py_Finalize(). Don't
88 call this twice without an intervening Py_Finalize() call. When
89 initializations fail, a fatal error is issued and the function does
90 not return. On return, the first thread and interpreter state have
91 been created.
92
93 Locking: you must hold the interpreter lock while calling this.
94 (If the lock has not yet been initialized, that's equivalent to
95 having the lock, but you cannot use multiple threads.)
96
97 */
98
99 static int
100 add_flag(int flag, const char *envs)
101 {
102 int env = atoi(envs);
103 if (flag < env)
104 flag = env;
105 if (flag < 1)
106 flag = 1;
107 return flag;
108 }
109
110 static char*
111 get_codec_name(const char *encoding)
112 {
113 char *name_utf8, *name_str;
114 PyObject *codec, *name = NULL;
115 _Py_IDENTIFIER(name);
116
117 codec = _PyCodec_Lookup(encoding);
118 if (!codec)
119 goto error;
120
121 name = _PyObject_GetAttrId(codec, &PyId_name);
122 Py_CLEAR(codec);
123 if (!name)
124 goto error;
125
126 name_utf8 = _PyUnicode_AsString(name);
127 if (name_utf8 == NULL)
128 goto error;
129 name_str = strdup(name_utf8);
130 Py_DECREF(name);
131 if (name_str == NULL) {
132 PyErr_NoMemory();
133 return NULL;
134 }
135 return name_str;
136
137 error:
138 Py_XDECREF(codec);
139 Py_XDECREF(name);
140 return NULL;
141 }
142
143 static char*
144 get_locale_encoding(void)
145 {
146 PyErr_SetNone(PyExc_NotImplementedError);
147 return NULL;
148 }
149
150 void
151 Py_InitializeEx(int install_sigs)
152 {
153 PyInterpreterState *interp;
154 PyThreadState *tstate;
155 PyObject *bimod, *sysmod, *pstderr;
156 char *p;
157 extern void _Py_ReadyTypes(void);
158
159 if (initialized)
160 return;
161 initialized = 1;
162 _Py_Finalizing = NULL;
163
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);
170 if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
171 Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
172
173 interp = PyInterpreterState_New();
174 if (interp == NULL)
175 Py_FatalError("Py_Initialize: can't make first interpreter");
176
177 tstate = PyThreadState_New(interp);
178 if (tstate == NULL)
179 Py_FatalError("Py_Initialize: can't make first thread");
180 (void) PyThreadState_Swap(tstate);
181
182 #ifdef WITH_THREAD
183 /* We can't call _PyEval_FiniThreads() in Py_Finalize because
184 destroying the GIL might fail when it is being referenced from
185 another running thread (see issue #9901).
186 Instead we destroy the previously created GIL here, which ensures
187 that we can call Py_Initialize / Py_Finalize multiple times. */
188 _PyEval_FiniThreads();
189
190 /* Auto-thread-state API */
191 _PyGILState_Init(interp, tstate);
192 #endif /* WITH_THREAD */
193
194 _Py_ReadyTypes();
195
196 if (!_PyFrame_Init())
197 Py_FatalError("Py_Initialize: can't init frames");
198
199 if (!_PyLong_Init())
200 Py_FatalError("Py_Initialize: can't init longs");
201
202 if (!PyByteArray_Init())
203 Py_FatalError("Py_Initialize: can't init bytearray");
204
205 _PyFloat_Init();
206
207 interp->modules = PyDict_New();
208 if (interp->modules == NULL)
209 Py_FatalError("Py_Initialize: can't make modules dictionary");
210 interp->modules_reloading = PyDict_New();
211 if (interp->modules_reloading == NULL)
212 Py_FatalError("Py_Initialize: can't make modules_reloading dictionary");
213
214 /* Init Unicode implementation; relies on the codec registry */
215 if (_PyUnicode_Init() < 0)
216 Py_FatalError("Py_Initialize: can't initialize unicode");
217
218 bimod = _PyBuiltin_Init();
219 if (bimod == NULL)
220 Py_FatalError("Py_Initialize: can't initialize builtins modules");
221 _PyImport_FixupBuiltin(bimod, "builtins");
222 interp->builtins = PyModule_GetDict(bimod);
223 if (interp->builtins == NULL)
224 Py_FatalError("Py_Initialize: can't initialize builtins dict");
225 Py_INCREF(interp->builtins);
226
227 /* initialize builtin exceptions */
228 _PyExc_Init();
229
230 sysmod = _PySys_Init();
231 if (sysmod == NULL)
232 Py_FatalError("Py_Initialize: can't initialize sys");
233 interp->sysdict = PyModule_GetDict(sysmod);
234 if (interp->sysdict == NULL)
235 Py_FatalError("Py_Initialize: can't initialize sys dict");
236 Py_INCREF(interp->sysdict);
237 _PyImport_FixupBuiltin(sysmod, "sys");
238 PySys_SetPath(Py_GetPath());
239 PyDict_SetItemString(interp->sysdict, "modules",
240 interp->modules);
241
242 /* Set up a preliminary stderr printer until we have enough
243 infrastructure for the io module in place. */
244 pstderr = PyFile_NewStdPrinter(fileno(stderr));
245 if (pstderr == NULL)
246 Py_FatalError("Py_Initialize: can't set preliminary stderr");
247 PySys_SetObject("stderr", pstderr);
248 PySys_SetObject("__stderr__", pstderr);
249 Py_DECREF(pstderr);
250
251 _PyImport_Init();
252
253 _PyImportHooks_Init();
254
255 /* initialize the faulthandler module */
256 if (_PyFaulthandler_Init())
257 Py_FatalError("Py_Initialize: can't initialize faulthandler");
258
259 /* Initialize _warnings. */
260 _PyWarnings_Init();
261
262 _PyTime_Init();
263
264 if (initfsencoding(interp) < 0)
265 Py_FatalError("Py_Initialize: unable to load the file system codec");
266
267 if (install_sigs)
268 initsigs(); /* Signal handling stuff, including initintr() */
269
270 initmain(); /* Module __main__ */
271 if (initstdio() < 0)
272 Py_FatalError(
273 "Py_Initialize: can't initialize sys standard streams");
274
275 /* Initialize warnings. */
276 if (PySys_HasWarnOptions()) {
277 PyObject *warnings_module = PyImport_ImportModule("warnings");
278 if (warnings_module == NULL) {
279 fprintf(stderr, "'import warnings' failed; traceback:\n");
280 PyErr_Print();
281 }
282 Py_XDECREF(warnings_module);
283 }
284
285 if (!Py_NoSiteFlag)
286 initsite(); /* Module site */
287 }
288
289 void
290 Py_Initialize(void)
291 {
292 Py_InitializeEx(1);
293 }
294
295 /* Flush stdout and stderr */
296
297 static int
298 file_is_closed(PyObject *fobj)
299 {
300 int r;
301 PyObject *tmp = PyObject_GetAttrString(fobj, "closed");
302 if (tmp == NULL) {
303 PyErr_Clear();
304 return 0;
305 }
306 r = PyObject_IsTrue(tmp);
307 Py_DECREF(tmp);
308 if (r < 0)
309 PyErr_Clear();
310 return r > 0;
311 }
312
313 static void
314 flush_std_files(void)
315 {
316 PyObject *fout = PySys_GetObject("stdout");
317 PyObject *ferr = PySys_GetObject("stderr");
318 PyObject *tmp;
319 _Py_IDENTIFIER(flush);
320
321 if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
322 tmp = _PyObject_CallMethodId(fout, &PyId_flush, "");
323 if (tmp == NULL)
324 PyErr_WriteUnraisable(fout);
325 else
326 Py_DECREF(tmp);
327 }
328
329 if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
330 tmp = _PyObject_CallMethodId(ferr, &PyId_flush, "");
331 if (tmp == NULL)
332 PyErr_Clear();
333 else
334 Py_DECREF(tmp);
335 }
336 }
337
338 /* Undo the effect of Py_Initialize().
339
340 Beware: if multiple interpreter and/or thread states exist, these
341 are not wiped out; only the current thread and interpreter state
342 are deleted. But since everything else is deleted, those other
343 interpreter and thread states should no longer be used.
344
345 (XXX We should do better, e.g. wipe out all interpreters and
346 threads.)
347
348 Locking: as above.
349
350 */
351
352 void
353 Py_Finalize(void)
354 {
355 PyInterpreterState *interp;
356 PyThreadState *tstate;
357
358 if (!initialized)
359 return;
360
361 wait_for_thread_shutdown();
362
363 /* The interpreter is still entirely intact at this point, and the
364 * exit funcs may be relying on that. In particular, if some thread
365 * or exit func is still waiting to do an import, the import machinery
366 * expects Py_IsInitialized() to return true. So don't say the
367 * interpreter is uninitialized until after the exit funcs have run.
368 * Note that Threading.py uses an exit func to do a join on all the
369 * threads created thru it, so this also protects pending imports in
370 * the threads created via Threading.
371 */
372 call_py_exitfuncs();
373
374 /* Get current thread state and interpreter pointer */
375 tstate = PyThreadState_GET();
376 interp = tstate->interp;
377
378 /* Remaining threads (e.g. daemon threads) will automatically exit
379 after taking the GIL (in PyEval_RestoreThread()). */
380 _Py_Finalizing = tstate;
381 initialized = 0;
382
383 /* Flush stdout+stderr */
384 flush_std_files();
385
386 /* Disable signal handling */
387 PyOS_FiniInterrupts();
388
389 /* Clear type lookup cache */
390 PyType_ClearCache();
391
392 /* Collect garbage. This may call finalizers; it's nice to call these
393 * before all modules are destroyed.
394 * XXX If a __del__ or weakref callback is triggered here, and tries to
395 * XXX import a module, bad things can happen, because Python no
396 * XXX longer believes it's initialized.
397 * XXX Fatal Python error: Interpreter not initialized (version mismatch?)
398 * XXX is easy to provoke that way. I've also seen, e.g.,
399 * XXX Exception exceptions.ImportError: 'No module named sha'
400 * XXX in <function callback at 0x008F5718> ignored
401 * XXX but I'm unclear on exactly how that one happens. In any case,
402 * XXX I haven't seen a real-life report of either of these.
403 */
404 PyGC_Collect();
405 /* We run this while most interpreter state is still alive, so that
406 debug information can be printed out */
407 _PyGC_Fini();
408
409 /* Destroy all modules */
410 PyImport_Cleanup();
411
412 /* Flush stdout+stderr (again, in case more was printed) */
413 flush_std_files();
414
415 /* Collect final garbage. This disposes of cycles created by
416 * new-style class definitions, for example.
417 * XXX This is disabled because it caused too many problems. If
418 * XXX a __del__ or weakref callback triggers here, Python code has
419 * XXX a hard time running, because even the sys module has been
420 * XXX cleared out (sys.stdout is gone, sys.excepthook is gone, etc).
421 * XXX One symptom is a sequence of information-free messages
422 * XXX coming from threads (if a __del__ or callback is invoked,
423 * XXX other threads can execute too, and any exception they encounter
424 * XXX triggers a comedy of errors as subsystem after subsystem
425 * XXX fails to find what it *expects* to find in sys to help report
426 * XXX the exception and consequent unexpected failures). I've also
427 * XXX seen segfaults then, after adding print statements to the
428 * XXX Python code getting called.
429 */
430
431 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
432 _PyImport_Fini();
433
434 /* unload faulthandler module */
435 _PyFaulthandler_Fini();
436
437 /* Clear interpreter state */
438 PyInterpreterState_Clear(interp);
439
440 /* Now we decref the exception classes. After this point nothing
441 can raise an exception. That's okay, because each Fini() method
442 below has been checked to make sure no exceptions are ever
443 raised.
444 */
445
446 _PyExc_Fini();
447
448 /* Cleanup auto-thread-state */
449 #ifdef WITH_THREAD
450 _PyGILState_Fini();
451 #endif /* WITH_THREAD */
452
453 /* Delete current thread */
454 PyThreadState_Swap(NULL);
455 PyInterpreterState_Delete(interp);
456
457 /* Sundry finalizers */
458 PyMethod_Fini();
459 PyFrame_Fini();
460 PyCFunction_Fini();
461 PyTuple_Fini();
462 PyList_Fini();
463 PySet_Fini();
464 PyBytes_Fini();
465 PyByteArray_Fini();
466 PyLong_Fini();
467 PyFloat_Fini();
468 PyDict_Fini();
469 PySlice_Fini();
470
471 /* Cleanup Unicode implementation */
472 _PyUnicode_Fini();
473
474 /* reset file system default encoding */
475 if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
476 free((char*)Py_FileSystemDefaultEncoding);
477 Py_FileSystemDefaultEncoding = NULL;
478 }
479
480 /* XXX Still allocated:
481 - various static ad-hoc pointers to interned strings
482 - int and float free list blocks
483 - whatever various modules and libraries allocate
484 */
485
486 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
487
488 call_ll_exitfuncs();
489 }
490
491 /* Create and initialize a new interpreter and thread, and return the
492 new thread. This requires that Py_Initialize() has been called
493 first.
494
495 Unsuccessful initialization yields a NULL pointer. Note that *no*
496 exception information is available even in this case -- the
497 exception information is held in the thread, and there is no
498 thread.
499
500 Locking: as above.
501
502 */
503
504 PyThreadState *
505 Py_NewInterpreter(void)
506 {
507 PyInterpreterState *interp;
508 PyThreadState *tstate, *save_tstate;
509 PyObject *bimod, *sysmod;
510
511 if (!initialized)
512 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
513
514 interp = PyInterpreterState_New();
515 if (interp == NULL)
516 return NULL;
517
518 tstate = PyThreadState_New(interp);
519 if (tstate == NULL) {
520 PyInterpreterState_Delete(interp);
521 return NULL;
522 }
523
524 save_tstate = PyThreadState_Swap(tstate);
525
526 /* XXX The following is lax in error checking */
527
528 interp->modules = PyDict_New();
529 interp->modules_reloading = PyDict_New();
530
531 bimod = _PyImport_FindBuiltin("builtins");
532 if (bimod != NULL) {
533 interp->builtins = PyModule_GetDict(bimod);
534 if (interp->builtins == NULL)
535 goto handle_error;
536 Py_INCREF(interp->builtins);
537 }
538
539 /* initialize builtin exceptions */
540 _PyExc_Init();
541
542 sysmod = _PyImport_FindBuiltin("sys");
543 if (bimod != NULL && sysmod != NULL) {
544 PyObject *pstderr;
545 interp->sysdict = PyModule_GetDict(sysmod);
546 if (interp->sysdict == NULL)
547 goto handle_error;
548 Py_INCREF(interp->sysdict);
549 PySys_SetPath(Py_GetPath());
550 PyDict_SetItemString(interp->sysdict, "modules",
551 interp->modules);
552 /* Set up a preliminary stderr printer until we have enough
553 infrastructure for the io module in place. */
554 pstderr = PyFile_NewStdPrinter(fileno(stderr));
555 if (pstderr == NULL)
556 Py_FatalError("Py_Initialize: can't set preliminary stderr");
557 PySys_SetObject("stderr", pstderr);
558 PySys_SetObject("__stderr__", pstderr);
559 Py_DECREF(pstderr);
560
561 _PyImportHooks_Init();
562
563 if (initfsencoding(interp) < 0)
564 goto handle_error;
565
566 if (initstdio() < 0)
567 Py_FatalError(
568 "Py_Initialize: can't initialize sys standard streams");
569 initmain();
570 if (!Py_NoSiteFlag)
571 initsite();
572 }
573
574 if (!PyErr_Occurred())
575 return tstate;
576
577 handle_error:
578 /* Oops, it didn't work. Undo it all. */
579
580 PyErr_PrintEx(0);
581 PyThreadState_Clear(tstate);
582 PyThreadState_Swap(save_tstate);
583 PyThreadState_Delete(tstate);
584 PyInterpreterState_Delete(interp);
585
586 return NULL;
587 }
588
589 /* Delete an interpreter and its last thread. This requires that the
590 given thread state is current, that the thread has no remaining
591 frames, and that it is its interpreter's only remaining thread.
592 It is a fatal error to violate these constraints.
593
594 (Py_Finalize() doesn't have these constraints -- it zaps
595 everything, regardless.)
596
597 Locking: as above.
598
599 */
600
601 void
602 Py_EndInterpreter(PyThreadState *tstate)
603 {
604 PyInterpreterState *interp = tstate->interp;
605
606 if (tstate != PyThreadState_GET())
607 Py_FatalError("Py_EndInterpreter: thread is not current");
608 if (tstate->frame != NULL)
609 Py_FatalError("Py_EndInterpreter: thread still has a frame");
610 if (tstate != interp->tstate_head || tstate->next != NULL)
611 Py_FatalError("Py_EndInterpreter: not the last thread");
612
613 PyImport_Cleanup();
614 PyInterpreterState_Clear(interp);
615 PyThreadState_Swap(NULL);
616 PyInterpreterState_Delete(interp);
617 }
618
619 static wchar_t *progname = L"python";
620
621 void
622 Py_SetProgramName(wchar_t *pn)
623 {
624 if (pn && *pn)
625 progname = pn;
626 }
627
628 wchar_t *
629 Py_GetProgramName(void)
630 {
631 return progname;
632 }
633
634 static wchar_t *default_home = NULL;
635 static wchar_t env_home[PATH_MAX+1];
636
637 void
638 Py_SetPythonHome(wchar_t *home)
639 {
640 default_home = home;
641 }
642
643 wchar_t *
644 Py_GetPythonHome(void)
645 {
646 wchar_t *home = default_home;
647 if (home == NULL && !Py_IgnoreEnvironmentFlag) {
648 char* chome = Py_GETENV("PYTHONHOME");
649 if (chome) {
650 size_t r = mbstowcs(env_home, chome, PATH_MAX+1);
651 if (r != (size_t)-1 && r <= PATH_MAX)
652 home = env_home;
653 }
654
655 }
656 return home;
657 }
658
659 /* Create __main__ module */
660
661 static void
662 initmain(void)
663 {
664 PyObject *m, *d;
665 m = PyImport_AddModule("__main__");
666 if (m == NULL)
667 Py_FatalError("can't create __main__ module");
668 d = PyModule_GetDict(m);
669 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
670 PyObject *bimod = PyImport_ImportModule("builtins");
671 if (bimod == NULL ||
672 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
673 Py_FatalError("can't add __builtins__ to __main__");
674 Py_DECREF(bimod);
675 }
676 }
677
678 static int
679 initfsencoding(PyInterpreterState *interp)
680 {
681 PyObject *codec;
682
683 if (Py_FileSystemDefaultEncoding == NULL)
684 {
685 Py_FileSystemDefaultEncoding = get_locale_encoding();
686 if (Py_FileSystemDefaultEncoding == NULL)
687 Py_FatalError("Py_Initialize: Unable to get the locale encoding");
688
689 Py_HasFileSystemDefaultEncoding = 0;
690 interp->fscodec_initialized = 1;
691 return 0;
692 }
693
694 /* the encoding is mbcs, utf-8 or ascii */
695 codec = _PyCodec_Lookup(Py_FileSystemDefaultEncoding);
696 if (!codec) {
697 /* Such error can only occurs in critical situations: no more
698 * memory, import a module of the standard library failed,
699 * etc. */
700 return -1;
701 }
702 Py_DECREF(codec);
703 interp->fscodec_initialized = 1;
704 return 0;
705 }
706
707 /* Import the site module (not into __main__ though) */
708
709 static void
710 initsite(void)
711 {
712 PyObject *m;
713 m = PyImport_ImportModule("site");
714 if (m == NULL) {
715 PyErr_Print();
716 Py_Finalize();
717 exit(1);
718 }
719 else {
720 Py_DECREF(m);
721 }
722 }
723
724 static PyObject*
725 create_stdio(PyObject* io,
726 int fd, int write_mode, char* name,
727 char* encoding, char* errors)
728 {
729 PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
730 const char* mode;
731 const char* newline;
732 PyObject *line_buffering;
733 int buffering, isatty;
734 _Py_IDENTIFIER(open);
735 _Py_IDENTIFIER(isatty);
736 _Py_IDENTIFIER(TextIOWrapper);
737 _Py_IDENTIFIER(name);
738 _Py_IDENTIFIER(mode);
739
740 /* stdin is always opened in buffered mode, first because it shouldn't
741 make a difference in common use cases, second because TextIOWrapper
742 depends on the presence of a read1() method which only exists on
743 buffered streams.
744 */
745 if (Py_UnbufferedStdioFlag && write_mode)
746 buffering = 0;
747 else
748 buffering = -1;
749 if (write_mode)
750 mode = "wb";
751 else
752 mode = "rb";
753 buf = _PyObject_CallMethodId(io, &PyId_open, "isiOOOi",
754 fd, mode, buffering,
755 Py_None, Py_None, Py_None, 0);
756 if (buf == NULL)
757 goto error;
758
759 if (buffering) {
760 _Py_IDENTIFIER(raw);
761 raw = _PyObject_GetAttrId(buf, &PyId_raw);
762 if (raw == NULL)
763 goto error;
764 }
765 else {
766 raw = buf;
767 Py_INCREF(raw);
768 }
769
770 text = PyUnicode_FromString(name);
771 if (text == NULL || _PyObject_SetAttrId(raw, &PyId_name, text) < 0)
772 goto error;
773 res = _PyObject_CallMethodId(raw, &PyId_isatty, "");
774 if (res == NULL)
775 goto error;
776 isatty = PyObject_IsTrue(res);
777 Py_DECREF(res);
778 if (isatty == -1)
779 goto error;
780 if (isatty || Py_UnbufferedStdioFlag)
781 line_buffering = Py_True;
782 else
783 line_buffering = Py_False;
784
785 Py_CLEAR(raw);
786 Py_CLEAR(text);
787
788 newline = "\n";
789
790 stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssO",
791 buf, encoding, errors,
792 newline, line_buffering);
793 Py_CLEAR(buf);
794 if (stream == NULL)
795 goto error;
796
797 if (write_mode)
798 mode = "w";
799 else
800 mode = "r";
801 text = PyUnicode_FromString(mode);
802 if (!text || _PyObject_SetAttrId(stream, &PyId_mode, text) < 0)
803 goto error;
804 Py_CLEAR(text);
805 return stream;
806
807 error:
808 Py_XDECREF(buf);
809 Py_XDECREF(stream);
810 Py_XDECREF(text);
811 Py_XDECREF(raw);
812 return NULL;
813 }
814
815 static int
816 is_valid_fd(int fd)
817 {
818 int dummy_fd;
819 if (fd < 0 || !_PyVerify_fd(fd))
820 return 0;
821 dummy_fd = dup(fd);
822 if (dummy_fd < 0)
823 return 0;
824 close(dummy_fd);
825 return 1;
826 }
827
828 /* Initialize sys.stdin, stdout, stderr and builtins.open */
829 static int
830 initstdio(void)
831 {
832 PyObject *iomod = NULL, *wrapper;
833 PyObject *bimod = NULL;
834 PyObject *m;
835 PyObject *std = NULL;
836 int status = 0, fd;
837 PyObject * encoding_attr;
838 char *encoding = NULL, *errors;
839
840 /* Hack to avoid a nasty recursion issue when Python is invoked
841 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
842 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
843 goto error;
844 }
845 Py_DECREF(m);
846
847 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
848 goto error;
849 }
850 Py_DECREF(m);
851
852 if (!(bimod = PyImport_ImportModule("builtins"))) {
853 goto error;
854 }
855
856 if (!(iomod = PyImport_ImportModule("io"))) {
857 goto error;
858 }
859 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
860 goto error;
861 }
862
863 /* Set builtins.open */
864 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
865 Py_DECREF(wrapper);
866 goto error;
867 }
868 Py_DECREF(wrapper);
869
870 encoding = Py_GETENV("PYTHONIOENCODING");
871 errors = NULL;
872 if (encoding) {
873 encoding = strdup(encoding);
874 errors = strchr(encoding, ':');
875 if (errors) {
876 *errors = '\0';
877 errors++;
878 }
879 }
880
881 /* Set sys.stdin */
882 fd = fileno(stdin);
883 /* Under some conditions stdin, stdout and stderr may not be connected
884 * and fileno() may point to an invalid file descriptor. For example
885 * GUI apps don't have valid standard streams by default.
886 */
887 if (!is_valid_fd(fd)) {
888 std = Py_None;
889 Py_INCREF(std);
890 }
891 else {
892 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
893 if (std == NULL)
894 goto error;
895 } /* if (fd < 0) */
896 PySys_SetObject("__stdin__", std);
897 PySys_SetObject("stdin", std);
898 Py_DECREF(std);
899
900 /* Set sys.stdout */
901 fd = fileno(stdout);
902 if (!is_valid_fd(fd)) {
903 std = Py_None;
904 Py_INCREF(std);
905 }
906 else {
907 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
908 if (std == NULL)
909 goto error;
910 } /* if (fd < 0) */
911 PySys_SetObject("__stdout__", std);
912 PySys_SetObject("stdout", std);
913 Py_DECREF(std);
914
915 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
916 /* Set sys.stderr, replaces the preliminary stderr */
917 fd = fileno(stderr);
918 if (!is_valid_fd(fd)) {
919 std = Py_None;
920 Py_INCREF(std);
921 }
922 else {
923 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");
924 if (std == NULL)
925 goto error;
926 } /* if (fd < 0) */
927
928 /* Same as hack above, pre-import stderr's codec to avoid recursion
929 when import.c tries to write to stderr in verbose mode. */
930 encoding_attr = PyObject_GetAttrString(std, "encoding");
931 if (encoding_attr != NULL) {
932 const char * encoding;
933 encoding = _PyUnicode_AsString(encoding_attr);
934 if (encoding != NULL) {
935 _PyCodec_Lookup(encoding);
936 }
937 Py_DECREF(encoding_attr);
938 }
939 PyErr_Clear(); /* Not a fatal error if codec isn't available */
940
941 PySys_SetObject("__stderr__", std);
942 PySys_SetObject("stderr", std);
943 Py_DECREF(std);
944 #endif
945
946 if (0) {
947 error:
948 status = -1;
949 }
950
951 if (encoding)
952 free(encoding);
953 Py_XDECREF(bimod);
954 Py_XDECREF(iomod);
955 return status;
956 }
957
958 /* Parse input from a file and execute it */
959
960 int
961 PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
962 PyCompilerFlags *flags)
963 {
964 if (filename == NULL)
965 filename = "???";
966 if (Py_FdIsInteractive(fp, filename)) {
967 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
968 if (closeit)
969 fclose(fp);
970 return err;
971 }
972 else
973 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
974 }
975
976 int
977 PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
978 {
979 PyObject *v;
980 int ret;
981 PyCompilerFlags local_flags;
982
983 if (flags == NULL) {
984 flags = &local_flags;
985 local_flags.cf_flags = 0;
986 }
987 v = PySys_GetObject("ps1");
988 if (v == NULL) {
989 PySys_SetObject("ps1", v = PyUnicode_FromString(">>> "));
990 Py_XDECREF(v);
991 }
992 v = PySys_GetObject("ps2");
993 if (v == NULL) {
994 PySys_SetObject("ps2", v = PyUnicode_FromString("... "));
995 Py_XDECREF(v);
996 }
997 for (;;) {
998 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
999 PRINT_TOTAL_REFS();
1000 if (ret == E_EOF)
1001 return 0;
1002 /*
1003 if (ret == E_NOMEM)
1004 return -1;
1005 */
1006 }
1007 }
1008
1009 /* compute parser flags based on compiler flags */
1010 static int PARSER_FLAGS(PyCompilerFlags *flags)
1011 {
1012 int parser_flags = 0;
1013 if (!flags)
1014 return 0;
1015 if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
1016 parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
1017 if (flags->cf_flags & PyCF_IGNORE_COOKIE)
1018 parser_flags |= PyPARSE_IGNORE_COOKIE;
1019 if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
1020 parser_flags |= PyPARSE_BARRY_AS_BDFL;
1021 return parser_flags;
1022 }
1023
1024 int
1025 PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
1026 {
1027 PyObject *m, *d, *v, *w, *oenc = NULL;
1028 mod_ty mod;
1029 PyArena *arena;
1030 char *ps1 = "", *ps2 = "", *enc = NULL;
1031 int errcode = 0;
1032 _Py_IDENTIFIER(encoding);
1033
1034 if (fp == stdin) {
1035 /* Fetch encoding from sys.stdin */
1036 v = PySys_GetObject("stdin");
1037 if (v == NULL || v == Py_None)
1038 return -1;
1039 oenc = _PyObject_GetAttrId(v, &PyId_encoding);
1040 if (!oenc)
1041 return -1;
1042 enc = _PyUnicode_AsString(oenc);
1043 if (enc == NULL)
1044 return -1;
1045 }
1046 v = PySys_GetObject("ps1");
1047 if (v != NULL) {
1048 v = PyObject_Str(v);
1049 if (v == NULL)
1050 PyErr_Clear();
1051 else if (PyUnicode_Check(v)) {
1052 ps1 = _PyUnicode_AsString(v);
1053 if (ps1 == NULL) {
1054 PyErr_Clear();
1055 ps1 = "";
1056 }
1057 }
1058 }
1059 w = PySys_GetObject("ps2");
1060 if (w != NULL) {
1061 w = PyObject_Str(w);
1062 if (w == NULL)
1063 PyErr_Clear();
1064 else if (PyUnicode_Check(w)) {
1065 ps2 = _PyUnicode_AsString(w);
1066 if (ps2 == NULL) {
1067 PyErr_Clear();
1068 ps2 = "";
1069 }
1070 }
1071 }
1072 arena = PyArena_New();
1073 if (arena == NULL) {
1074 Py_XDECREF(v);
1075 Py_XDECREF(w);
1076 Py_XDECREF(oenc);
1077 return -1;
1078 }
1079 mod = PyParser_ASTFromFile(fp, filename, enc,
1080 Py_single_input, ps1, ps2,
1081 flags, &errcode, arena);
1082 Py_XDECREF(v);
1083 Py_XDECREF(w);
1084 Py_XDECREF(oenc);
1085 if (mod == NULL) {
1086 PyArena_Free(arena);
1087 if (errcode == E_EOF) {
1088 PyErr_Clear();
1089 return E_EOF;
1090 }
1091 PyErr_Print();
1092 return -1;
1093 }
1094 m = PyImport_AddModule("__main__");
1095 if (m == NULL) {
1096 PyArena_Free(arena);
1097 return -1;
1098 }
1099 d = PyModule_GetDict(m);
1100 v = run_mod(mod, filename, d, d, flags, arena);
1101 PyArena_Free(arena);
1102 flush_io();
1103 if (v == NULL) {
1104 PyErr_Print();
1105 return -1;
1106 }
1107 Py_DECREF(v);
1108 return 0;
1109 }
1110
1111 /* Check whether a file maybe a pyc file: Look at the extension,
1112 the file type, and, if we may close it, at the first few bytes. */
1113
1114 static int
1115 maybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
1116 {
1117 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
1118 return 1;
1119
1120 /* Only look into the file if we are allowed to close it, since
1121 it then should also be seekable. */
1122 if (closeit) {
1123 /* Read only two bytes of the magic. If the file was opened in
1124 text mode, the bytes 3 and 4 of the magic (\r\n) might not
1125 be read as they are on disk. */
1126 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
1127 unsigned char buf[2];
1128 /* Mess: In case of -x, the stream is NOT at its start now,
1129 and ungetc() was used to push back the first newline,
1130 which makes the current stream position formally undefined,
1131 and a x-platform nightmare.
1132 Unfortunately, we have no direct way to know whether -x
1133 was specified. So we use a terrible hack: if the current
1134 stream position is not 0, we assume -x was specified, and
1135 give up. Bug 132850 on SourceForge spells out the
1136 hopelessness of trying anything else (fseek and ftell
1137 don't work predictably x-platform for text-mode files).
1138 */
1139 int ispyc = 0;
1140 if (ftell(fp) == 0) {
1141 if (fread(buf, 1, 2, fp) == 2 &&
1142 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
1143 ispyc = 1;
1144 rewind(fp);
1145 }
1146 return ispyc;
1147 }
1148 return 0;
1149 }
1150
1151 int
1152 PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
1153 PyCompilerFlags *flags)
1154 {
1155 PyObject *m, *d, *v;
1156 const char *ext;
1157 int set_file_name = 0, ret;
1158 size_t len;
1159
1160 m = PyImport_AddModule("__main__");
1161 if (m == NULL)
1162 return -1;
1163 d = PyModule_GetDict(m);
1164 if (PyDict_GetItemString(d, "__file__") == NULL) {
1165 PyObject *f;
1166 f = PyUnicode_DecodeFSDefault(filename);
1167 if (f == NULL)
1168 return -1;
1169 if (PyDict_SetItemString(d, "__file__", f) < 0) {
1170 Py_DECREF(f);
1171 return -1;
1172 }
1173 if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
1174 Py_DECREF(f);
1175 return -1;
1176 }
1177 set_file_name = 1;
1178 Py_DECREF(f);
1179 }
1180 len = strlen(filename);
1181 ext = filename + len - (len > 4 ? 4 : 0);
1182 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1183 /* Try to run a pyc file. First, re-open in binary */
1184 if (closeit)
1185 fclose(fp);
1186 if ((fp = fopen(filename, "rb")) == NULL) {
1187 fprintf(stderr, "python: Can't reopen .pyc file\n");
1188 ret = -1;
1189 goto done;
1190 }
1191 /* Turn on optimization if a .pyo file is given */
1192 if (strcmp(ext, ".pyo") == 0)
1193 Py_OptimizeFlag = 1;
1194 v = run_pyc_file(fp, filename, d, d, flags);
1195 } else {
1196 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
1197 closeit, flags);
1198 }
1199 flush_io();
1200 if (v == NULL) {
1201 PyErr_Print();
1202 ret = -1;
1203 goto done;
1204 }
1205 Py_DECREF(v);
1206 ret = 0;
1207 done:
1208 if (set_file_name && PyDict_DelItemString(d, "__file__"))
1209 PyErr_Clear();
1210 return ret;
1211 }
1212
1213 int
1214 PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
1215 {
1216 PyObject *m, *d, *v;
1217 m = PyImport_AddModule("__main__");
1218 if (m == NULL)
1219 return -1;
1220 d = PyModule_GetDict(m);
1221 v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
1222 if (v == NULL) {
1223 PyErr_Print();
1224 return -1;
1225 }
1226 Py_DECREF(v);
1227 return 0;
1228 }
1229
1230 static int
1231 parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
1232 int *lineno, int *offset, const char **text)
1233 {
1234 long hold;
1235 PyObject *v;
1236 _Py_IDENTIFIER(msg);
1237 _Py_IDENTIFIER(filename);
1238 _Py_IDENTIFIER(lineno);
1239 _Py_IDENTIFIER(offset);
1240 _Py_IDENTIFIER(text);
1241
1242 /* old style errors */
1243 if (PyTuple_Check(err))
1244 return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
1245 lineno, offset, text);
1246
1247 /* new style errors. `err' is an instance */
1248
1249 if (! (v = _PyObject_GetAttrId(err, &PyId_msg)))
1250 goto finally;
1251 *message = v;
1252
1253 if (!(v = _PyObject_GetAttrId(err, &PyId_filename)))
1254 goto finally;
1255 if (v == Py_None)
1256 *filename = NULL;
1257 else if (! (*filename = _PyUnicode_AsString(v)))
1258 goto finally;
1259
1260 Py_DECREF(v);
1261 if (!(v = _PyObject_GetAttrId(err, &PyId_lineno)))
1262 goto finally;
1263 hold = PyLong_AsLong(v);
1264 Py_DECREF(v);
1265 v = NULL;
1266 if (hold < 0 && PyErr_Occurred())
1267 goto finally;
1268 *lineno = (int)hold;
1269
1270 if (!(v = _PyObject_GetAttrId(err, &PyId_offset)))
1271 goto finally;
1272 if (v == Py_None) {
1273 *offset = -1;
1274 Py_DECREF(v);
1275 v = NULL;
1276 } else {
1277 hold = PyLong_AsLong(v);
1278 Py_DECREF(v);
1279 v = NULL;
1280 if (hold < 0 && PyErr_Occurred())
1281 goto finally;
1282 *offset = (int)hold;
1283 }
1284
1285 if (!(v = _PyObject_GetAttrId(err, &PyId_text)))
1286 goto finally;
1287 if (v == Py_None)
1288 *text = NULL;
1289 else if (!PyUnicode_Check(v) ||
1290 !(*text = _PyUnicode_AsString(v)))
1291 goto finally;
1292 Py_DECREF(v);
1293 return 1;
1294
1295 finally:
1296 Py_XDECREF(v);
1297 return 0;
1298 }
1299
1300 void
1301 PyErr_Print(void)
1302 {
1303 PyErr_PrintEx(1);
1304 }
1305
1306 static void
1307 print_error_text(PyObject *f, int offset, const char *text)
1308 {
1309 char *nl;
1310 if (offset >= 0) {
1311 if (offset > 0 && offset == strlen(text) && text[offset - 1] == '\n')
1312 offset--;
1313 for (;;) {
1314 nl = strchr(text, '\n');
1315 if (nl == NULL || nl-text >= offset)
1316 break;
1317 offset -= (int)(nl+1-text);
1318 text = nl+1;
1319 }
1320 while (*text == ' ' || *text == '\t') {
1321 text++;
1322 offset--;
1323 }
1324 }
1325 PyFile_WriteString(" ", f);
1326 PyFile_WriteString(text, f);
1327 if (*text == '\0' || text[strlen(text)-1] != '\n')
1328 PyFile_WriteString("\n", f);
1329 if (offset == -1)
1330 return;
1331 PyFile_WriteString(" ", f);
1332 while (--offset > 0)
1333 PyFile_WriteString(" ", f);
1334 PyFile_WriteString("^\n", f);
1335 }
1336
1337 static void
1338 handle_system_exit(void)
1339 {
1340 PyObject *exception, *value, *tb;
1341 int exitcode = 0;
1342
1343 if (Py_InspectFlag)
1344 /* Don't exit if -i flag was given. This flag is set to 0
1345 * when entering interactive mode for inspecting. */
1346 return;
1347
1348 PyErr_Fetch(&exception, &value, &tb);
1349 fflush(stdout);
1350 if (value == NULL || value == Py_None)
1351 goto done;
1352 if (PyExceptionInstance_Check(value)) {
1353 /* The error code should be in the `code' attribute. */
1354 _Py_IDENTIFIER(code);
1355 PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
1356 if (code) {
1357 Py_DECREF(value);
1358 value = code;
1359 if (value == Py_None)
1360 goto done;
1361 }
1362 /* If we failed to dig out the 'code' attribute,
1363 just let the else clause below print the error. */
1364 }
1365 if (PyLong_Check(value))
1366 exitcode = (int)PyLong_AsLong(value);
1367 else {
1368 PyObject *sys_stderr = PySys_GetObject("stderr");
1369 if (sys_stderr != NULL && sys_stderr != Py_None) {
1370 PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
1371 } else {
1372 PyObject_Print(value, stderr, Py_PRINT_RAW);
1373 fflush(stderr);
1374 }
1375 PySys_WriteStderr("\n");
1376 exitcode = 1;
1377 }
1378 done:
1379 /* Restore and clear the exception info, in order to properly decref
1380 * the exception, value, and traceback. If we just exit instead,
1381 * these leak, which confuses PYTHONDUMPREFS output, and may prevent
1382 * some finalizers from running.
1383 */
1384 PyErr_Restore(exception, value, tb);
1385 PyErr_Clear();
1386 Py_Exit(exitcode);
1387 /* NOTREACHED */
1388 }
1389
1390 void
1391 PyErr_PrintEx(int set_sys_last_vars)
1392 {
1393 PyObject *exception, *v, *tb, *hook;
1394
1395 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1396 handle_system_exit();
1397 }
1398 PyErr_Fetch(&exception, &v, &tb);
1399 if (exception == NULL)
1400 return;
1401 PyErr_NormalizeException(&exception, &v, &tb);
1402 if (tb == NULL) {
1403 tb = Py_None;
1404 Py_INCREF(tb);
1405 }
1406 PyException_SetTraceback(v, tb);
1407 if (exception == NULL)
1408 return;
1409 /* Now we know v != NULL too */
1410 if (set_sys_last_vars) {
1411 PySys_SetObject("last_type", exception);
1412 PySys_SetObject("last_value", v);
1413 PySys_SetObject("last_traceback", tb);
1414 }
1415 hook = PySys_GetObject("excepthook");
1416 if (hook) {
1417 PyObject *args = PyTuple_Pack(3, exception, v, tb);
1418 PyObject *result = PyEval_CallObject(hook, args);
1419 if (result == NULL) {
1420 PyObject *exception2, *v2, *tb2;
1421 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
1422 handle_system_exit();
1423 }
1424 PyErr_Fetch(&exception2, &v2, &tb2);
1425 PyErr_NormalizeException(&exception2, &v2, &tb2);
1426 /* It should not be possible for exception2 or v2
1427 to be NULL. However PyErr_Display() can't
1428 tolerate NULLs, so just be safe. */
1429 if (exception2 == NULL) {
1430 exception2 = Py_None;
1431 Py_INCREF(exception2);
1432 }
1433 if (v2 == NULL) {
1434 v2 = Py_None;
1435 Py_INCREF(v2);
1436 }
1437 fflush(stdout);
1438 PySys_WriteStderr("Error in sys.excepthook:\n");
1439 PyErr_Display(exception2, v2, tb2);
1440 PySys_WriteStderr("\nOriginal exception was:\n");
1441 PyErr_Display(exception, v, tb);
1442 Py_DECREF(exception2);
1443 Py_DECREF(v2);
1444 Py_XDECREF(tb2);
1445 }
1446 Py_XDECREF(result);
1447 Py_XDECREF(args);
1448 } else {
1449 PySys_WriteStderr("sys.excepthook is missing\n");
1450 PyErr_Display(exception, v, tb);
1451 }
1452 Py_XDECREF(exception);
1453 Py_XDECREF(v);
1454 Py_XDECREF(tb);
1455 }
1456
1457 static void
1458 print_exception(PyObject *f, PyObject *value)
1459 {
1460 int err = 0;
1461 PyObject *type, *tb;
1462 _Py_IDENTIFIER(print_file_and_line);
1463
1464 if (!PyExceptionInstance_Check(value)) {
1465 PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
1466 PyFile_WriteString(Py_TYPE(value)->tp_name, f);
1467 PyFile_WriteString(" found\n", f);
1468 return;
1469 }
1470
1471 Py_INCREF(value);
1472 fflush(stdout);
1473 type = (PyObject *) Py_TYPE(value);
1474 tb = PyException_GetTraceback(value);
1475 if (tb && tb != Py_None)
1476 err = PyTraceBack_Print(tb, f);
1477 if (err == 0 &&
1478 _PyObject_HasAttrId(value, &PyId_print_file_and_line))
1479 {
1480 PyObject *message;
1481 const char *filename, *text;
1482 int lineno, offset;
1483 if (!parse_syntax_error(value, &message, &filename,
1484 &lineno, &offset, &text))
1485 PyErr_Clear();
1486 else {
1487 char buf[10];
1488 PyFile_WriteString(" File \"", f);
1489 if (filename == NULL)
1490 PyFile_WriteString("<string>", f);
1491 else
1492 PyFile_WriteString(filename, f);
1493 PyFile_WriteString("\", line ", f);
1494 PyOS_snprintf(buf, sizeof(buf), "%d", lineno);
1495 PyFile_WriteString(buf, f);
1496 PyFile_WriteString("\n", f);
1497 if (text != NULL)
1498 print_error_text(f, offset, text);
1499 Py_DECREF(value);
1500 value = message;
1501 /* Can't be bothered to check all those
1502 PyFile_WriteString() calls */
1503 if (PyErr_Occurred())
1504 err = -1;
1505 }
1506 }
1507 if (err) {
1508 /* Don't do anything else */
1509 }
1510 else {
1511 PyObject* moduleName;
1512 char* className;
1513 _Py_IDENTIFIER(__module__);
1514 assert(PyExceptionClass_Check(type));
1515 className = PyExceptionClass_Name(type);
1516 if (className != NULL) {
1517 char *dot = strrchr(className, '.');
1518 if (dot != NULL)
1519 className = dot+1;
1520 }
1521
1522 moduleName = _PyObject_GetAttrId(type, &PyId___module__);
1523 if (moduleName == NULL || !PyUnicode_Check(moduleName))
1524 {
1525 Py_XDECREF(moduleName);
1526 err = PyFile_WriteString("<unknown>", f);
1527 }
1528 else {
1529 char* modstr = _PyUnicode_AsString(moduleName);
1530 if (modstr && strcmp(modstr, "builtins"))
1531 {
1532 err = PyFile_WriteString(modstr, f);
1533 err += PyFile_WriteString(".", f);
1534 }
1535 Py_DECREF(moduleName);
1536 }
1537 if (err == 0) {
1538 if (className == NULL)
1539 err = PyFile_WriteString("<unknown>", f);
1540 else
1541 err = PyFile_WriteString(className, f);
1542 }
1543 }
1544 if (err == 0 && (value != Py_None)) {
1545 PyObject *s = PyObject_Str(value);
1546 /* only print colon if the str() of the
1547 object is not the empty string
1548 */
1549 if (s == NULL)
1550 err = -1;
1551 else if (!PyUnicode_Check(s) ||
1552 PyUnicode_GetLength(s) != 0)
1553 err = PyFile_WriteString(": ", f);
1554 if (err == 0)
1555 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
1556 Py_XDECREF(s);
1557 }
1558 /* try to write a newline in any case */
1559 err += PyFile_WriteString("\n", f);
1560 Py_XDECREF(tb);
1561 Py_DECREF(value);
1562 /* If an error happened here, don't show it.
1563 XXX This is wrong, but too many callers rely on this behavior. */
1564 if (err != 0)
1565 PyErr_Clear();
1566 }
1567
1568 static const char *cause_message =
1569 "\nThe above exception was the direct cause "
1570 "of the following exception:\n\n";
1571
1572 static const char *context_message =
1573 "\nDuring handling of the above exception, "
1574 "another exception occurred:\n\n";
1575
1576 static void
1577 print_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
1578 {
1579 int err = 0, res;
1580 PyObject *cause, *context;
1581
1582 if (seen != NULL) {
1583 /* Exception chaining */
1584 if (PySet_Add(seen, value) == -1)
1585 PyErr_Clear();
1586 else if (PyExceptionInstance_Check(value)) {
1587 cause = PyException_GetCause(value);
1588 context = PyException_GetContext(value);
1589 if (cause) {
1590 res = PySet_Contains(seen, cause);
1591 if (res == -1)
1592 PyErr_Clear();
1593 if (res == 0) {
1594 print_exception_recursive(
1595 f, cause, seen);
1596 err |= PyFile_WriteString(
1597 cause_message, f);
1598 }
1599 }
1600 else if (context) {
1601 res = PySet_Contains(seen, context);
1602 if (res == -1)
1603 PyErr_Clear();
1604 if (res == 0) {
1605 print_exception_recursive(
1606 f, context, seen);
1607 err |= PyFile_WriteString(
1608 context_message, f);
1609 }
1610 }
1611 Py_XDECREF(context);
1612 Py_XDECREF(cause);
1613 }
1614 }
1615 print_exception(f, value);
1616 if (err != 0)
1617 PyErr_Clear();
1618 }
1619
1620 void
1621 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1622 {
1623 PyObject *seen;
1624 PyObject *f = PySys_GetObject("stderr");
1625 if (f == Py_None) {
1626 /* pass */
1627 }
1628 else if (f == NULL) {
1629 _PyObject_Dump(value);
1630 fprintf(stderr, "lost sys.stderr\n");
1631 }
1632 else {
1633 /* We choose to ignore seen being possibly NULL, and report
1634 at least the main exception (it could be a MemoryError).
1635 */
1636 seen = PySet_New(NULL);
1637 if (seen == NULL)
1638 PyErr_Clear();
1639 print_exception_recursive(f, value, seen);
1640 Py_XDECREF(seen);
1641 }
1642 }
1643
1644 PyObject *
1645 PyRun_StringFlags(const char *str, int start, PyObject *globals,
1646 PyObject *locals, PyCompilerFlags *flags)
1647 {
1648 PyObject *ret = NULL;
1649 mod_ty mod;
1650 PyArena *arena = PyArena_New();
1651 if (arena == NULL)
1652 return NULL;
1653
1654 mod = PyParser_ASTFromString(str, "<string>", start, flags, arena);
1655 if (mod != NULL)
1656 ret = run_mod(mod, "<string>", globals, locals, flags, arena);
1657 PyArena_Free(arena);
1658 return ret;
1659 }
1660
1661 PyObject *
1662 PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
1663 PyObject *locals, int closeit, PyCompilerFlags *flags)
1664 {
1665 PyObject *ret;
1666 mod_ty mod;
1667 PyArena *arena = PyArena_New();
1668 if (arena == NULL)
1669 return NULL;
1670
1671 mod = PyParser_ASTFromFile(fp, filename, NULL, start, 0, 0,
1672 flags, NULL, arena);
1673 if (closeit)
1674 fclose(fp);
1675 if (mod == NULL) {
1676 PyArena_Free(arena);
1677 return NULL;
1678 }
1679 ret = run_mod(mod, filename, globals, locals, flags, arena);
1680 PyArena_Free(arena);
1681 return ret;
1682 }
1683
1684 static void
1685 flush_io(void)
1686 {
1687 PyObject *f, *r;
1688 PyObject *type, *value, *traceback;
1689 _Py_IDENTIFIER(flush);
1690
1691 /* Save the current exception */
1692 PyErr_Fetch(&type, &value, &traceback);
1693
1694 f = PySys_GetObject("stderr");
1695 if (f != NULL) {
1696 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1697 if (r)
1698 Py_DECREF(r);
1699 else
1700 PyErr_Clear();
1701 }
1702 f = PySys_GetObject("stdout");
1703 if (f != NULL) {
1704 r = _PyObject_CallMethodId(f, &PyId_flush, "");
1705 if (r)
1706 Py_DECREF(r);
1707 else
1708 PyErr_Clear();
1709 }
1710
1711 PyErr_Restore(type, value, traceback);
1712 }
1713
1714 static PyObject *
1715 run_mod(mod_ty mod, const char *filename, PyObject *globals, PyObject *locals,
1716 PyCompilerFlags *flags, PyArena *arena)
1717 {
1718 PyCodeObject *co;
1719 PyObject *v;
1720 co = PyAST_Compile(mod, filename, flags, arena);
1721 if (co == NULL)
1722 return NULL;
1723 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1724 Py_DECREF(co);
1725 return v;
1726 }
1727
1728 static PyObject *
1729 run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
1730 PyObject *locals, PyCompilerFlags *flags)
1731 {
1732 PyCodeObject *co;
1733 PyObject *v;
1734 long magic;
1735 long PyImport_GetMagicNumber(void);
1736
1737 magic = PyMarshal_ReadLongFromFile(fp);
1738 if (magic != PyImport_GetMagicNumber()) {
1739 PyErr_SetString(PyExc_RuntimeError,
1740 "Bad magic number in .pyc file");
1741 return NULL;
1742 }
1743 (void) PyMarshal_ReadLongFromFile(fp);
1744 v = PyMarshal_ReadLastObjectFromFile(fp);
1745 fclose(fp);
1746 if (v == NULL || !PyCode_Check(v)) {
1747 Py_XDECREF(v);
1748 PyErr_SetString(PyExc_RuntimeError,
1749 "Bad code object in .pyc file");
1750 return NULL;
1751 }
1752 co = (PyCodeObject *)v;
1753 v = PyEval_EvalCode((PyObject*)co, globals, locals);
1754 if (v && flags)
1755 flags->cf_flags |= (co->co_flags & PyCF_MASK);
1756 Py_DECREF(co);
1757 return v;
1758 }
1759
1760 PyObject *
1761 Py_CompileStringExFlags(const char *str, const char *filename, int start,
1762 PyCompilerFlags *flags, int optimize)
1763 {
1764 PyCodeObject *co;
1765 mod_ty mod;
1766 PyArena *arena = PyArena_New();
1767 if (arena == NULL)
1768 return NULL;
1769
1770 mod = PyParser_ASTFromString(str, filename, start, flags, arena);
1771 if (mod == NULL) {
1772 PyArena_Free(arena);
1773 return NULL;
1774 }
1775 if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1776 PyObject *result = PyAST_mod2obj(mod);
1777 PyArena_Free(arena);
1778 return result;
1779 }
1780 co = PyAST_CompileEx(mod, filename, flags, optimize, arena);
1781 PyArena_Free(arena);
1782 return (PyObject *)co;
1783 }
1784
1785 /* For use in Py_LIMITED_API */
1786 #undef Py_CompileString
1787 PyObject *
1788 PyCompileString(const char *str, const char *filename, int start)
1789 {
1790 return Py_CompileStringFlags(str, filename, start, NULL);
1791 }
1792
1793 struct symtable *
1794 Py_SymtableString(const char *str, const char *filename, int start)
1795 {
1796 struct symtable *st;
1797 mod_ty mod;
1798 PyCompilerFlags flags;
1799 PyArena *arena = PyArena_New();
1800 if (arena == NULL)
1801 return NULL;
1802
1803 flags.cf_flags = 0;
1804 mod = PyParser_ASTFromString(str, filename, start, &flags, arena);
1805 if (mod == NULL) {
1806 PyArena_Free(arena);
1807 return NULL;
1808 }
1809 st = PySymtable_Build(mod, filename, 0);
1810 PyArena_Free(arena);
1811 return st;
1812 }
1813
1814 /* Preferred access to parser is through AST. */
1815 mod_ty
1816 PyParser_ASTFromString(const char *s, const char *filename, int start,
1817 PyCompilerFlags *flags, PyArena *arena)
1818 {
1819 mod_ty mod;
1820 PyCompilerFlags localflags;
1821 perrdetail err;
1822 int iflags = PARSER_FLAGS(flags);
1823
1824 node *n = PyParser_ParseStringFlagsFilenameEx(s, filename,
1825 &_PyParser_Grammar, start, &err,
1826 &iflags);
1827 if (flags == NULL) {
1828 localflags.cf_flags = 0;
1829 flags = &localflags;
1830 }
1831 if (n) {
1832 flags->cf_flags |= iflags & PyCF_MASK;
1833 mod = PyAST_FromNode(n, flags, filename, arena);
1834 PyNode_Free(n);
1835 }
1836 else {
1837 err_input(&err);
1838 mod = NULL;
1839 }
1840 err_free(&err);
1841 return mod;
1842 }
1843
1844 mod_ty
1845 PyParser_ASTFromFile(FILE *fp, const char *filename, const char* enc,
1846 int start, char *ps1,
1847 char *ps2, PyCompilerFlags *flags, int *errcode,
1848 PyArena *arena)
1849 {
1850 mod_ty mod;
1851 PyCompilerFlags localflags;
1852 perrdetail err;
1853 int iflags = PARSER_FLAGS(flags);
1854
1855 node *n = PyParser_ParseFileFlagsEx(fp, filename, enc,
1856 &_PyParser_Grammar,
1857 start, ps1, ps2, &err, &iflags);
1858 if (flags == NULL) {
1859 localflags.cf_flags = 0;
1860 flags = &localflags;
1861 }
1862 if (n) {
1863 flags->cf_flags |= iflags & PyCF_MASK;
1864 mod = PyAST_FromNode(n, flags, filename, arena);
1865 PyNode_Free(n);
1866 }
1867 else {
1868 err_input(&err);
1869 if (errcode)
1870 *errcode = err.error;
1871 mod = NULL;
1872 }
1873 err_free(&err);
1874 return mod;
1875 }
1876
1877 /* Simplified interface to parsefile -- return node or set exception */
1878
1879 node *
1880 PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1881 {
1882 perrdetail err;
1883 node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1884 &_PyParser_Grammar,
1885 start, NULL, NULL, &err, flags);
1886 if (n == NULL)
1887 err_input(&err);
1888 err_free(&err);
1889
1890 return n;
1891 }
1892
1893 /* Simplified interface to parsestring -- return node or set exception */
1894
1895 node *
1896 PyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1897 {
1898 perrdetail err;
1899 node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1900 start, &err, flags);
1901 if (n == NULL)
1902 err_input(&err);
1903 err_free(&err);
1904 return n;
1905 }
1906
1907 node *
1908 PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1909 int start, int flags)
1910 {
1911 perrdetail err;
1912 node *n = PyParser_ParseStringFlagsFilename(str, filename,
1913 &_PyParser_Grammar, start, &err, flags);
1914 if (n == NULL)
1915 err_input(&err);
1916 err_free(&err);
1917 return n;
1918 }
1919
1920 node *
1921 PyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1922 {
1923 return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1924 }
1925
1926 /* May want to move a more generalized form of this to parsetok.c or
1927 even parser modules. */
1928
1929 void
1930 PyParser_ClearError(perrdetail *err)
1931 {
1932 err_free(err);
1933 }
1934
1935 void
1936 PyParser_SetError(perrdetail *err)
1937 {
1938 err_input(err);
1939 }
1940
1941 static void
1942 err_free(perrdetail *err)
1943 {
1944 Py_CLEAR(err->filename);
1945 }
1946
1947 /* Set the error appropriate to the given input error code (see errcode.h) */
1948
1949 static void
1950 err_input(perrdetail *err)
1951 {
1952 PyObject *v, *w, *errtype, *errtext;
1953 PyObject *msg_obj = NULL;
1954 char *msg = NULL;
1955
1956 errtype = PyExc_SyntaxError;
1957 switch (err->error) {
1958 case E_ERROR:
1959 return;
1960 case E_SYNTAX:
1961 errtype = PyExc_IndentationError;
1962 if (err->expected == INDENT)
1963 msg = "expected an indented block";
1964 else if (err->token == INDENT)
1965 msg = "unexpected indent";
1966 else if (err->token == DEDENT)
1967 msg = "unexpected unindent";
1968 else {
1969 errtype = PyExc_SyntaxError;
1970 msg = "invalid syntax";
1971 }
1972 break;
1973 case E_TOKEN:
1974 msg = "invalid token";
1975 break;
1976 case E_EOFS:
1977 msg = "EOF while scanning triple-quoted string literal";
1978 break;
1979 case E_EOLS:
1980 msg = "EOL while scanning string literal";
1981 break;
1982 case E_INTR:
1983 if (!PyErr_Occurred())
1984 PyErr_SetNone(PyExc_KeyboardInterrupt);
1985 goto cleanup;
1986 case E_NOMEM:
1987 PyErr_NoMemory();
1988 goto cleanup;
1989 case E_EOF:
1990 msg = "unexpected EOF while parsing";
1991 break;
1992 case E_TABSPACE:
1993 errtype = PyExc_TabError;
1994 msg = "inconsistent use of tabs and spaces in indentation";
1995 break;
1996 case E_OVERFLOW:
1997 msg = "expression too long";
1998 break;
1999 case E_DEDENT:
2000 errtype = PyExc_IndentationError;
2001 msg = "unindent does not match any outer indentation level";
2002 break;
2003 case E_TOODEEP:
2004 errtype = PyExc_IndentationError;
2005 msg = "too many levels of indentation";
2006 break;
2007 case E_DECODE: {
2008 PyObject *type, *value, *tb;
2009 PyErr_Fetch(&type, &value, &tb);
2010 msg = "unknown decode error";
2011 if (value != NULL)
2012 msg_obj = PyObject_Str(value);
2013 Py_XDECREF(type);
2014 Py_XDECREF(value);
2015 Py_XDECREF(tb);
2016 break;
2017 }
2018 case E_LINECONT:
2019 msg = "unexpected character after line continuation character";
2020 break;
2021
2022 case E_IDENTIFIER:
2023 msg = "invalid character in identifier";
2024 break;
2025 default:
2026 fprintf(stderr, "error=%d\n", err->error);
2027 msg = "unknown parsing error";
2028 break;
2029 }
2030 /* err->text may not be UTF-8 in case of decoding errors.
2031 Explicitly convert to an object. */
2032 if (!err->text) {
2033 errtext = Py_None;
2034 Py_INCREF(Py_None);
2035 } else {
2036 errtext = PyUnicode_DecodeUTF8(err->text, strlen(err->text),
2037 "replace");
2038 }
2039 v = Py_BuildValue("(OiiN)", err->filename,
2040 err->lineno, err->offset, errtext);
2041 if (v != NULL) {
2042 if (msg_obj)
2043 w = Py_BuildValue("(OO)", msg_obj, v);
2044 else
2045 w = Py_BuildValue("(sO)", msg, v);
2046 } else
2047 w = NULL;
2048 Py_XDECREF(v);
2049 PyErr_SetObject(errtype, w);
2050 Py_XDECREF(w);
2051 cleanup:
2052 Py_XDECREF(msg_obj);
2053 if (err->text != NULL) {
2054 PyObject_FREE(err->text);
2055 err->text = NULL;
2056 }
2057 }
2058
2059 /* Print fatal error message and abort */
2060
2061 void
2062 Py_FatalError(const char *msg)
2063 {
2064 const int fd = fileno(stderr);
2065 PyThreadState *tstate;
2066
2067 fprintf(stderr, "Fatal Python error: %s\n", msg);
2068 fflush(stderr); /* it helps in Windows debug build */
2069 if (PyErr_Occurred()) {
2070 PyErr_PrintEx(0);
2071 }
2072 else {
2073 tstate = _Py_atomic_load_relaxed(&_PyThreadState_Current);
2074 if (tstate != NULL) {
2075 fputc('\n', stderr);
2076 fflush(stderr);
2077 _Py_DumpTracebackThreads(fd, tstate->interp, tstate);
2078 }
2079 _PyFaulthandler_Fini();
2080 }
2081
2082 abort();
2083 }
2084
2085 /* Clean up and exit */
2086
2087 #ifdef WITH_THREAD
2088 #include "pythread.h"
2089 #endif
2090
2091 static void (*pyexitfunc)(void) = NULL;
2092 /* For the atexit module. */
2093 void _Py_PyAtExit(void (*func)(void))
2094 {
2095 pyexitfunc = func;
2096 }
2097
2098 static void
2099 call_py_exitfuncs(void)
2100 {
2101 if (pyexitfunc == NULL)
2102 return;
2103
2104 (*pyexitfunc)();
2105 PyErr_Clear();
2106 }
2107
2108 /* Wait until threading._shutdown completes, provided
2109 the threading module was imported in the first place.
2110 The shutdown routine will wait until all non-daemon
2111 "threading" threads have completed. */
2112 static void
2113 wait_for_thread_shutdown(void)
2114 {
2115 #ifdef WITH_THREAD
2116 _Py_IDENTIFIER(_shutdown);
2117 PyObject *result;
2118 PyThreadState *tstate = PyThreadState_GET();
2119 PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
2120 "threading");
2121 if (threading == NULL) {
2122 /* threading not imported */
2123 PyErr_Clear();
2124 return;
2125 }
2126 result = _PyObject_CallMethodId(threading, &PyId__shutdown, "");
2127 if (result == NULL) {
2128 PyErr_WriteUnraisable(threading);
2129 }
2130 else {
2131 Py_DECREF(result);
2132 }
2133 Py_DECREF(threading);
2134 #endif
2135 }
2136
2137 #define NEXITFUNCS 32
2138 static void (*exitfuncs[NEXITFUNCS])(void);
2139 static int nexitfuncs = 0;
2140
2141 int Py_AtExit(void (*func)(void))
2142 {
2143 if (nexitfuncs >= NEXITFUNCS)
2144 return -1;
2145 exitfuncs[nexitfuncs++] = func;
2146 return 0;
2147 }
2148
2149 static void
2150 call_ll_exitfuncs(void)
2151 {
2152 while (nexitfuncs > 0)
2153 (*exitfuncs[--nexitfuncs])();
2154
2155 fflush(stdout);
2156 fflush(stderr);
2157 }
2158
2159 void
2160 Py_Exit(int sts)
2161 {
2162 Py_Finalize();
2163
2164 exit(sts);
2165 }
2166
2167 static void
2168 initsigs(void)
2169 {
2170 #ifdef SIGPIPE
2171 PyOS_setsig(SIGPIPE, SIG_IGN);
2172 #endif
2173 #ifdef SIGXFZ
2174 PyOS_setsig(SIGXFZ, SIG_IGN);
2175 #endif
2176 #ifdef SIGXFSZ
2177 PyOS_setsig(SIGXFSZ, SIG_IGN);
2178 #endif
2179 PyOS_InitInterrupts(); /* May imply initsignal() */
2180 }
2181
2182
2183 /* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
2184 *
2185 * All of the code in this function must only use async-signal-safe functions,
2186 * listed at `man 7 signal` or
2187 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2188 */
2189 void
2190 _Py_RestoreSignals(void)
2191 {
2192 #ifdef SIGPIPE
2193 PyOS_setsig(SIGPIPE, SIG_DFL);
2194 #endif
2195 #ifdef SIGXFZ
2196 PyOS_setsig(SIGXFZ, SIG_DFL);
2197 #endif
2198 #ifdef SIGXFSZ
2199 PyOS_setsig(SIGXFSZ, SIG_DFL);
2200 #endif
2201 }
2202
2203
2204 /*
2205 * The file descriptor fd is considered ``interactive'' if either
2206 * a) isatty(fd) is TRUE, or
2207 * b) the -i flag was given, and the filename associated with
2208 * the descriptor is NULL or "<stdin>" or "???".
2209 */
2210 int
2211 Py_FdIsInteractive(FILE *fp, const char *filename)
2212 {
2213 if (isatty((int)fileno(fp)))
2214 return 1;
2215 if (!Py_InteractiveFlag)
2216 return 0;
2217 return (filename == NULL) ||
2218 (strcmp(filename, "<stdin>") == 0) ||
2219 (strcmp(filename, "???") == 0);
2220 }
2221
2222 /* Wrappers around sigaction() or signal(). */
2223
2224 PyOS_sighandler_t
2225 PyOS_getsig(int sig)
2226 {
2227 #ifdef HAVE_SIGACTION
2228 struct sigaction context;
2229 if (sigaction(sig, NULL, &context) == -1)
2230 return SIG_ERR;
2231 return context.sa_handler;
2232 #else
2233 PyOS_sighandler_t handler;
2234 handler = signal(sig, SIG_IGN);
2235 if (handler != SIG_ERR)
2236 signal(sig, handler);
2237 return handler;
2238 #endif
2239 }
2240
2241 /*
2242 * All of the code in this function must only use async-signal-safe functions,
2243 * listed at `man 7 signal` or
2244 * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html.
2245 */
2246 PyOS_sighandler_t
2247 PyOS_setsig(int sig, PyOS_sighandler_t handler)
2248 {
2249 #ifdef HAVE_SIGACTION
2250 /* Some code in Modules/signalmodule.c depends on sigaction() being
2251 * used here if HAVE_SIGACTION is defined. Fix that if this code
2252 * changes to invalidate that assumption.
2253 */
2254 struct sigaction context, ocontext;
2255 context.sa_handler = handler;
2256 sigemptyset(&context.sa_mask);
2257 context.sa_flags = 0;
2258 if (sigaction(sig, &context, &ocontext) == -1)
2259 return SIG_ERR;
2260 return ocontext.sa_handler;
2261 #else
2262 PyOS_sighandler_t oldhandler;
2263 oldhandler = signal(sig, handler);
2264 #ifdef HAVE_SIGINTERRUPT
2265 siginterrupt(sig, 1);
2266 #endif
2267 return oldhandler;
2268 #endif
2269 }
2270