STINNER Victor <victor.stin...@gmail.com> added the comment:

New try (I fixed my email address and the patch).

----------
Added file: http://bugs.python.org/file24504/get_terminal_size_pipe-2.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13609>
_______________________________________
diff -r 5d0f7b275fe9 Lib/shutil.py
--- a/Lib/shutil.py     Sun Feb 12 21:06:57 2012 +0200
+++ b/Lib/shutil.py     Mon Feb 13 00:25:38 2012 +0100
@@ -912,7 +912,7 @@ def get_terminal_size(fallback=(80, 24))
     # only query if necessary
     if columns <= 0 or lines <= 0:
         try:
-            size = os.get_terminal_size(sys.__stdout__.fileno())
+            size = os.get_terminal_size()
         except (NameError, OSError):
             size = os.terminal_size(fallback)
         if columns <= 0:
diff -r 5d0f7b275fe9 Modules/posixmodule.c
--- a/Modules/posixmodule.c     Sun Feb 12 21:06:57 2012 +0200
+++ b/Modules/posixmodule.c     Mon Feb 13 00:25:38 2012 +0100
@@ -10532,7 +10532,7 @@ get_terminal_size(PyObject *self, PyObje
     int columns, lines;
     PyObject *termsize;
 
-    int fd = fileno(stdout);
+    int fd = -1;
     /* Under some conditions stdout may not be connected and
      * fileno(stdout) may point to an invalid file descriptor. For example
      * GUI apps don't have valid standard streams by default.
@@ -10547,8 +10547,23 @@ get_terminal_size(PyObject *self, PyObje
 #ifdef TERMSIZE_USE_IOCTL
     {
         struct winsize w;
-        if (ioctl(fd, TIOCGWINSZ, &w))
-            return PyErr_SetFromErrno(PyExc_OSError);
+
+        if (fd == -1) {
+            fd = fileno(stdout);
+            if (ioctl(fd, TIOCGWINSZ, &w)) {
+                if (errno != EINVAL)
+                    return PyErr_SetFromErrno(PyExc_OSError);
+                /* ioctl(TIOCGWINSZ) may fail on stdout if stdout is a pipe,
+                   retry on stin */
+                fd = fileno(stdin);
+                if (ioctl(fd, TIOCGWINSZ, &w))
+                    return PyErr_SetFromErrno(PyExc_OSError);
+            }
+        }
+        else {
+            if (ioctl(fd, TIOCGWINSZ, &w))
+                return PyErr_SetFromErrno(PyExc_OSError);
+        }
         columns = w.ws_col;
         lines = w.ws_row;
     }
@@ -10559,6 +10574,10 @@ get_terminal_size(PyObject *self, PyObje
         DWORD nhandle;
         HANDLE handle;
         CONSOLE_SCREEN_BUFFER_INFO csbi;
+
+        if (fd == -1)
+            fd = 1;
+
         switch (fd) {
         case 0: nhandle = STD_INPUT_HANDLE;
             break;
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to