diff options
author | R David Murray <rdmurray@bitdance.com> | 2016-01-02 20:41:41 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2016-01-02 20:41:41 (GMT) |
commit | 830207e8f32df949ec35e2e7f5afe98975e262c9 (patch) | |
tree | faddddb33d51837ca7de7bfecb0f809d535f0255 | |
parent | fcb6db50d3cce6337f28c9c1b891905cc323ad13 (diff) | |
download | cpython-830207e8f32df949ec35e2e7f5afe98975e262c9.zip cpython-830207e8f32df949ec35e2e7f5afe98975e262c9.tar.gz cpython-830207e8f32df949ec35e2e7f5afe98975e262c9.tar.bz2 |
#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.
-rw-r--r-- | Lib/fileinput.py | 2 | ||||
-rw-r--r-- | Lib/test/test_fileinput.py | 11 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 15 insertions, 1 deletions
diff --git a/Lib/fileinput.py b/Lib/fileinput.py index c41b94a..021e39f 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -328,7 +328,7 @@ class FileInput: if self._filename == '-': self._filename = '<stdin>' 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 --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py index 4765a05..91c1166 100644 --- a/Lib/test/test_fileinput.py +++ b/Lib/test/test_fileinput.py @@ -240,6 +240,17 @@ class FileInputTests(unittest.TestCase): 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 @@ -41,6 +41,9 @@ Core and Builtins 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). |