changeset: 98924:e27c589e1c6a branch: 3.4 parent: 98909:44464bbd339c user: Serhiy Storchaka date: Sun Nov 01 16:43:58 2015 +0200 files: Lib/fileinput.py Lib/test/test_fileinput.py Misc/NEWS description: 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. diff -r 44464bbd339c -r e27c589e1c6a Lib/fileinput.py --- a/Lib/fileinput.py Sat Oct 31 11:48:53 2015 +0000 +++ b/Lib/fileinput.py Sun Nov 01 16:43:58 2015 +0200 @@ -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 -r 44464bbd339c -r e27c589e1c6a Lib/test/test_fileinput.py --- a/Lib/test/test_fileinput.py Sat Oct 31 11:48:53 2015 +0000 +++ b/Lib/test/test_fileinput.py Sun Nov 01 16:43:58 2015 +0200 @@ -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 -r 44464bbd339c -r e27c589e1c6a Misc/NEWS --- a/Misc/NEWS Sat Oct 31 11:48:53 2015 +0000 +++ b/Misc/NEWS Sun Nov 01 16:43:58 2015 +0200 @@ -96,6 +96,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 #21827: Fixed textwrap.dedent() for the case when largest common whitespace is a substring of smallest leading whitespace. Based on patch by Robert Li.