summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2011-11-28 18:09:45 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2011-11-28 18:09:45 (GMT)
commit39a73a4cfaac3cdd9592840ee3f6007593609789 (patch)
treee1595661b4da810b8874a3e578f32abcc398611b /Python
parent31b798d3c8592c84d36c7c9134a0bcbcb39dd92e (diff)
parent11942a58a190a235f69e8fb193d08a4fca2bcf3c (diff)
downloadcpython-39a73a4cfaac3cdd9592840ee3f6007593609789.zip
cpython-39a73a4cfaac3cdd9592840ee3f6007593609789.tar.gz
cpython-39a73a4cfaac3cdd9592840ee3f6007593609789.tar.bz2
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).
Diffstat (limited to 'Python')
-rw-r--r--Python/pythonrun.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 4956943..abc9ffc 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -912,6 +912,19 @@ error:
return NULL;
}
+static int
+is_valid_fd(int fd)
+{
+ int dummy_fd;
+ if (fd < 0 || !_PyVerify_fd(fd))
+ return 0;
+ dummy_fd = dup(fd);
+ if (dummy_fd < 0)
+ return 0;
+ close(dummy_fd);
+ return 1;
+}
+
/* Initialize sys.stdin, stdout, stderr and builtins.open */
static int
initstdio(void)
@@ -971,13 +984,9 @@ initstdio(void)
* and fileno() may point to an invalid file descriptor. For example
* GUI apps don't have valid standard streams by default.
*/
- if (fd < 0) {
-#ifdef MS_WINDOWS
+ if (!is_valid_fd(fd)) {
std = Py_None;
Py_INCREF(std);
-#else
- goto error;
-#endif
}
else {
std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
@@ -990,13 +999,9 @@ initstdio(void)
/* Set sys.stdout */
fd = fileno(stdout);
- if (fd < 0) {
-#ifdef MS_WINDOWS
+ if (!is_valid_fd(fd)) {
std = Py_None;
Py_INCREF(std);
-#else
- goto error;
-#endif
}
else {
std = create_stdio(iomod, fd, 1, "<stdout>", encoding, errors);
@@ -1010,13 +1015,9 @@ initstdio(void)
#if 1 /* Disable this if you have trouble debugging bootstrap stuff */
/* Set sys.stderr, replaces the preliminary stderr */
fd = fileno(stderr);
- if (fd < 0) {
-#ifdef MS_WINDOWS
+ if (!is_valid_fd(fd)) {
std = Py_None;
Py_INCREF(std);
-#else
- goto error;
-#endif
}
else {
std = create_stdio(iomod, fd, 1, "<stderr>", encoding, "backslashreplace");