Skip to content

Commit fd94138

Browse files
committed
add constant MAXIMUM_BUFFER_SIZE
1 parent 892f9fe commit fd94138

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

Lib/_pyio.py

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

26-
# open() uses max(st_blksize, io.DEFAULT_BUFFER_SIZE) when st_blksize is available
26+
# open() uses max(min(blocksize, MAXIMUM_BUFFER_SIZE), DEFAULT_BUFFER_SIZE)
27+
# when the device block size is available.
2728
DEFAULT_BUFFER_SIZE = 128 * 1024 # bytes
29+
MAXIMUM_BUFFER_SIZE = 8192 * 1024 # bytes
2830

2931
# NOTE: Base classes defined here are registered with the "official" ABCs
3032
# defined in io.py. We don't use real inheritance though, because we don't want
@@ -125,7 +127,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
125127
126128
* Binary files are buffered in fixed-size chunks; the size of the buffer
127129
is the maximum of the DEFAULT_BUFFER_SIZE and the device block size.
128-
On most systems, the buffer will typically be 131072 bytes long.
130+
On most systems, the buffer will typically be 128 kilobytes long.
129131
130132
* "Interactive" text files (files for which isatty() returns True)
131133
use line buffering. Other text files use the policy described above
@@ -241,7 +243,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
241243
buffering = -1
242244
line_buffering = True
243245
if buffering < 0:
244-
buffering = max(min(raw._blksize, 8192 * 1024), DEFAULT_BUFFER_SIZE)
246+
buffering = max(min(raw._blksize, MAXIMUM_BUFFER_SIZE), DEFAULT_BUFFER_SIZE)
245247
if buffering < 0:
246248
raise ValueError("invalid buffering size")
247249
if buffering == 0:

Modules/_io/_iomodule.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ PyDoc_STRVAR(module_doc,
6060
"DEFAULT_BUFFER_SIZE\n"
6161
"\n"
6262
" An int containing the default buffer size used by the module's buffered\n"
63-
" I/O classes. open() uses the file's blksize (as obtained by os.stat) if\n"
64-
" possible.\n"
63+
" I/O classes.\n"
64+
"\n"
65+
"MAXIMUM_BUFFER_SIZE\n"
66+
"\n"
67+
" An int containing the maximum buffer size used by the module's buffered\n"
68+
" I/O classes.\n"
6569
);
6670

6771

@@ -132,8 +136,9 @@ the size of a fixed-size chunk buffer. When no buffering argument is
132136
given, the default buffering policy works as follows:
133137
134138
* Binary files are buffered in fixed-size chunks; the size of the buffer
135-
is the maximum of the DEFAULT_BUFFER_SIZE and the device block size.
136-
On most systems, the buffer will typically be 131072 bytes long.
139+
is max(min(blocksize, MAXIMUM_BUFFER_SIZE), DEFAULT_BUFFER_SIZE)
140+
when the device block size is available.
141+
On most systems, the buffer will typically be 128 kilobytes long.
137142
138143
* "Interactive" text files (files for which isatty() returns True)
139144
use line buffering. Other text files use the policy described above
@@ -199,7 +204,7 @@ static PyObject *
199204
_io_open_impl(PyObject *module, PyObject *file, const char *mode,
200205
int buffering, const char *encoding, const char *errors,
201206
const char *newline, int closefd, PyObject *opener)
202-
/*[clinic end generated code: output=aefafc4ce2b46dc0 input=105f6f1cb63368c4]*/
207+
/*[clinic end generated code: output=aefafc4ce2b46dc0 input=e1e2d41c6e922cbe]*/
203208
{
204209
size_t i;
205210

@@ -367,14 +372,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
367372
if (blksize_obj == NULL)
368373
goto error;
369374
buffering = PyLong_AsLong(blksize_obj);
370-
if (buffering > 8192 * 1024)
371-
{
372-
buffering = 8192 * 1024;
373-
}
374-
if (buffering < DEFAULT_BUFFER_SIZE)
375-
{
376-
buffering = DEFAULT_BUFFER_SIZE;
377-
}
375+
buffering = Py_MAX(Py_MIN(buffering, MAXIMUM_BUFFER_SIZE), DEFAULT_BUFFER_SIZE);
378376
Py_DECREF(blksize_obj);
379377
if (buffering == -1 && PyErr_Occurred())
380378
goto error;

Modules/_io/_iomodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ extern Py_ssize_t _PyIO_find_line_ending(
7979
extern int _PyIO_trap_eintr(void);
8080

8181
#define DEFAULT_BUFFER_SIZE (128 * 1024) /* bytes */
82+
#define MAXIMUM_BUFFER_SIZE (8192 * 1024) /* bytes */
8283

8384
/*
8485
* Offset type for positioning.

Modules/_io/clinic/_iomodule.c.h

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)