Skip to content

Commit 94fa2a8

Browse files
Merge branch 'main' into fix/129640
2 parents e6aa09d + 72e5b25 commit 94fa2a8

File tree

13 files changed

+1356
-1297
lines changed

13 files changed

+1356
-1297
lines changed

Doc/library/functions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,10 +1405,10 @@ are always available. They are listed here in alphabetical order.
14051405
:func:`io.TextIOWrapper.reconfigure`. When no *buffering* argument is
14061406
given, the default buffering policy works as follows:
14071407

1408-
* Binary files are buffered in fixed-size chunks; the size of the buffer is
1409-
chosen using a heuristic trying to determine the underlying device's "block
1410-
size" and falling back on :const:`io.DEFAULT_BUFFER_SIZE`. On many systems,
1411-
the buffer will typically be 4096 or 8192 bytes long.
1408+
* Binary files are buffered in fixed-size chunks; the size of the buffer
1409+
is ``max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE)``
1410+
when the device block size is available.
1411+
On most systems, the buffer will typically be 128 kilobytes long.
14121412

14131413
* "Interactive" text files (files for which :meth:`~io.IOBase.isatty`
14141414
returns ``True``) use line buffering. Other text files use the policy

Lib/_pyio.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
valid_seek_flags.add(os.SEEK_HOLE)
2424
valid_seek_flags.add(os.SEEK_DATA)
2525

26-
# open() uses st_blksize whenever we can
27-
DEFAULT_BUFFER_SIZE = 8 * 1024 # bytes
26+
# open() uses max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE)
27+
# when the device block size is available.
28+
DEFAULT_BUFFER_SIZE = 128 * 1024 # bytes
2829

2930
# NOTE: Base classes defined here are registered with the "official" ABCs
3031
# defined in io.py. We don't use real inheritance though, because we don't want
@@ -123,10 +124,10 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
123124
the size of a fixed-size chunk buffer. When no buffering argument is
124125
given, the default buffering policy works as follows:
125126
126-
* Binary files are buffered in fixed-size chunks; the size of the buffer
127-
is chosen using a heuristic trying to determine the underlying device's
128-
"block size" and falling back on `io.DEFAULT_BUFFER_SIZE`.
129-
On many systems, the buffer will typically be 4096 or 8192 bytes long.
127+
* Binary files are buffered in fixed-size chunks; the size of the buffer
128+
is max(min(blocksize, 8 MiB), DEFAULT_BUFFER_SIZE)
129+
when the device block size is available.
130+
On most systems, the buffer will typically be 128 kilobytes long.
130131
131132
* "Interactive" text files (files for which isatty() returns True)
132133
use line buffering. Other text files use the policy described above
@@ -242,7 +243,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
242243
buffering = -1
243244
line_buffering = True
244245
if buffering < 0:
245-
buffering = raw._blksize
246+
buffering = max(min(raw._blksize, 8192 * 1024), DEFAULT_BUFFER_SIZE)
246247
if buffering < 0:
247248
raise ValueError("invalid buffering size")
248249
if buffering == 0:

Lib/gzip.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,31 +325,41 @@ def _write_raw(self, data):
325325

326326
return length
327327

328-
def read(self, size=-1):
329-
self._check_not_closed()
328+
def _check_read(self, caller):
330329
if self.mode != READ:
331330
import errno
332-
raise OSError(errno.EBADF, "read() on write-only GzipFile object")
331+
msg = f"{caller}() on write-only GzipFile object"
332+
raise OSError(errno.EBADF, msg)
333+
334+
def read(self, size=-1):
335+
self._check_not_closed()
336+
self._check_read("read")
333337
return self._buffer.read(size)
334338

335339
def read1(self, size=-1):
336340
"""Implements BufferedIOBase.read1()
337341
338342
Reads up to a buffer's worth of data if size is negative."""
339343
self._check_not_closed()
340-
if self.mode != READ:
341-
import errno
342-
raise OSError(errno.EBADF, "read1() on write-only GzipFile object")
344+
self._check_read("read1")
343345

344346
if size < 0:
345347
size = io.DEFAULT_BUFFER_SIZE
346348
return self._buffer.read1(size)
347349

350+
def readinto(self, b):
351+
self._check_not_closed()
352+
self._check_read("readinto")
353+
return self._buffer.readinto(b)
354+
355+
def readinto1(self, b):
356+
self._check_not_closed()
357+
self._check_read("readinto1")
358+
return self._buffer.readinto1(b)
359+
348360
def peek(self, n):
349361
self._check_not_closed()
350-
if self.mode != READ:
351-
import errno
352-
raise OSError(errno.EBADF, "peek() on write-only GzipFile object")
362+
self._check_read("peek")
353363
return self._buffer.peek(n)
354364

355365
@property

0 commit comments

Comments
 (0)