mirror of
https://github.com/python/cpython.git
synced 2025-10-03 13:45:29 +00:00
Merge: #22709: Use stdin as-is if it does not have a buffer attribute.
This commit is contained in:
commit
01759d5554
3 changed files with 15 additions and 1 deletions
|
@ -328,7 +328,7 @@ class FileInput:
|
||||||
if self._filename == '-':
|
if self._filename == '-':
|
||||||
self._filename = '<stdin>'
|
self._filename = '<stdin>'
|
||||||
if 'b' in self._mode:
|
if 'b' in self._mode:
|
||||||
self._file = sys.stdin.buffer
|
self._file = getattr(sys.stdin, 'buffer', sys.stdin)
|
||||||
else:
|
else:
|
||||||
self._file = sys.stdin
|
self._file = sys.stdin
|
||||||
self._isstdin = True
|
self._isstdin = True
|
||||||
|
|
|
@ -240,6 +240,17 @@ class FileInputTests(unittest.TestCase):
|
||||||
lines = list(fi)
|
lines = list(fi)
|
||||||
self.assertEqual(lines, [b'spam, bacon, sausage, and spam'])
|
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):
|
def test_file_opening_hook(self):
|
||||||
try:
|
try:
|
||||||
# cannot use openhook and inplace mode
|
# cannot use openhook and inplace mode
|
||||||
|
|
|
@ -128,6 +128,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #25447: fileinput now uses sys.stdin as-is if it does not have a
|
||||||
|
buffer attribute (restores backward compatibility).
|
||||||
|
|
||||||
- Issue #25971: Optimized creating Fractions from floats by 2 times and from
|
- Issue #25971: Optimized creating Fractions from floats by 2 times and from
|
||||||
Decimals by 3 times.
|
Decimals by 3 times.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue