diff -r ab7f57e92629 Lib/fileinput.py --- a/Lib/fileinput.py Mon Apr 14 12:16:18 2014 -0400 +++ b/Lib/fileinput.py Tue Apr 15 12:36:37 2014 -0400 @@ -320,7 +320,10 @@ self._backupfilename = 0 if self._filename == '-': self._filename = '' - self._file = sys.stdin + if 'b' in self._mode: + self._file = sys.stdin.buffer + else: + self._file = sys.stdin self._isstdin = True else: if self._inplace: diff -r ab7f57e92629 Lib/test/test_fileinput.py --- a/Lib/test/test_fileinput.py Mon Apr 14 12:16:18 2014 -0400 +++ b/Lib/test/test_fileinput.py Tue Apr 15 12:36:37 2014 -0400 @@ -19,11 +19,12 @@ except ImportError: gzip = None -from io import StringIO +from io import BytesIO, StringIO from fileinput import FileInput, hook_encoded from test.support import verbose, TESTFN, run_unittest, check_warnings from test.support import unlink as safe_unlink +from unittest import mock # The fileinput module has 2 interfaces: the FileInput class which does @@ -232,6 +233,13 @@ finally: remove_tempfiles(t1) + def test_stdin_binary_mode(self): + with mock.patch('sys.stdin') as m_stdin: + m_stdin.buffer = BytesIO(b'spam, bacon, sausage, and spam') + fi = FileInput(files=['-'], mode='rb') + lines = list(fi) + self.assertEqual(lines, [b'spam, bacon, sausage, and spam']) + def test_file_opening_hook(self): try: # cannot use openhook and inplace mode