Skip to content

Commit

Permalink
bpo-45975: Simplify some while-loops with walrus operator (pythonGH-2…
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd authored Nov 26, 2022
1 parent 25bc115 commit 024ac54
Show file tree
Hide file tree
Showing 28 changed files with 41 additions and 153 deletions.
5 changes: 1 addition & 4 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,7 @@ def read(self, size=-1):
def readall(self):
"""Read until EOF, using multiple read() call."""
res = bytearray()
while True:
data = self.read(DEFAULT_BUFFER_SIZE)
if not data:
break
while data := self.read(DEFAULT_BUFFER_SIZE):
res += data
if res:
return bytes(res)
Expand Down
15 changes: 3 additions & 12 deletions Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,25 +508,16 @@ def b85decode(b):

def encode(input, output):
"""Encode a file; input and output are binary files."""
while True:
s = input.read(MAXBINSIZE)
if not s:
break
while len(s) < MAXBINSIZE:
ns = input.read(MAXBINSIZE-len(s))
if not ns:
break
while s := input.read(MAXBINSIZE):
while len(s) < MAXBINSIZE and (ns := input.read(MAXBINSIZE-len(s))):
s += ns
line = binascii.b2a_base64(s)
output.write(line)


def decode(input, output):
"""Decode a file; input and output are binary files."""
while True:
line = input.readline()
if not line:
break
while line := input.readline():
s = binascii.a2b_base64(line)
output.write(s)

Expand Down
8 changes: 2 additions & 6 deletions Lib/ctypes/_aix.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ def get_ld_headers(file):
p = Popen(["/usr/bin/dump", f"-X{AIX_ABI}", "-H", file],
universal_newlines=True, stdout=PIPE, stderr=DEVNULL)
# be sure to read to the end-of-file - getting all entries
while True:
ld_header = get_ld_header(p)
if ld_header:
ldr_headers.append((ld_header, get_ld_header_info(p)))
else:
break
while ld_header := get_ld_header(p):
ldr_headers.append((ld_header, get_ld_header_info(p)))
p.stdout.close()
p.wait()
return ldr_headers
Expand Down
5 changes: 1 addition & 4 deletions Lib/email/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ def parse(self, fp, headersonly=False):
feedparser = FeedParser(self._class, policy=self.policy)
if headersonly:
feedparser._set_headersonly()
while True:
data = fp.read(8192)
if not data:
break
while data := fp.read(8192):
feedparser.feed(data)
return feedparser.close()

Expand Down
10 changes: 2 additions & 8 deletions Lib/ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,7 @@ def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
"""
self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn:
while 1:
data = conn.recv(blocksize)
if not data:
break
while data := conn.recv(blocksize):
callback(data)
# shutdown ssl layer
if _SSLSocket is not None and isinstance(conn, _SSLSocket):
Expand Down Expand Up @@ -496,10 +493,7 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
"""
self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn:
while 1:
buf = fp.read(blocksize)
if not buf:
break
while buf := fp.read(blocksize):
conn.sendall(buf)
if callback:
callback(buf)
Expand Down
16 changes: 3 additions & 13 deletions Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,7 @@ def _read_chunked(self, amt=None):
assert self.chunked != _UNKNOWN
value = []
try:
while True:
chunk_left = self._get_chunk_left()
if chunk_left is None:
break

while (chunk_left := self._get_chunk_left()) is not None:
if amt is not None and amt <= chunk_left:
value.append(self._safe_read(amt))
self.chunk_left = chunk_left - amt
Expand Down Expand Up @@ -998,10 +994,7 @@ def send(self, data):
encode = self._is_textIO(data)
if encode and self.debuglevel > 0:
print("encoding file using iso-8859-1")
while 1:
datablock = data.read(self.blocksize)
if not datablock:
break
while datablock := data.read(self.blocksize):
if encode:
datablock = datablock.encode("iso-8859-1")
sys.audit("http.client.send", self, datablock)
Expand Down Expand Up @@ -1031,10 +1024,7 @@ def _read_readable(self, readable):
encode = self._is_textIO(readable)
if encode and self.debuglevel > 0:
print("encoding file using iso-8859-1")
while True:
datablock = readable.read(self.blocksize)
if not datablock:
break
while datablock := readable.read(self.blocksize):
if encode:
datablock = datablock.encode("iso-8859-1")
yield datablock
Expand Down
9 changes: 2 additions & 7 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1915,9 +1915,7 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
"comment", "commenturl")

try:
while 1:
line = f.readline()
if line == "": break
while (line := f.readline()) != "":
if not line.startswith(header):
continue
line = line[len(header):].strip()
Expand Down Expand Up @@ -2017,12 +2015,9 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
filename)

try:
while 1:
line = f.readline()
while (line := f.readline()) != "":
rest = {}

if line == "": break

# httponly is a cookie flag as defined in rfc6265
# when encoded in a netscape cookie file,
# the line is prepended with "#HttpOnly_"
Expand Down
5 changes: 1 addition & 4 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1956,10 +1956,7 @@ def readlines(self, sizehint=None):

def __iter__(self):
"""Iterate over lines."""
while True:
line = self.readline()
if not line:
return
while line := self.readline():
yield line

def tell(self):
Expand Down
4 changes: 1 addition & 3 deletions Lib/mailcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def _readmailcapfile(fp, lineno):
the viewing command is stored with the key "view".
"""
caps = {}
while 1:
line = fp.readline()
if not line: break
while line := fp.readline():
# Ignore comments and blank lines
if line[0] == '#' or line.strip() == '':
continue
Expand Down
5 changes: 1 addition & 4 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ def readfp(self, fp, strict=True):
list of standard types, else to the list of non-standard
types.
"""
while 1:
line = fp.readline()
if not line:
break
while line := fp.readline():
words = line.split()
for i in range(len(words)):
if words[i][0] == '#':
Expand Down
2 changes: 0 additions & 2 deletions Lib/pstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,6 @@ def get_sort_arg_defs(self):
for word, tup in self.sort_arg_dict_default.items():
fragment = word
while fragment:
if not fragment:
break
if fragment in dict:
bad_list[fragment] = 0
break
Expand Down
4 changes: 1 addition & 3 deletions Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,7 @@ def markup(self, text, escape=None, funcs={}, classes={}, methods={}):
r'RFC[- ]?(\d+)|'
r'PEP[- ]?(\d+)|'
r'(self\.)?(\w+))')
while True:
match = pattern.search(text, here)
if not match: break
while match := pattern.search(text, here):
start, end = match.span()
results.append(escape(text[here:start]))

Expand Down
9 changes: 2 additions & 7 deletions Lib/quopri.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def write(s, output=output, lineEnd=b'\n'):
output.write(s + lineEnd)

prevline = None
while 1:
line = input.readline()
if not line:
break
while line := input.readline():
outline = []
# Strip off any readline induced trailing newline
stripped = b''
Expand Down Expand Up @@ -126,9 +123,7 @@ def decode(input, output, header=False):
return

new = b''
while 1:
line = input.readline()
if not line: break
while line := input.readline():
i, n = 0, len(line)
if n > 0 and line[n-1:n] == b'\n':
partial = 0; n = n-1
Expand Down
5 changes: 1 addition & 4 deletions Lib/shlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,7 @@ def quote(s):


def _print_tokens(lexer):
while 1:
tt = lexer.get_token()
if not tt:
break
while tt := lexer.get_token():
print("Token: " + repr(tt))

if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ def copyfileobj(fsrc, fdst, length=0):
# Localize variable access to minimize overhead.
fsrc_read = fsrc.read
fdst_write = fdst.write
while True:
buf = fsrc_read(length)
if not buf:
break
while buf := fsrc_read(length):
fdst_write(buf)

def _samefile(src, dst):
Expand Down
5 changes: 1 addition & 4 deletions Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,10 +1099,7 @@ def prompt(prompt):
toaddrs = prompt("To").split(',')
print("Enter message, end with ^D:")
msg = ''
while 1:
line = sys.stdin.readline()
if not line:
break
while line := sys.stdin.readline():
msg = msg + line
print("Message length is %d" % len(msg))

Expand Down
3 changes: 1 addition & 2 deletions Lib/socketserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ def handle_request(self):
selector.register(self, selectors.EVENT_READ)

while True:
ready = selector.select(timeout)
if ready:
if selector.select(timeout):
return self._handle_request_noblock()
else:
if timeout is not None:
Expand Down
12 changes: 3 additions & 9 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,11 +1262,7 @@ def _proc_pax(self, tarfile):
# the newline. keyword and value are both UTF-8 encoded strings.
regex = re.compile(br"(\d+) ([^=]+)=")
pos = 0
while True:
match = regex.match(buf, pos)
if not match:
break

while match := regex.match(buf, pos):
length, keyword = match.groups()
length = int(length)
if length == 0:
Expand Down Expand Up @@ -2418,10 +2414,8 @@ def _load(self):
"""Read through the entire archive file and look for readable
members.
"""
while True:
tarinfo = self.next()
if tarinfo is None:
break
while self.next() is not None:
pass
self._loaded = True

def _check(self, mode=None):
Expand Down
20 changes: 4 additions & 16 deletions Lib/test/test_lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,10 +825,7 @@ def test_read_0(self):
def test_read_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
chunks = []
while True:
result = f.read(10)
if not result:
break
while result := f.read(10):
self.assertLessEqual(len(result), 10)
chunks.append(result)
self.assertEqual(b"".join(chunks), INPUT)
Expand Down Expand Up @@ -911,10 +908,7 @@ def test_read_bad_data(self):
def test_read1(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = []
while True:
result = f.read1()
if not result:
break
while result := f.read1():
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"")
Expand All @@ -926,21 +920,15 @@ def test_read1_0(self):
def test_read1_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = []
while True:
result = f.read1(10)
if not result:
break
while result := f.read1(10):
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"")

def test_read1_multistream(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
blocks = []
while True:
result = f.read1()
if not result:
break
while result := f.read1():
blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT * 5)
self.assertEqual(f.read1(), b"")
Expand Down
10 changes: 2 additions & 8 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,7 @@ def urlretrieve(url, filename=None, reporthook=None, data=None):
if reporthook:
reporthook(blocknum, bs, size)

while True:
block = fp.read(bs)
if not block:
break
while block := fp.read(bs):
read += len(block)
tfp.write(block)
blocknum += 1
Expand Down Expand Up @@ -1847,10 +1844,7 @@ def retrieve(self, url, filename=None, reporthook=None, data=None):
size = int(headers["Content-Length"])
if reporthook:
reporthook(blocknum, bs, size)
while 1:
block = fp.read(bs)
if not block:
break
while block := fp.read(bs):
read += len(block)
tfp.write(block)
blocknum += 1
Expand Down
5 changes: 1 addition & 4 deletions Lib/wsgiref/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ def _write(self,data):
from warnings import warn
warn("SimpleHandler.stdout.write() should not do partial writes",
DeprecationWarning)
while True:
data = data[result:]
if not data:
break
while data := data[result:]:
result = self.stdout.write(data)

def _flush(self):
Expand Down
5 changes: 1 addition & 4 deletions Lib/wsgiref/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,7 @@ def readlines(self, *args):
return lines

def __iter__(self):
while 1:
line = self.readline()
if not line:
return
while line := self.readline():
yield line

def close(self):
Expand Down
Loading

0 comments on commit 024ac54

Please sign in to comment.