summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-05-14 18:11:08 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-05-14 18:11:08 (GMT)
commitb9183bb4fdcc26a1273e39ea6c32052152898802 (patch)
treee053ae9c2d2d8a50e749131e75d68248bac97241
parent45e8e2f2185fc593ae84a628922c5cbb1799b6ea (diff)
parent946cfc3e238bbd67ff436af5fd05414bbd2e5a08 (diff)
downloadcpython-b9183bb4fdcc26a1273e39ea6c32052152898802.zip
cpython-b9183bb4fdcc26a1273e39ea6c32052152898802.tar.gz
cpython-b9183bb4fdcc26a1273e39ea6c32052152898802.tar.bz2
Issue #21075: fileinput.FileInput now reads bytes from standard stream if
binary mode is specified. Patch by Sam Kimbrel.
-rw-r--r--Lib/fileinput.py5
-rw-r--r--Lib/test/test_fileinput.py10
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
4 files changed, 17 insertions, 2 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index de29518..87758ad 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -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:
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index eba55c9..1d089f5 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -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
diff --git a/Misc/ACKS b/Misc/ACKS
index ff76964..db44737 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -676,6 +676,7 @@ Mads Kiilerich
Jason Killen
Jan Kim
Taek Joo Kim
+Sam Kimbrel
W. Trevor King
Paul Kippes
Steve Kirsch
diff --git a/Misc/NEWS b/Misc/NEWS
index 787239b..e815e0e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,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 #19775: Add a samefile() method to pathlib Path objects. Initial
patch by Vajrasky Kok.