]> granicus.if.org Git - python/commitdiff
Issue #21075: fileinput.FileInput now reads bytes from standard stream if
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 14 May 2014 18:08:33 +0000 (21:08 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 14 May 2014 18:08:33 +0000 (21:08 +0300)
binary mode is specified.  Patch by Sam Kimbrel.

Lib/fileinput.py
Lib/test/test_fileinput.py
Misc/ACKS
Misc/NEWS

index de2951844aef2b5831558c41d5c643cf5456b3cf..87758ad82b3026e87e0391e86edd1f9fc6661a92 100644 (file)
@@ -320,7 +320,10 @@ class FileInput:
             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:
index eba55c99a4b634f17aea77ac4a8a489ae669a5e3..1d089f52b8a78b794c445452961b17e4087c1799 100644 (file)
@@ -19,11 +19,12 @@ try:
 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 @@ class FileInputTests(unittest.TestCase):
         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
index bbcd00e75523cbed74fac96a151d1516a07d69c8..62d2b204a1358f048efab6aecca40907c368334d 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -674,6 +674,7 @@ Mads Kiilerich
 Jason Killen
 Jan Kim
 Taek Joo Kim
+Sam Kimbrel
 W. Trevor King
 Paul Kippes
 Steve Kirsch
index eff6573d94705a3fca28c9690066d70c7a6648de..161016ab402c6b79d1235c9500d60042e463bdf5 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21075: fileinput.FileInput now reads bytes from standard stream if
+  binary mode is specified.  Patch by Sam Kimbrel.
+
 - Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a
   flush() on the underlying binary stream.  Patch by akira.