summaryrefslogtreecommitdiffstats
path: root/Lib/io.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-06-07 23:45:37 (GMT)
committerGuido van Rossum <guido@python.org>2007-06-07 23:45:37 (GMT)
commit48fc58ad31408e0f452d046a23c59b7556f6a9b0 (patch)
treeef1405180331f17842dd8880057768b7410d99e2 /Lib/io.py
parent1325790b932c4bab4f8f94f5a36c09f4036ed9f8 (diff)
downloadcpython-48fc58ad31408e0f452d046a23c59b7556f6a9b0.zip
cpython-48fc58ad31408e0f452d046a23c59b7556f6a9b0.tar.gz
cpython-48fc58ad31408e0f452d046a23c59b7556f6a9b0.tar.bz2
Accellerate binary readline() a bit.
Diffstat (limited to 'Lib/io.py')
-rw-r--r--Lib/io.py22
1 files changed, 14 insertions, 8 deletions
diff --git a/Lib/io.py b/Lib/io.py
index f1be881..4d46b47 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -298,17 +298,23 @@ class IOBase:
### Readline ###
- def readline(self, sizehint: int = -1) -> bytes:
- """For backwards compatibility, a (slow) readline()."""
- if sizehint is None:
- sizehint = -1
- res = b""
- while sizehint < 0 or len(res) < sizehint:
- b = self.read(1)
+ def readline(self, limit: int = -1) -> bytes:
+ """For backwards compatibility, a (slowish) readline()."""
+ if limit is None:
+ limit = -1
+ res = bytes()
+ while limit < 0 or len(res) < limit:
+ readahead = self.peek(1, unsafe=True)
+ if not readahead:
+ break
+ n = (readahead.find(b"\n") + 1) or len(readahead)
+ if limit >= 0:
+ n = min(n, limit)
+ b = self.read(n)
if not b:
break
res += b
- if b == b"\n":
+ if res.endswith(b"\n"):
break
return res