comparison Python/pythonrun.c @ 43605:a2bb76d11d66

Patch 1267 by Christian Heimes. Move the initialization of sys.std{in,out,err} and __builtin__.open to C code. This solves the problem that "python -S" wouldn't work.
author Guido van Rossum <guido@python.org>
date Fri, 19 Oct 2007 23:16:50 +0000
parents 19319e70fc3e
children 0067a6ab4854
comparison
equal deleted inserted replaced
43604:b18f6092f6e0 43605:a2bb76d11d66
49 extern grammar _PyParser_Grammar; /* From graminit.c */ 49 extern grammar _PyParser_Grammar; /* From graminit.c */
50 50
51 /* Forward */ 51 /* Forward */
52 static void initmain(void); 52 static void initmain(void);
53 static void initsite(void); 53 static void initsite(void);
54 static int initstdio(void);
54 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *, 55 static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
55 PyCompilerFlags *, PyArena *); 56 PyCompilerFlags *, PyArena *);
56 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *, 57 static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
57 PyCompilerFlags *); 58 PyCompilerFlags *);
58 static void err_input(perrdetail *); 59 static void err_input(perrdetail *);
239 240
240 if (install_sigs) 241 if (install_sigs)
241 initsigs(); /* Signal handling stuff, including initintr() */ 242 initsigs(); /* Signal handling stuff, including initintr() */
242 243
243 initmain(); /* Module __main__ */ 244 initmain(); /* Module __main__ */
245 if (initstdio() < 0)
246 Py_FatalError(
247 "Py_Initialize: can't initialize sys standard streams");
244 if (!Py_NoSiteFlag) 248 if (!Py_NoSiteFlag)
245 initsite(); /* Module site */ 249 initsite(); /* Module site */
246 250
247 /* auto-thread-state API, if available */ 251 /* auto-thread-state API, if available */
248 #ifdef WITH_THREAD 252 #ifdef WITH_THREAD
672 } 676 }
673 } 677 }
674 else { 678 else {
675 Py_DECREF(m); 679 Py_DECREF(m);
676 } 680 }
681 }
682
683 /* Initialize sys.stdin, stdout, stderr and __builtin__.open */
684 static int
685 initstdio(void)
686 {
687 PyObject *iomod = NULL, *wrapper;
688 PyObject *bimod = NULL;
689 PyObject *m;
690 PyObject *std = NULL;
691 int status = 0;
692
693 /* Hack to avoid a nasty recursion issue when Python is invoked
694 in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
695 if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
696 goto error;
697 }
698 Py_DECREF(m);
699
700 if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
701 goto error;
702 }
703 Py_DECREF(m);
704
705 if (!(bimod = PyImport_ImportModule("__builtin__"))) {
706 goto error;
707 }
708
709 if (!(iomod = PyImport_ImportModule("io"))) {
710 goto error;
711 }
712 if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
713 goto error;
714 }
715
716 /* Set __builtin__.open */
717 if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
718 goto error;
719 }
720
721 /* Set sys.stdin */
722 if (!(std = PyFile_FromFileEx(stdin, "<stdin>", "r", fclose, -1,
723 NULL, "\n"))) {
724 goto error;
725 }
726 PySys_SetObject("__stdin__", std);
727 PySys_SetObject("stdin", std);
728 Py_DECREF(std);
729
730 /* Set sys.stdout */
731 if (!(std = PyFile_FromFileEx(stdout, "<stdout>", "w", fclose, -1,
732 NULL, "\n"))) {
733 goto error;
734 }
735 PySys_SetObject("__stdout__", std);
736 PySys_SetObject("stdout", std);
737 Py_DECREF(std);
738
739 /* Set sys.stderr */
740 if (!(std = PyFile_FromFileEx(stderr, "<stderr>", "w", fclose, -1,
741 NULL, "\n"))) {
742 goto error;
743 }
744 PySys_SetObject("__stderr__", std);
745 PySys_SetObject("stderr", std);
746 Py_DECREF(std);
747
748 if (0) {
749 error:
750 status = -1;
751 }
752
753 Py_XDECREF(bimod);
754 Py_XDECREF(iomod);
755 return status;
677 } 756 }
678 757
679 /* Parse input from a file and execute it */ 758 /* Parse input from a file and execute it */
680 759
681 int 760 int
1144 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb) 1223 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
1145 { 1224 {
1146 int err = 0; 1225 int err = 0;
1147 PyObject *f = PySys_GetObject("stderr"); 1226 PyObject *f = PySys_GetObject("stderr");
1148 Py_INCREF(value); 1227 Py_INCREF(value);
1149 if (f == NULL) 1228 if (f == NULL) {
1150 _PyObject_Dump(value); 1229 _PyObject_Dump(value);
1151 if (f == NULL)
1152 fprintf(stderr, "lost sys.stderr\n"); 1230 fprintf(stderr, "lost sys.stderr\n");
1231 }
1153 else { 1232 else {
1154 fflush(stdout); 1233 fflush(stdout);
1155 if (tb && tb != Py_None) 1234 if (tb && tb != Py_None)
1156 err = PyTraceBack_Print(tb, f); 1235 err = PyTraceBack_Print(tb, f);
1157 if (err == 0 && 1236 if (err == 0 &&
1587 1666
1588 void 1667 void
1589 Py_FatalError(const char *msg) 1668 Py_FatalError(const char *msg)
1590 { 1669 {
1591 fprintf(stderr, "Fatal Python error: %s\n", msg); 1670 fprintf(stderr, "Fatal Python error: %s\n", msg);
1671 if (PyErr_Occurred()) {
1672 PyErr_Print();
1673 }
1592 #ifdef MS_WINDOWS 1674 #ifdef MS_WINDOWS
1593 OutputDebugString("Fatal Python error: "); 1675 OutputDebugString("Fatal Python error: ");
1594 OutputDebugString(msg); 1676 OutputDebugString(msg);
1595 OutputDebugString("\n"); 1677 OutputDebugString("\n");
1596 #ifdef _DEBUG 1678 #ifdef _DEBUG