Commit 56275dc1 authored by Serhiy Storchaka's avatar Serhiy Storchaka

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.
parents 14eefe35 314464d0
...@@ -315,7 +315,10 @@ class FileInput: ...@@ -315,7 +315,10 @@ class FileInput:
return line return line
if not self._file: if not self._file:
if not self._files: if not self._files:
return "" if 'b' in self._mode:
return b''
else:
return ''
self._filename = self._files[0] self._filename = self._files[0]
self._files = self._files[1:] self._files = self._files[1:]
self._filelineno = 0 self._filelineno = 0
......
...@@ -288,6 +288,21 @@ class FileInputTests(unittest.TestCase): ...@@ -288,6 +288,21 @@ class FileInputTests(unittest.TestCase):
with self.assertRaises(UnicodeDecodeError): with self.assertRaises(UnicodeDecodeError):
# Read to the end of file. # Read to the end of file.
list(fi) 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): def test_context_manager(self):
try: try:
......
...@@ -48,6 +48,10 @@ Core and Builtins ...@@ -48,6 +48,10 @@ Core and Builtins
Library 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 #25503: Fixed inspect.getdoc() for inherited docstrings of properties. - Issue #25503: Fixed inspect.getdoc() for inherited docstrings of properties.
Original patch by John Mark Vandenberg. Original patch by John Mark Vandenberg.
......
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