[Python-checkins] cpython (merge 3.5 -> default): Merge: #22709: Use stdin as-is if it does not have a buffer attribute.

r.david.murray python-checkins at python.org
Sat Jan 2 15:45:06 EST 2016


https://hg.python.org/cpython/rev/688d32cdbc0c
changeset:   99759:688d32cdbc0c
parent:      99757:3a6b1186745f
parent:      99758:ded1336bff49
user:        R David Murray <rdmurray at bitdance.com>
date:        Sat Jan 02 15:43:44 2016 -0500
summary:
  Merge: #22709: Use stdin as-is if it does not have a buffer attribute.

files:
  Lib/fileinput.py           |   2 +-
  Lib/test/test_fileinput.py |  11 +++++++++++
  Misc/NEWS                  |   3 +++
  3 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/fileinput.py b/Lib/fileinput.py
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -328,7 +328,7 @@
             if self._filename == '-':
                 self._filename = '<stdin>'
                 if 'b' in self._mode:
-                    self._file = sys.stdin.buffer
+                    self._file = getattr(sys.stdin, 'buffer', sys.stdin)
                 else:
                     self._file = sys.stdin
                 self._isstdin = True
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -240,6 +240,17 @@
             lines = list(fi)
             self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
 
+    def test_detached_stdin_binary_mode(self):
+        orig_stdin = sys.stdin
+        try:
+            sys.stdin = BytesIO(b'spam, bacon, sausage, and spam')
+            self.assertFalse(hasattr(sys.stdin, 'buffer'))
+            fi = FileInput(files=['-'], mode='rb')
+            lines = list(fi)
+            self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
+        finally:
+            sys.stdin = orig_stdin
+
     def test_file_opening_hook(self):
         try:
             # cannot use openhook and inplace mode
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -128,6 +128,9 @@
 Library
 -------
 
+- Issue #25447: fileinput now uses sys.stdin as-is if it does not have a
+  buffer attribute (restores backward compatibility).
+
 - Issue #25971: Optimized creating Fractions from floats by 2 times and from
   Decimals by 3 times.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list