Skip to content

Commit

Permalink
Issue python#21075: fileinput.FileInput now reads bytes from standard…
Browse files Browse the repository at this point in the history
… stream if

binary mode is specified.  Patch by Sam Kimbrel.
  • Loading branch information
serhiy-storchaka committed May 14, 2014
1 parent e2d6690 commit 946cfc3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Lib/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ def readline(self):
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:
Expand Down
10 changes: 9 additions & 1 deletion Lib/test/test_fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -232,6 +233,13 @@ def test_opening_mode(self):
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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ Mads Kiilerich
Jason Killen
Jan Kim
Taek Joo Kim
Sam Kimbrel
W. Trevor King
Paul Kippes
Steve Kirsch
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit 946cfc3

Please sign in to comment.