Skip to content

Commit 3122665

Browse files
committed
Add comments around stat_atopen and why bufsize is + 1
1 parent bfcfcf2 commit 3122665

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Lib/_pyio.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,10 @@ def readall(self):
16551655
if self._stat_atopen is None or self._stat_atopen.st_size <= 0:
16561656
bufsize = DEFAULT_BUFFER_SIZE
16571657
else:
1658+
# In order to detect end of file, need a read() of at least 1
1659+
# byte which returns size 0. Oversize the buffer by 1 byte so the
1660+
# I/O can be completed with two read() calls (one for all data, one
1661+
# for EOF) without needing to resize the buffer.
16581662
bufsize = self._stat_atopen.st_size + 1
16591663

16601664
if self._stat_atopen.st_size > 65536:

Modules/_io/fileio.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ typedef struct {
7474
signed int seekable : 2; /* -1 means unknown */
7575
unsigned int closefd : 1;
7676
char finalizing;
77+
/* Stat result which was grabbed at file open, useful for optimizing common
78+
File I/O patterns to be more efficient. This is only guidance / an estimate,
79+
as it is subject to Time-Of-Check to Time-Of-Use (TOCTOU) issues / bugs.
80+
Both the underlying file descriptor and file may be modified outside of the
81+
fileio object / Python (ex. gh-90102, GH-121941, gh-109523). */
7782
struct _Py_stat_struct *stat_atopen;
7883
PyObject *weakreflist;
7984
PyObject *dict;

0 commit comments

Comments
 (0)