changeset: 99758:ded1336bff49 branch: 3.5 parent: 99756:6b9d8957aeef user: R David Murray date: Sat Jan 02 15:41:41 2016 -0500 files: Lib/fileinput.py Lib/test/test_fileinput.py Misc/NEWS description: #22709: Use stdin as-is if it does not have a buffer attribute. This restores backward compatibility lost in the fix for #21075, and is better duck typing. Patch by Akira Li. diff -r 6b9d8957aeef -r ded1336bff49 Lib/fileinput.py --- a/Lib/fileinput.py Fri Jan 01 23:26:53 2016 -0800 +++ b/Lib/fileinput.py Sat Jan 02 15:41:41 2016 -0500 @@ -328,7 +328,7 @@ if self._filename == '-': self._filename = '' if 'b' in self._mode: - self._file = sys.stdin.buffer + self._file = getattr(sys.stdin, 'buffer', sys.stdin) else: self._file = sys.stdin self._isstdin = True diff -r 6b9d8957aeef -r ded1336bff49 Lib/test/test_fileinput.py --- a/Lib/test/test_fileinput.py Fri Jan 01 23:26:53 2016 -0800 +++ b/Lib/test/test_fileinput.py Sat Jan 02 15:41:41 2016 -0500 @@ -240,6 +240,17 @@ lines = list(fi) self.assertEqual(lines, [b'spam, bacon, sausage, and spam']) + def test_detached_stdin_binary_mode(self): + orig_stdin = sys.stdin + try: + sys.stdin = BytesIO(b'spam, bacon, sausage, and spam') + self.assertFalse(hasattr(sys.stdin, 'buffer')) + fi = FileInput(files=['-'], mode='rb') + lines = list(fi) + self.assertEqual(lines, [b'spam, bacon, sausage, and spam']) + finally: + sys.stdin = orig_stdin + def test_file_opening_hook(self): try: # cannot use openhook and inplace mode diff -r 6b9d8957aeef -r ded1336bff49 Misc/NEWS --- a/Misc/NEWS Fri Jan 01 23:26:53 2016 -0800 +++ b/Misc/NEWS Sat Jan 02 15:41:41 2016 -0500 @@ -41,6 +41,9 @@ Library ------- +- Issue #25447: fileinput now uses sys.stdin as-is if it does not have a + buffer attribute (restores backward compatibility). + - Issue #25447: Copying the lru_cache() wrapper object now always works, independedly from the type of the wrapped object (by returning the original object unchanged).