Commit 178bd642 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #21075: fileinput.FileInput now reads bytes from standard stream if

binary mode is specified.  Patch by Sam Kimbrel.
parent 7a765781
...@@ -320,6 +320,9 @@ class FileInput: ...@@ -320,6 +320,9 @@ class FileInput:
self._backupfilename = 0 self._backupfilename = 0
if self._filename == '-': if self._filename == '-':
self._filename = '<stdin>' self._filename = '<stdin>'
if 'b' in self._mode:
self._file = sys.stdin.buffer
else:
self._file = sys.stdin self._file = sys.stdin
self._isstdin = True self._isstdin = True
else: else:
......
...@@ -19,11 +19,12 @@ try: ...@@ -19,11 +19,12 @@ try:
except ImportError: except ImportError:
gzip = None gzip = None
from io import StringIO from io import BytesIO, StringIO
from fileinput import FileInput, hook_encoded from fileinput import FileInput, hook_encoded
from test.support import verbose, TESTFN, run_unittest, check_warnings from test.support import verbose, TESTFN, run_unittest, check_warnings
from test.support import unlink as safe_unlink from test.support import unlink as safe_unlink
from unittest import mock
# The fileinput module has 2 interfaces: the FileInput class which does # The fileinput module has 2 interfaces: the FileInput class which does
...@@ -232,6 +233,13 @@ class FileInputTests(unittest.TestCase): ...@@ -232,6 +233,13 @@ class FileInputTests(unittest.TestCase):
finally: finally:
remove_tempfiles(t1) 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): def test_file_opening_hook(self):
try: try:
# cannot use openhook and inplace mode # cannot use openhook and inplace mode
......
...@@ -674,6 +674,7 @@ Mads Kiilerich ...@@ -674,6 +674,7 @@ Mads Kiilerich
Jason Killen Jason Killen
Jan Kim Jan Kim
Taek Joo Kim Taek Joo Kim
Sam Kimbrel
W. Trevor King W. Trevor King
Paul Kippes Paul Kippes
Steve Kirsch Steve Kirsch
......
...@@ -23,6 +23,9 @@ Core and Builtins ...@@ -23,6 +23,9 @@ Core and Builtins
Library 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 - Issue #21396: Fix TextIOWrapper(..., write_through=True) to not force a
flush() on the underlying binary stream. Patch by akira. flush() on the underlying binary stream. Patch by akira.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment