Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 73776:c86fb10eaf68
Issue #7111: Python can now be run without a stdin, stdout or stderr stream.
It was already the case with Python 2. However, the corresponding
sys module entries are now set to None (instead of an unusable file object).
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Mon, 28 Nov 2011 19:09:45 +0100 |
| parents | 37300a1df7d7 f15943505db0 |
| children | 021e5bb297d1 |
comparison
equal
deleted
inserted
replaced
| 73774:e3647275f468 | 73776:c86fb10eaf68 |
|---|---|
| 910 Py_XDECREF(text); | 910 Py_XDECREF(text); |
| 911 Py_XDECREF(raw); | 911 Py_XDECREF(raw); |
| 912 return NULL; | 912 return NULL; |
| 913 } | 913 } |
| 914 | 914 |
| 915 static int | |
| 916 is_valid_fd(int fd) | |
| 917 { | |
| 918 int dummy_fd; | |
| 919 if (fd < 0 || !_PyVerify_fd(fd)) | |
| 920 return 0; | |
| 921 dummy_fd = dup(fd); | |
| 922 if (dummy_fd < 0) | |
| 923 return 0; | |
| 924 close(dummy_fd); | |
| 925 return 1; | |
| 926 } | |
| 927 | |
| 915 /* Initialize sys.stdin, stdout, stderr and builtins.open */ | 928 /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
| 916 static int | 929 static int |
| 917 initstdio(void) | 930 initstdio(void) |
| 918 { | 931 { |
| 919 PyObject *iomod = NULL, *wrapper; | 932 PyObject *iomod = NULL, *wrapper; |
| 969 fd = fileno(stdin); | 982 fd = fileno(stdin); |
| 970 /* Under some conditions stdin, stdout and stderr may not be connected | 983 /* Under some conditions stdin, stdout and stderr may not be connected |
| 971 * and fileno() may point to an invalid file descriptor. For example | 984 * and fileno() may point to an invalid file descriptor. For example |
| 972 * GUI apps don't have valid standard streams by default. | 985 * GUI apps don't have valid standard streams by default. |
| 973 */ | 986 */ |
| 974 if (fd < 0) { | 987 if (!is_valid_fd(fd)) { |
| 975 #ifdef MS_WINDOWS | |
| 976 std = Py_None; | 988 std = Py_None; |
| 977 Py_INCREF(std); | 989 Py_INCREF(std); |
| 978 #else | |
| 979 goto error; | |
| 980 #endif | |
| 981 } | 990 } |
| 982 else { | 991 else { |
| 983 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); | 992 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
| 984 if (std == NULL) | 993 if (std == NULL) |
| 985 goto error; | 994 goto error; |
| 988 PySys_SetObject("stdin", std); | 997 PySys_SetObject("stdin", std); |
| 989 Py_DECREF(std); | 998 Py_DECREF(std); |
| 990 | 999 |
| 991 /* Set sys.stdout */ | 1000 /* Set sys.stdout */ |
| 992 fd = fileno(stdout); | 1001 fd = fileno(stdout); |
| 993 if (fd < 0) { | 1002 if (!is_valid_fd(fd)) { |
| 994 #ifdef MS_WINDOWS | |
| 995 std = Py_None; | 1003 std = Py_None; |
| 996 Py_INCREF(std); | 1004 Py_INCREF(std); |
| 997 #else | |
| 998 goto error; | |
| 999 #endif | |
| 1000 } | 1005 } |
| 1001 else { | 1006 else { |
| 1002 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); | 1007 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
| 1003 if (std == NULL) | 1008 if (std == NULL) |
| 1004 goto error; | 1009 goto error; |
| 1008 Py_DECREF(std); | 1013 Py_DECREF(std); |
| 1009 | 1014 |
| 1010 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ | 1015 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 1011 /* Set sys.stderr, replaces the preliminary stderr */ | 1016 /* Set sys.stderr, replaces the preliminary stderr */ |
| 1012 fd = fileno(stderr); | 1017 fd = fileno(stderr); |
| 1013 if (fd < 0) { | 1018 if (!is_valid_fd(fd)) { |
| 1014 #ifdef MS_WINDOWS | |
| 1015 std = Py_None; | 1019 std = Py_None; |
| 1016 Py_INCREF(std); | 1020 Py_INCREF(std); |
| 1017 #else | |
| 1018 goto error; | |
| 1019 #endif | |
| 1020 } | 1021 } |
| 1021 else { | 1022 else { |
| 1022 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); | 1023 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
| 1023 if (std == NULL) | 1024 if (std == NULL) |
| 1024 goto error; | 1025 goto error; |
