[Python-3000-checkins] r58959 - in python/branches/py3k: Doc/library/sys.rst Misc/NEWS Objects/fileobject.c Python/pythonrun.c

christian.heimes python-3000-checkins at python.org
Tue Nov 13 03:19:40 CET 2007


Author: christian.heimes
Date: Tue Nov 13 03:19:40 2007
New Revision: 58959

Modified:
   python/branches/py3k/Doc/library/sys.rst
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/fileobject.c
   python/branches/py3k/Python/pythonrun.c
Log:
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.

Modified: python/branches/py3k/Doc/library/sys.rst
==============================================================================
--- python/branches/py3k/Doc/library/sys.rst	(original)
+++ python/branches/py3k/Doc/library/sys.rst	Tue Nov 13 03:19:40 2007
@@ -513,6 +513,13 @@
    could be useful to restore the actual files to known working file objects in
    case they have been overwritten with a broken object.
 
+  .. note::
+
+    Under some conditions ``stdin``, ``stdout`` and ``stderr`` as well as the
+    original values ``__stdin__``, ``__stdout__`` and ``__stderr__`` can be
+    None. It is usually the case for Windows GUI apps that aren't connected to
+    a console and Python apps started with :program:`pythonw`.
+
 
 .. data:: tracebacklimit
 
@@ -571,3 +578,4 @@
    Module :mod:`site`
       This describes how to use .pth files to extend ``sys.path``.
 
+

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Tue Nov 13 03:19:40 2007
@@ -34,6 +34,11 @@
 - Added a new option -b to issues warnings (-bb for errors) about certain
   operations between bytes/buffer and str like str(b'') and comparsion.
 
+- The standards streams sys.stdin, stdout and stderr may be None when the
+  when the C runtime library returns an invalid file descriptor for the
+  streams (fileno(stdin) < 0). For now this happens only for Windows GUI
+  apps and scripts started with `pythonw.exe`.
+
 Extension Modules
 -----------------
 

Modified: python/branches/py3k/Objects/fileobject.c
==============================================================================
--- python/branches/py3k/Objects/fileobject.c	(original)
+++ python/branches/py3k/Objects/fileobject.c	Tue Nov 13 03:19:40 2007
@@ -363,7 +363,7 @@
 {
 	PyStdPrinter_Object *self;
 
-	if ((fd != fileno(stdout) && fd != fileno(stderr)) || fd < 0) {
+	if (fd != fileno(stdout) && fd != fileno(stderr)) {
 		/* not enough infrastructure for PyErr_BadInternalCall() */
 		return NULL;
 	}

Modified: python/branches/py3k/Python/pythonrun.c
==============================================================================
--- python/branches/py3k/Python/pythonrun.c	(original)
+++ python/branches/py3k/Python/pythonrun.c	Tue Nov 13 03:19:40 2007
@@ -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;
         }


More information about the Python-3000-checkins mailing list