Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2e0b92

Browse files
committed
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.
1 parent 479d280 commit b2e0b92

1 file changed

Lines changed: 30 additions & 40 deletions

File tree

Lib/httplib.py

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def readheaders(self):
139139

140140
self.dict = {}
141141
self.unixfrom = ''
142-
self.headers = list = []
142+
self.headers = hlist = []
143143
self.status = ''
144144
headerseen = ""
145145
firstline = 1
@@ -148,7 +148,7 @@ def readheaders(self):
148148
unread = self.fp.unread
149149
elif self.seekable:
150150
tell = self.fp.tell
151-
while 1:
151+
while True:
152152
if tell:
153153
try:
154154
startofline = tell()
@@ -168,7 +168,7 @@ def readheaders(self):
168168
# XXX Not sure if continuation lines are handled properly
169169
# for http and/or for repeating headers
170170
# It's a continuation line.
171-
list.append(line)
171+
hlist.append(line)
172172
self.addcontinue(headerseen, line.strip())
173173
continue
174174
elif self.iscomment(line):
@@ -180,7 +180,7 @@ def readheaders(self):
180180
headerseen = self.isheader(line)
181181
if headerseen:
182182
# It's a legal header line, save it.
183-
list.append(line)
183+
hlist.append(line)
184184
self.addheader(headerseen, line[len(headerseen)+1:].strip())
185185
continue
186186
else:
@@ -264,12 +264,12 @@ def begin(self):
264264
return
265265

266266
# read until we get a non-100 response
267-
while 1:
267+
while True:
268268
version, status, reason = self._read_status()
269269
if status != 100:
270270
break
271271
# skip the header from the 100 response
272-
while 1:
272+
while True:
273273
skip = self.fp.readline().strip()
274274
if not skip:
275275
break
@@ -411,7 +411,7 @@ def _read_chunked(self, amt):
411411

412412
# XXX This accumulates chunks by repeated string concatenation,
413413
# which is not efficient as the number or size of chunks gets big.
414-
while 1:
414+
while True:
415415
if chunk_left is None:
416416
line = self.fp.readline()
417417
i = line.find(';')
@@ -441,7 +441,7 @@ def _read_chunked(self, amt):
441441

442442
# read and discard trailer up to the CRLF terminator
443443
### note: we shouldn't have any trailers!
444-
while 1:
444+
while True:
445445
line = self.fp.readline()
446446
if line == '\r\n':
447447
break
@@ -471,8 +471,8 @@ def _safe_read(self, amt):
471471
chunk = self.fp.read(amt)
472472
if not chunk:
473473
raise IncompleteRead(s)
474-
s = s + chunk
475-
amt = amt - len(chunk)
474+
s += chunk
475+
amt -= len(chunk)
476476
return s
477477

478478
def getheader(self, name, default=None):
@@ -728,7 +728,7 @@ def _send_request(self, method, url, body, headers):
728728

729729
if body:
730730
self.putheader('Content-Length', str(len(body)))
731-
for hdr, value in headers.items():
731+
for hdr, value in headers.iteritems():
732732
self.putheader(hdr, value)
733733
self.endheaders()
734734

@@ -840,7 +840,7 @@ def __init__(self, sock, ssl, bufsize=None):
840840
def _read(self):
841841
buf = ''
842842
# put in a loop so that we retry on transient errors
843-
while 1:
843+
while True:
844844
try:
845845
buf = self._ssl.read(self._bufsize)
846846
except socket.sslerror, err:
@@ -864,42 +864,32 @@ def _read(self):
864864

865865
def read(self, size=None):
866866
L = [self._buf]
867-
avail = len(self._buf)
868-
while size is None or avail < size:
869-
s = self._read()
870-
if s == '':
871-
break
872-
L.append(s)
873-
avail += len(s)
874-
all = "".join(L)
875867
if size is None:
876868
self._buf = ''
877-
return all
869+
for s in iter(self._read, ""):
870+
L.append(s)
871+
return "".join(L)
878872
else:
879-
self._buf = all[size:]
880-
return all[:size]
873+
avail = len(self._buf)
874+
for s in iter(self._read, ""):
875+
L.append(s)
876+
avail += len(s)
877+
if avail >= size:
878+
all = "".join(L)
879+
self._buf = all[size:]
880+
return all[:size]
881881

882882
def readline(self):
883883
L = [self._buf]
884884
self._buf = ''
885-
while 1:
886-
i = L[-1].find("\n")
887-
if i >= 0:
888-
break
889-
s = self._read()
890-
if s == '':
891-
break
885+
for s in iter(self._read, ""):
892886
L.append(s)
893-
if i == -1:
894-
# loop exited because there is no more data
895-
return "".join(L)
896-
else:
897-
all = "".join(L)
898-
# XXX could do enough bookkeeping not to do a 2nd search
899-
i = all.find("\n") + 1
900-
line = all[:i]
901-
self._buf = all[i:]
902-
return line
887+
if "\n" in s:
888+
i = s.find("\n") + 1
889+
self._buf = s[i:]
890+
L[-1] = s[:i]
891+
break
892+
return "".join(L)
903893

904894
class FakeSocket(SharedSocketClient):
905895

0 commit comments

Comments
 (0)