mirror of
https://github.com/python/cpython.git
synced 2025-10-14 10:53:40 +00:00
Module review:
* Replaced "while 1" with "while True" * Rewrote read() and readline() for clarity and speed. * Replaced variable 'list' with 'hlist' * Used augmented assignment in two places.
This commit is contained in:
parent
479d280218
commit
b2e0b92ef1
1 changed files with 30 additions and 40 deletions
|
@ -139,7 +139,7 @@ class HTTPMessage(mimetools.Message):
|
||||||
|
|
||||||
self.dict = {}
|
self.dict = {}
|
||||||
self.unixfrom = ''
|
self.unixfrom = ''
|
||||||
self.headers = list = []
|
self.headers = hlist = []
|
||||||
self.status = ''
|
self.status = ''
|
||||||
headerseen = ""
|
headerseen = ""
|
||||||
firstline = 1
|
firstline = 1
|
||||||
|
@ -148,7 +148,7 @@ class HTTPMessage(mimetools.Message):
|
||||||
unread = self.fp.unread
|
unread = self.fp.unread
|
||||||
elif self.seekable:
|
elif self.seekable:
|
||||||
tell = self.fp.tell
|
tell = self.fp.tell
|
||||||
while 1:
|
while True:
|
||||||
if tell:
|
if tell:
|
||||||
try:
|
try:
|
||||||
startofline = tell()
|
startofline = tell()
|
||||||
|
@ -168,7 +168,7 @@ class HTTPMessage(mimetools.Message):
|
||||||
# XXX Not sure if continuation lines are handled properly
|
# XXX Not sure if continuation lines are handled properly
|
||||||
# for http and/or for repeating headers
|
# for http and/or for repeating headers
|
||||||
# It's a continuation line.
|
# It's a continuation line.
|
||||||
list.append(line)
|
hlist.append(line)
|
||||||
self.addcontinue(headerseen, line.strip())
|
self.addcontinue(headerseen, line.strip())
|
||||||
continue
|
continue
|
||||||
elif self.iscomment(line):
|
elif self.iscomment(line):
|
||||||
|
@ -180,7 +180,7 @@ class HTTPMessage(mimetools.Message):
|
||||||
headerseen = self.isheader(line)
|
headerseen = self.isheader(line)
|
||||||
if headerseen:
|
if headerseen:
|
||||||
# It's a legal header line, save it.
|
# It's a legal header line, save it.
|
||||||
list.append(line)
|
hlist.append(line)
|
||||||
self.addheader(headerseen, line[len(headerseen)+1:].strip())
|
self.addheader(headerseen, line[len(headerseen)+1:].strip())
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
@ -264,12 +264,12 @@ class HTTPResponse:
|
||||||
return
|
return
|
||||||
|
|
||||||
# read until we get a non-100 response
|
# read until we get a non-100 response
|
||||||
while 1:
|
while True:
|
||||||
version, status, reason = self._read_status()
|
version, status, reason = self._read_status()
|
||||||
if status != 100:
|
if status != 100:
|
||||||
break
|
break
|
||||||
# skip the header from the 100 response
|
# skip the header from the 100 response
|
||||||
while 1:
|
while True:
|
||||||
skip = self.fp.readline().strip()
|
skip = self.fp.readline().strip()
|
||||||
if not skip:
|
if not skip:
|
||||||
break
|
break
|
||||||
|
@ -411,7 +411,7 @@ class HTTPResponse:
|
||||||
|
|
||||||
# XXX This accumulates chunks by repeated string concatenation,
|
# XXX This accumulates chunks by repeated string concatenation,
|
||||||
# which is not efficient as the number or size of chunks gets big.
|
# which is not efficient as the number or size of chunks gets big.
|
||||||
while 1:
|
while True:
|
||||||
if chunk_left is None:
|
if chunk_left is None:
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
i = line.find(';')
|
i = line.find(';')
|
||||||
|
@ -441,7 +441,7 @@ class HTTPResponse:
|
||||||
|
|
||||||
# read and discard trailer up to the CRLF terminator
|
# read and discard trailer up to the CRLF terminator
|
||||||
### note: we shouldn't have any trailers!
|
### note: we shouldn't have any trailers!
|
||||||
while 1:
|
while True:
|
||||||
line = self.fp.readline()
|
line = self.fp.readline()
|
||||||
if line == '\r\n':
|
if line == '\r\n':
|
||||||
break
|
break
|
||||||
|
@ -471,8 +471,8 @@ class HTTPResponse:
|
||||||
chunk = self.fp.read(amt)
|
chunk = self.fp.read(amt)
|
||||||
if not chunk:
|
if not chunk:
|
||||||
raise IncompleteRead(s)
|
raise IncompleteRead(s)
|
||||||
s = s + chunk
|
s += chunk
|
||||||
amt = amt - len(chunk)
|
amt -= len(chunk)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def getheader(self, name, default=None):
|
def getheader(self, name, default=None):
|
||||||
|
@ -728,7 +728,7 @@ class HTTPConnection:
|
||||||
|
|
||||||
if body:
|
if body:
|
||||||
self.putheader('Content-Length', str(len(body)))
|
self.putheader('Content-Length', str(len(body)))
|
||||||
for hdr, value in headers.items():
|
for hdr, value in headers.iteritems():
|
||||||
self.putheader(hdr, value)
|
self.putheader(hdr, value)
|
||||||
self.endheaders()
|
self.endheaders()
|
||||||
|
|
||||||
|
@ -840,7 +840,7 @@ class SSLFile(SharedSocketClient):
|
||||||
def _read(self):
|
def _read(self):
|
||||||
buf = ''
|
buf = ''
|
||||||
# put in a loop so that we retry on transient errors
|
# put in a loop so that we retry on transient errors
|
||||||
while 1:
|
while True:
|
||||||
try:
|
try:
|
||||||
buf = self._ssl.read(self._bufsize)
|
buf = self._ssl.read(self._bufsize)
|
||||||
except socket.sslerror, err:
|
except socket.sslerror, err:
|
||||||
|
@ -864,42 +864,32 @@ class SSLFile(SharedSocketClient):
|
||||||
|
|
||||||
def read(self, size=None):
|
def read(self, size=None):
|
||||||
L = [self._buf]
|
L = [self._buf]
|
||||||
avail = len(self._buf)
|
|
||||||
while size is None or avail < size:
|
|
||||||
s = self._read()
|
|
||||||
if s == '':
|
|
||||||
break
|
|
||||||
L.append(s)
|
|
||||||
avail += len(s)
|
|
||||||
all = "".join(L)
|
|
||||||
if size is None:
|
if size is None:
|
||||||
self._buf = ''
|
self._buf = ''
|
||||||
return all
|
for s in iter(self._read, ""):
|
||||||
|
L.append(s)
|
||||||
|
return "".join(L)
|
||||||
else:
|
else:
|
||||||
self._buf = all[size:]
|
avail = len(self._buf)
|
||||||
return all[:size]
|
for s in iter(self._read, ""):
|
||||||
|
L.append(s)
|
||||||
|
avail += len(s)
|
||||||
|
if avail >= size:
|
||||||
|
all = "".join(L)
|
||||||
|
self._buf = all[size:]
|
||||||
|
return all[:size]
|
||||||
|
|
||||||
def readline(self):
|
def readline(self):
|
||||||
L = [self._buf]
|
L = [self._buf]
|
||||||
self._buf = ''
|
self._buf = ''
|
||||||
while 1:
|
for s in iter(self._read, ""):
|
||||||
i = L[-1].find("\n")
|
|
||||||
if i >= 0:
|
|
||||||
break
|
|
||||||
s = self._read()
|
|
||||||
if s == '':
|
|
||||||
break
|
|
||||||
L.append(s)
|
L.append(s)
|
||||||
if i == -1:
|
if "\n" in s:
|
||||||
# loop exited because there is no more data
|
i = s.find("\n") + 1
|
||||||
return "".join(L)
|
self._buf = s[i:]
|
||||||
else:
|
L[-1] = s[:i]
|
||||||
all = "".join(L)
|
break
|
||||||
# XXX could do enough bookkeeping not to do a 2nd search
|
return "".join(L)
|
||||||
i = all.find("\n") + 1
|
|
||||||
line = all[:i]
|
|
||||||
self._buf = all[i:]
|
|
||||||
return line
|
|
||||||
|
|
||||||
class FakeSocket(SharedSocketClient):
|
class FakeSocket(SharedSocketClient):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue