summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-01 16:43:58 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-01 16:43:58 +0200
commitf89b2c2ac4782f1054b756b556808da16792efa6 (patch)
tree801ae514f8ffa661a97c9c118ac1f8c5e62b2af2
parent68f08a92ac3b8eb23e8fea1291cd86b12fffd1af (diff)
downloadcpython-f89b2c2ac4782f1054b756b556808da16792efa6.tar.gz
Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
at the end if the FileInput was opened with binary mode. Patch by Ryosuke Ito.
-rw-r--r--Lib/fileinput.py5
-rw-r--r--Lib/test/test_fileinput.py15
-rw-r--r--Misc/NEWS4
3 files changed, 23 insertions, 1 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index 8af4a57f02..81a7545dd8 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -315,7 +315,10 @@ class FileInput:
return line
if not self._file:
if not self._files:
- return ""
+ if 'b' in self._mode:
+ return b''
+ else:
+ return ''
self._filename = self._files[0]
self._files = self._files[1:]
self._filelineno = 0
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index 1d089f52b8..4765a056f6 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -288,6 +288,21 @@ class FileInputTests(unittest.TestCase):
with self.assertRaises(UnicodeDecodeError):
# Read to the end of file.
list(fi)
+ self.assertEqual(fi.readline(), '')
+ self.assertEqual(fi.readline(), '')
+
+ def test_readline_binary_mode(self):
+ with open(TESTFN, 'wb') as f:
+ f.write(b'A\nB\r\nC\rD')
+ self.addCleanup(safe_unlink, TESTFN)
+
+ with FileInput(files=TESTFN, mode='rb') as fi:
+ self.assertEqual(fi.readline(), b'A\n')
+ self.assertEqual(fi.readline(), b'B\r\n')
+ self.assertEqual(fi.readline(), b'C\rD')
+ # Read to the end of file.
+ self.assertEqual(fi.readline(), b'')
+ self.assertEqual(fi.readline(), b'')
def test_context_manager(self):
try:
diff --git a/Misc/NEWS b/Misc/NEWS
index d2bb816b14..fb7dcd3c05 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -96,6 +96,10 @@ Core and Builtins
Library
-------
+- Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
+ at the end if the FileInput was opened with binary mode.
+ Patch by Ryosuke Ito.
+
- Issue #21827: Fixed textwrap.dedent() for the case when largest common
whitespace is a substring of smallest leading whitespace.
Based on patch by Robert Li.