[Python-checkins] cpython (merge 3.4 -> default): Issue #21075: fileinput.FileInput now reads bytes from standard stream if

serhiy.storchaka python-checkins at python.org
Wed May 14 20:12:36 CEST 2014


http://hg.python.org/cpython/rev/4041d4077a85
changeset:   90708:4041d4077a85
parent:      90706:849efd365ab4
parent:      90707:7e640fefc9c1
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed May 14 21:11:08 2014 +0300
summary:
  Issue #21075: fileinput.FileInput now reads bytes from standard stream if
binary mode is specified.  Patch by Sam Kimbrel.

files:
  Lib/fileinput.py           |   5 ++++-
  Lib/test/test_fileinput.py |  10 +++++++++-
  Misc/ACKS                  |   1 +
  Misc/NEWS                  |   3 +++
  4 files changed, 17 insertions(+), 2 deletions(-)


diff --git a/Lib/fileinput.py b/Lib/fileinput.py
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -320,7 +320,10 @@
             self._backupfilename = 0
             if self._filename == '-':
                 self._filename = '<stdin>'
-                self._file = sys.stdin
+                if 'b' in self._mode:
+                    self._file = sys.stdin.buffer
+                else:
+                    self._file = sys.stdin
                 self._isstdin = True
             else:
                 if self._inplace:
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
@@ -19,11 +19,12 @@
 except ImportError:
     gzip = None
 
-from io import StringIO
+from io import BytesIO, StringIO
 from fileinput import FileInput, hook_encoded
 
 from test.support import verbose, TESTFN, run_unittest, check_warnings
 from test.support import unlink as safe_unlink
+from unittest import mock
 
 
 # The fileinput module has 2 interfaces: the FileInput class which does
@@ -232,6 +233,13 @@
         finally:
             remove_tempfiles(t1)
 
+    def test_stdin_binary_mode(self):
+        with mock.patch('sys.stdin') as m_stdin:
+            m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam')
+            fi = FileInput(files=['-'], mode='rb')
+            lines = list(fi)
+            self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
+
     def test_file_opening_hook(self):
         try:
             # cannot use openhook and inplace mode
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -676,6 +676,7 @@
 Jason Killen
 Jan Kim
 Taek Joo Kim
+Sam Kimbrel
 W. Trevor King
 Paul Kippes
 Steve Kirsch
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,9 @@
 Library
 -------
 
+- Issue #21075: fileinput.FileInput now reads bytes from standard stream if
+  binary mode is specified.  Patch by Sam Kimbrel.
+
 - Issue #19775: Add a samefile() method to pathlib Path objects.  Initial
   patch by Vajrasky Kok.
 

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


More information about the Python-checkins mailing list