Skip to content

Commit 4c65c86

Browse files
committed
Update pyio to try and fstat and seek less
1 parent 1a2e7a7 commit 4c65c86

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Lib/_pyio.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,7 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
15751575
# don't exist.
15761576
pass
15771577
self._blksize = getattr(fdfstat, 'st_blksize', 0)
1578+
self._estimated_size = getattr(fdfstat, 'st_size', -1)
15781579
if self._blksize <= 1:
15791580
self._blksize = DEFAULT_BUFFER_SIZE
15801581

@@ -1654,14 +1655,18 @@ def readall(self):
16541655
"""
16551656
self._checkClosed()
16561657
self._checkReadable()
1657-
bufsize = DEFAULT_BUFFER_SIZE
1658-
try:
1659-
pos = os.lseek(self._fd, 0, SEEK_CUR)
1660-
end = os.fstat(self._fd).st_size
1661-
if end >= pos:
1662-
bufsize = end - pos + 1
1663-
except OSError:
1664-
pass
1658+
if self._estimated_size <= 0:
1659+
bufsize = DEFAULT_BUFFER_SIZE
1660+
else:
1661+
bufsize = self._estimated_size + 1
1662+
1663+
if self._estimated_size > 65536:
1664+
try:
1665+
pos = os.lseek(self._fd, 0, SEEK_CUR)
1666+
if self._estimated_size >= pos:
1667+
bufsize = self._estimated_size - pos + 1
1668+
except OSError:
1669+
pass
16651670

16661671
result = bytearray()
16671672
while True:

0 commit comments

Comments
 (0)