[Python-checkins] cpython (merge 3.5 -> default): Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''

serhiy.storchaka python-checkins at python.org
Sun Nov 1 09:46:46 EST 2015


https://hg.python.org/cpython/rev/4f0e293e6eb0
changeset:   98926:4f0e293e6eb0
parent:      98923:bd6bfa5fe203
parent:      98925:321b34824020
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Nov 01 16:45:54 2015 +0200
summary:
  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.

files:
  Lib/fileinput.py           |   5 ++++-
  Lib/test/test_fileinput.py |  15 +++++++++++++++
  Misc/NEWS                  |   4 ++++
  3 files changed, 23 insertions(+), 1 deletions(-)


diff --git a/Lib/fileinput.py b/Lib/fileinput.py
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -315,7 +315,10 @@
             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
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -288,6 +288,21 @@
             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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -66,6 +66,10 @@
 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.
   Original patch by John Mark Vandenberg.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list