diff options
author | Guido van Rossum <guido@python.org> | 2000-02-02 16:57:32 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-02-02 16:57:32 (GMT) |
commit | 75a7959335a4390f30048fcb198f618da4408799 (patch) | |
tree | 0a779e5e9ef0e569dd75ef7e311177ddbb29a35c /Lib | |
parent | 823e91c767ef2ddf1568210fdf4763f79c85e103 (diff) | |
download | cpython-75a7959335a4390f30048fcb198f618da4408799.zip cpython-75a7959335a4390f30048fcb198f618da4408799.tar.gz cpython-75a7959335a4390f30048fcb198f618da4408799.tar.bz2 |
In _fileobject, optimize read() a bit (it could be really slow), and
remove "import string" -- use string methods instead!
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/plat-win/socket.py | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/Lib/plat-win/socket.py b/Lib/plat-win/socket.py index d5024f9..0410aa7 100755 --- a/Lib/plat-win/socket.py +++ b/Lib/plat-win/socket.py @@ -96,31 +96,47 @@ class _fileobject: def read(self, n=-1): if n >= 0: - while len(self._rbuf) < n: - new = self._sock.recv(self._rbufsize) + k = len(self._rbuf) + if n <= k: + data = self._rbuf[:n] + self._rbuf = self._rbuf[n:] + return data + n = n - k + l = [self._rbuf] + self._rbuf = "" + while n > 0: + new = self._sock.recv(max(n, self._rbufsize)) if not new: break - self._rbuf = self._rbuf + new - data, self._rbuf = self._rbuf[:n], self._rbuf[n:] - return data + k = len(new) + if k > n: + l.append(new[:n]) + self._rbuf = new[n:] + break + l.append(new) + n = n - k + return "".join(l) + k = max(512, self._rbufsize) + l = [self._rbuf] + self._rbuf = "" while 1: - new = self._sock.recv(self._rbufsize) + new = self._sock.recv(k) if not new: break - self._rbuf = self._rbuf + new - data, self._rbuf = self._rbuf, "" - return data + l.append(new) + k = min(k*2, 1024**2) + return "".join(l) - def readline(self): - import string + def readline(self, limit=-1): data = "" - i = string.find(self._rbuf, '\n') - while i < 0: + i = self._rbuf.find('\n') + while i < 0 and not (0 < limit <= len(self._rbuf)): new = self._sock.recv(self._rbufsize) if not new: break - i = string.find(new, '\n') + i = new.find('\n') if i >= 0: i = i + len(self._rbuf) self._rbuf = self._rbuf + new if i < 0: i = len(self._rbuf) else: i = i+1 + if 0 <= limit < len(self._rbuf): i = limit data, self._rbuf = self._rbuf[:i], self._rbuf[i:] return data |