]> granicus.if.org Git - python/commitdiff
Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 1 Nov 2015 14:43:58 +0000 (16:43 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 1 Nov 2015 14:43:58 +0000 (16:43 +0200)
at the end if the FileInput was opened with binary mode.
Patch by Ryosuke Ito.

Lib/fileinput.py
Lib/test/test_fileinput.py
Misc/NEWS

index 8af4a57f021fa5c191e8131ca0357f1bf3bebc6a..81a7545dd87a5d5d2f6bceb0c2c92450e9398603 100644 (file)
@@ -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
index 1d089f52b8a78b794c445452961b17e4087c1799..4765a056f6a4ffb029a66e3ea65fc30a89145d47 100644 (file)
@@ -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:
index d2bb816b14d209e3fba4dfe7d510f22d16814ee3..fb7dcd3c05c6a5f3222fae727f407de4748501d8 100644 (file)
--- 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.