diff Python/pythonrun.c @ 43841:ccceaec11442

Fix for #1415 pythonw.exe fails because std streams a missing After a long discussion about the problem with Windows GUI apps Guido decided that sys.stdin, stdout and stderr should be None when the C runtime library returns invalid file descriptors for the standard streams. So far the only known cases are Windows GUI apps and scripts started with pythonw on Windows. The OS restrictions are tight enough to catch the problem on other OSes.
author Christian Heimes <christian@cheimes.de>
date Tue, 13 Nov 2007 02:19:40 +0000
parents 0d462d789b18
children 241281123fd8
line wrap: on
line diff
--- a/Python/pythonrun.c	Tue Nov 13 01:05:30 2007 +0000
+++ b/Python/pythonrun.c	Tue Nov 13 02:19:40 2007 +0000
@@ -717,7 +717,7 @@
 	PyObject *bimod = NULL;
 	PyObject *m;
 	PyObject *std = NULL;
-	int status = 0;
+	int status = 0, fd;
 
 	/* Hack to avoid a nasty recursion issue when Python is invoked
 	   in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
@@ -748,35 +748,72 @@
 	}
 
 	/* Set sys.stdin */
-	if (!(std = PyFile_FromFd(fileno(stdin), "<stdin>", "r", -1,
-				  NULL, "\n", 0))) {
+	fd = fileno(stdin);
+	/* Under some conditions stdin, stdout and stderr may not be connected
+	 * 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
+		std = Py_None;
+		Py_INCREF(std);
+#else
 		goto error;
+#endif
 	}
+	else {
+		if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, 
+					  "\n", 0))) {
+			goto error;
+		}
+	} /* if (fd < 0) */
 	PySys_SetObject("__stdin__", std);
 	PySys_SetObject("stdin", std);
 	Py_DECREF(std);
 
 	/* Set sys.stdout */
-	if (!(std = PyFile_FromFd(fileno(stdout), "<stdout>", "w", -1,
-				  NULL, "\n", 0))) {
-            goto error;
-        }
+	fd = fileno(stdout);
+	if (fd < 0) {
+#ifdef MS_WINDOWS
+		std = Py_None;
+		Py_INCREF(std);
+#else
+		goto error;
+#endif
+	}
+	else {
+		if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, 
+					  "\n", 0))) {
+			goto error;
+		}
+	} /* if (fd < 0) */
 	PySys_SetObject("__stdout__", std);
 	PySys_SetObject("stdout", std);
 	Py_DECREF(std);
 
 #if 1 /* Disable this if you have trouble debugging bootstrap stuff */
 	/* Set sys.stderr, replaces the preliminary stderr */
-	if (!(std = PyFile_FromFd(fileno(stderr), "<stderr>", "w", -1,
-				  NULL, "\n", 0))) {
-            goto error;
-        }
+	fd = fileno(stderr);
+	if (fd < 0) {
+#ifdef MS_WINDOWS
+		std = Py_None;
+		Py_INCREF(std);
+#else
+		goto error;
+#endif
+	}
+	else {
+		if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, 
+					  "\n", 0))) {
+			goto error;
+		}
+	} /* if (fd < 0) */
         PySys_SetObject("__stderr__", std);
 	PySys_SetObject("stderr", std);
 	Py_DECREF(std);
 #endif
 
-        if (0) {
+	if (0) {
   error:
                 status = -1;
         }