Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 01759d5

Browse files
committed
Merge: #22709: Use stdin as-is if it does not have a buffer attribute.
2 parents 5b3455c + 830207e commit 01759d5

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/fileinput.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def readline(self):
328328
if self._filename == '-':
329329
self._filename = '<stdin>'
330330
if 'b' in self._mode:
331-
self._file = sys.stdin.buffer
331+
self._file = getattr(sys.stdin, 'buffer', sys.stdin)
332332
else:
333333
self._file = sys.stdin
334334
self._isstdin = True

Lib/test/test_fileinput.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ def test_stdin_binary_mode(self):
240240
lines = list(fi)
241241
self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
242242

243+
def test_detached_stdin_binary_mode(self):
244+
orig_stdin = sys.stdin
245+
try:
246+
sys.stdin = BytesIO(b'spam, bacon, sausage, and spam')
247+
self.assertFalse(hasattr(sys.stdin, 'buffer'))
248+
fi = FileInput(files=['-'], mode='rb')
249+
lines = list(fi)
250+
self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
251+
finally:
252+
sys.stdin = orig_stdin
253+
243254
def test_file_opening_hook(self):
244255
try:
245256
# cannot use openhook and inplace mode

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Core and Builtins
128128
Library
129129
-------
130130

131+
- Issue #25447: fileinput now uses sys.stdin as-is if it does not have a
132+
buffer attribute (restores backward compatibility).
133+
131134
- Issue #25971: Optimized creating Fractions from floats by 2 times and from
132135
Decimals by 3 times.
133136

0 commit comments

Comments
 (0)