File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -315,7 +315,10 @@ def readline(self):
315315 return line
316316 if not self ._file :
317317 if not self ._files :
318- return ""
318+ if 'b' in self ._mode :
319+ return b''
320+ else :
321+ return ''
319322 self ._filename = self ._files [0 ]
320323 self ._files = self ._files [1 :]
321324 self ._filelineno = 0
Original file line number Diff line number Diff line change @@ -288,6 +288,21 @@ def test_readline(self):
288288 with self .assertRaises (UnicodeDecodeError ):
289289 # Read to the end of file.
290290 list (fi )
291+ self .assertEqual (fi .readline (), '' )
292+ self .assertEqual (fi .readline (), '' )
293+
294+ def test_readline_binary_mode (self ):
295+ with open (TESTFN , 'wb' ) as f :
296+ f .write (b'A\n B\r \n C\r D' )
297+ self .addCleanup (safe_unlink , TESTFN )
298+
299+ with FileInput (files = TESTFN , mode = 'rb' ) as fi :
300+ self .assertEqual (fi .readline (), b'A\n ' )
301+ self .assertEqual (fi .readline (), b'B\r \n ' )
302+ self .assertEqual (fi .readline (), b'C\r D' )
303+ # Read to the end of file.
304+ self .assertEqual (fi .readline (), b'' )
305+ self .assertEqual (fi .readline (), b'' )
291306
292307 def test_context_manager (self ):
293308 try :
Original file line number Diff line number Diff line change @@ -96,6 +96,10 @@ Core and Builtins
9696Library
9797-------
9898
99+ - Issue #25510: fileinput.FileInput.readline() now returns b'' instead of ''
100+ at the end if the FileInput was opened with binary mode.
101+ Patch by Ryosuke Ito.
102+
99103- Issue #21827: Fixed textwrap.dedent() for the case when largest common
100104 whitespace is a substring of smallest leading whitespace.
101105 Based on patch by Robert Li.
You can’t perform that action at this time.
0 commit comments