Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 73775:f15943505db0 3.2
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:08:36 +0100 |
| parents | 150e096095e5 |
| children | 93e86b9318f7 c86fb10eaf68 |
comparison
equal
deleted
inserted
replaced
| 73773:661fb211f220 | 73775:f15943505db0 |
|---|---|
| 890 Py_XDECREF(text); | 890 Py_XDECREF(text); |
| 891 Py_XDECREF(raw); | 891 Py_XDECREF(raw); |
| 892 return NULL; | 892 return NULL; |
| 893 } | 893 } |
| 894 | 894 |
| 895 static int | |
| 896 is_valid_fd(int fd) | |
| 897 { | |
| 898 int dummy_fd; | |
| 899 if (fd < 0 || !_PyVerify_fd(fd)) | |
| 900 return 0; | |
| 901 dummy_fd = dup(fd); | |
| 902 if (dummy_fd < 0) | |
| 903 return 0; | |
| 904 close(dummy_fd); | |
| 905 return 1; | |
| 906 } | |
| 907 | |
| 895 /* Initialize sys.stdin, stdout, stderr and builtins.open */ | 908 /* Initialize sys.stdin, stdout, stderr and builtins.open */ |
| 896 static int | 909 static int |
| 897 initstdio(void) | 910 initstdio(void) |
| 898 { | 911 { |
| 899 PyObject *iomod = NULL, *wrapper; | 912 PyObject *iomod = NULL, *wrapper; |
| 949 fd = fileno(stdin); | 962 fd = fileno(stdin); |
| 950 /* Under some conditions stdin, stdout and stderr may not be connected | 963 /* Under some conditions stdin, stdout and stderr may not be connected |
| 951 * and fileno() may point to an invalid file descriptor. For example | 964 * and fileno() may point to an invalid file descriptor. For example |
| 952 * GUI apps don't have valid standard streams by default. | 965 * GUI apps don't have valid standard streams by default. |
| 953 */ | 966 */ |
| 954 if (fd < 0) { | 967 if (!is_valid_fd(fd)) { |
| 955 #ifdef MS_WINDOWS | |
| 956 std = Py_None; | 968 std = Py_None; |
| 957 Py_INCREF(std); | 969 Py_INCREF(std); |
| 958 #else | |
| 959 goto error; | |
| 960 #endif | |
| 961 } | 970 } |
| 962 else { | 971 else { |
| 963 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); | 972 std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors); |
| 964 if (std == NULL) | 973 if (std == NULL) |
| 965 goto error; | 974 goto error; |
| 968 PySys_SetObject("stdin", std); | 977 PySys_SetObject("stdin", std); |
| 969 Py_DECREF(std); | 978 Py_DECREF(std); |
| 970 | 979 |
| 971 /* Set sys.stdout */ | 980 /* Set sys.stdout */ |
| 972 fd = fileno(stdout); | 981 fd = fileno(stdout); |
| 973 if (fd < 0) { | 982 if (!is_valid_fd(fd)) { |
| 974 #ifdef MS_WINDOWS | |
| 975 std = Py_None; | 983 std = Py_None; |
| 976 Py_INCREF(std); | 984 Py_INCREF(std); |
| 977 #else | |
| 978 goto error; | |
| 979 #endif | |
| 980 } | 985 } |
| 981 else { | 986 else { |
| 982 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); | 987 std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors); |
| 983 if (std == NULL) | 988 if (std == NULL) |
| 984 goto error; | 989 goto error; |
| 988 Py_DECREF(std); | 993 Py_DECREF(std); |
| 989 | 994 |
| 990 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ | 995 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */ |
| 991 /* Set sys.stderr, replaces the preliminary stderr */ | 996 /* Set sys.stderr, replaces the preliminary stderr */ |
| 992 fd = fileno(stderr); | 997 fd = fileno(stderr); |
| 993 if (fd < 0) { | 998 if (!is_valid_fd(fd)) { |
| 994 #ifdef MS_WINDOWS | |
| 995 std = Py_None; | 999 std = Py_None; |
| 996 Py_INCREF(std); | 1000 Py_INCREF(std); |
| 997 #else | |
| 998 goto error; | |
| 999 #endif | |
| 1000 } | 1001 } |
| 1001 else { | 1002 else { |
| 1002 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); | 1003 std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace"); |
| 1003 if (std == NULL) | 1004 if (std == NULL) |
| 1004 goto error; | 1005 goto error; |
