bpo-45975: Simplify some while-loops with walrus operator (GH-29347)

This commit is contained in:
Nick Drozd 2022-11-26 16:33:25 -06:00 committed by GitHub
parent 25bc115df9
commit 024ac542d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 41 additions and 153 deletions

View File

@ -638,10 +638,7 @@ class RawIOBase(IOBase):
def readall(self): def readall(self):
"""Read until EOF, using multiple read() call.""" """Read until EOF, using multiple read() call."""
res = bytearray() res = bytearray()
while True: while data := self.read(DEFAULT_BUFFER_SIZE):
data = self.read(DEFAULT_BUFFER_SIZE)
if not data:
break
res += data res += data
if res: if res:
return bytes(res) return bytes(res)

View File

@ -508,14 +508,8 @@ MAXBINSIZE = (MAXLINESIZE//4)*3
def encode(input, output): def encode(input, output):
"""Encode a file; input and output are binary files.""" """Encode a file; input and output are binary files."""
while True: while s := input.read(MAXBINSIZE):
s = input.read(MAXBINSIZE) while len(s) < MAXBINSIZE and (ns := input.read(MAXBINSIZE-len(s))):
if not s:
break
while len(s) < MAXBINSIZE:
ns = input.read(MAXBINSIZE-len(s))
if not ns:
break
s += ns s += ns
line = binascii.b2a_base64(s) line = binascii.b2a_base64(s)
output.write(line) output.write(line)
@ -523,10 +517,7 @@ def encode(input, output):
def decode(input, output): def decode(input, output):
"""Decode a file; input and output are binary files.""" """Decode a file; input and output are binary files."""
while True: while line := input.readline():
line = input.readline()
if not line:
break
s = binascii.a2b_base64(line) s = binascii.a2b_base64(line)
output.write(s) output.write(s)

View File

@ -108,12 +108,8 @@ def get_ld_headers(file):
p = Popen(["/usr/bin/dump", f"-X{AIX_ABI}", "-H", file], p = Popen(["/usr/bin/dump", f"-X{AIX_ABI}", "-H", file],
universal_newlines=True, stdout=PIPE, stderr=DEVNULL) universal_newlines=True, stdout=PIPE, stderr=DEVNULL)
# be sure to read to the end-of-file - getting all entries # be sure to read to the end-of-file - getting all entries
while True: while ld_header := get_ld_header(p):
ld_header = get_ld_header(p) ldr_headers.append((ld_header, get_ld_header_info(p)))
if ld_header:
ldr_headers.append((ld_header, get_ld_header_info(p)))
else:
break
p.stdout.close() p.stdout.close()
p.wait() p.wait()
return ldr_headers return ldr_headers

View File

@ -49,10 +49,7 @@ class Parser:
feedparser = FeedParser(self._class, policy=self.policy) feedparser = FeedParser(self._class, policy=self.policy)
if headersonly: if headersonly:
feedparser._set_headersonly() feedparser._set_headersonly()
while True: while data := fp.read(8192):
data = fp.read(8192)
if not data:
break
feedparser.feed(data) feedparser.feed(data)
return feedparser.close() return feedparser.close()

View File

@ -434,10 +434,7 @@ class FTP:
""" """
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn: with self.transfercmd(cmd, rest) as conn:
while 1: while data := conn.recv(blocksize):
data = conn.recv(blocksize)
if not data:
break
callback(data) callback(data)
# shutdown ssl layer # shutdown ssl layer
if _SSLSocket is not None and isinstance(conn, _SSLSocket): if _SSLSocket is not None and isinstance(conn, _SSLSocket):
@ -496,10 +493,7 @@ class FTP:
""" """
self.voidcmd('TYPE I') self.voidcmd('TYPE I')
with self.transfercmd(cmd, rest) as conn: with self.transfercmd(cmd, rest) as conn:
while 1: while buf := fp.read(blocksize):
buf = fp.read(blocksize)
if not buf:
break
conn.sendall(buf) conn.sendall(buf)
if callback: if callback:
callback(buf) callback(buf)

View File

@ -578,11 +578,7 @@ class HTTPResponse(io.BufferedIOBase):
assert self.chunked != _UNKNOWN assert self.chunked != _UNKNOWN
value = [] value = []
try: try:
while True: while (chunk_left := self._get_chunk_left()) is not None:
chunk_left = self._get_chunk_left()
if chunk_left is None:
break
if amt is not None and amt <= chunk_left: if amt is not None and amt <= chunk_left:
value.append(self._safe_read(amt)) value.append(self._safe_read(amt))
self.chunk_left = chunk_left - amt self.chunk_left = chunk_left - amt
@ -998,10 +994,7 @@ class HTTPConnection:
encode = self._is_textIO(data) encode = self._is_textIO(data)
if encode and self.debuglevel > 0: if encode and self.debuglevel > 0:
print("encoding file using iso-8859-1") print("encoding file using iso-8859-1")
while 1: while datablock := data.read(self.blocksize):
datablock = data.read(self.blocksize)
if not datablock:
break
if encode: if encode:
datablock = datablock.encode("iso-8859-1") datablock = datablock.encode("iso-8859-1")
sys.audit("http.client.send", self, datablock) sys.audit("http.client.send", self, datablock)
@ -1031,10 +1024,7 @@ class HTTPConnection:
encode = self._is_textIO(readable) encode = self._is_textIO(readable)
if encode and self.debuglevel > 0: if encode and self.debuglevel > 0:
print("encoding file using iso-8859-1") print("encoding file using iso-8859-1")
while True: while datablock := readable.read(self.blocksize):
datablock = readable.read(self.blocksize)
if not datablock:
break
if encode: if encode:
datablock = datablock.encode("iso-8859-1") datablock = datablock.encode("iso-8859-1")
yield datablock yield datablock

View File

@ -1915,9 +1915,7 @@ class LWPCookieJar(FileCookieJar):
"comment", "commenturl") "comment", "commenturl")
try: try:
while 1: while (line := f.readline()) != "":
line = f.readline()
if line == "": break
if not line.startswith(header): if not line.startswith(header):
continue continue
line = line[len(header):].strip() line = line[len(header):].strip()
@ -2017,12 +2015,9 @@ class MozillaCookieJar(FileCookieJar):
filename) filename)
try: try:
while 1: while (line := f.readline()) != "":
line = f.readline()
rest = {} rest = {}
if line == "": break
# httponly is a cookie flag as defined in rfc6265 # httponly is a cookie flag as defined in rfc6265
# when encoded in a netscape cookie file, # when encoded in a netscape cookie file,
# the line is prepended with "#HttpOnly_" # the line is prepended with "#HttpOnly_"

View File

@ -1956,10 +1956,7 @@ class _ProxyFile:
def __iter__(self): def __iter__(self):
"""Iterate over lines.""" """Iterate over lines."""
while True: while line := self.readline():
line = self.readline()
if not line:
return
yield line yield line
def tell(self): def tell(self):

View File

@ -90,9 +90,7 @@ def _readmailcapfile(fp, lineno):
the viewing command is stored with the key "view". the viewing command is stored with the key "view".
""" """
caps = {} caps = {}
while 1: while line := fp.readline():
line = fp.readline()
if not line: break
# Ignore comments and blank lines # Ignore comments and blank lines
if line[0] == '#' or line.strip() == '': if line[0] == '#' or line.strip() == '':
continue continue

View File

@ -217,10 +217,7 @@ class MimeTypes:
list of standard types, else to the list of non-standard list of standard types, else to the list of non-standard
types. types.
""" """
while 1: while line := fp.readline():
line = fp.readline()
if not line:
break
words = line.split() words = line.split()
for i in range(len(words)): for i in range(len(words)):
if words[i][0] == '#': if words[i][0] == '#':

View File

@ -223,8 +223,6 @@ class Stats:
for word, tup in self.sort_arg_dict_default.items(): for word, tup in self.sort_arg_dict_default.items():
fragment = word fragment = word
while fragment: while fragment:
if not fragment:
break
if fragment in dict: if fragment in dict:
bad_list[fragment] = 0 bad_list[fragment] = 0
break break

View File

@ -686,9 +686,7 @@ class HTMLDoc(Doc):
r'RFC[- ]?(\d+)|' r'RFC[- ]?(\d+)|'
r'PEP[- ]?(\d+)|' r'PEP[- ]?(\d+)|'
r'(self\.)?(\w+))') r'(self\.)?(\w+))')
while True: while match := pattern.search(text, here):
match = pattern.search(text, here)
if not match: break
start, end = match.span() start, end = match.span()
results.append(escape(text[here:start])) results.append(escape(text[here:start]))

View File

@ -67,10 +67,7 @@ def encode(input, output, quotetabs, header=False):
output.write(s + lineEnd) output.write(s + lineEnd)
prevline = None prevline = None
while 1: while line := input.readline():
line = input.readline()
if not line:
break
outline = [] outline = []
# Strip off any readline induced trailing newline # Strip off any readline induced trailing newline
stripped = b'' stripped = b''
@ -126,9 +123,7 @@ def decode(input, output, header=False):
return return
new = b'' new = b''
while 1: while line := input.readline():
line = input.readline()
if not line: break
i, n = 0, len(line) i, n = 0, len(line)
if n > 0 and line[n-1:n] == b'\n': if n > 0 and line[n-1:n] == b'\n':
partial = 0; n = n-1 partial = 0; n = n-1

View File

@ -333,10 +333,7 @@ def quote(s):
def _print_tokens(lexer): def _print_tokens(lexer):
while 1: while tt := lexer.get_token():
tt = lexer.get_token()
if not tt:
break
print("Token: " + repr(tt)) print("Token: " + repr(tt))
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -194,10 +194,7 @@ def copyfileobj(fsrc, fdst, length=0):
# Localize variable access to minimize overhead. # Localize variable access to minimize overhead.
fsrc_read = fsrc.read fsrc_read = fsrc.read
fdst_write = fdst.write fdst_write = fdst.write
while True: while buf := fsrc_read(length):
buf = fsrc_read(length)
if not buf:
break
fdst_write(buf) fdst_write(buf)
def _samefile(src, dst): def _samefile(src, dst):

View File

@ -1099,10 +1099,7 @@ if __name__ == '__main__':
toaddrs = prompt("To").split(',') toaddrs = prompt("To").split(',')
print("Enter message, end with ^D:") print("Enter message, end with ^D:")
msg = '' msg = ''
while 1: while line := sys.stdin.readline():
line = sys.stdin.readline()
if not line:
break
msg = msg + line msg = msg + line
print("Message length is %d" % len(msg)) print("Message length is %d" % len(msg))

View File

@ -292,8 +292,7 @@ class BaseServer:
selector.register(self, selectors.EVENT_READ) selector.register(self, selectors.EVENT_READ)
while True: while True:
ready = selector.select(timeout) if selector.select(timeout):
if ready:
return self._handle_request_noblock() return self._handle_request_noblock()
else: else:
if timeout is not None: if timeout is not None:

View File

@ -1262,11 +1262,7 @@ class TarInfo(object):
# the newline. keyword and value are both UTF-8 encoded strings. # the newline. keyword and value are both UTF-8 encoded strings.
regex = re.compile(br"(\d+) ([^=]+)=") regex = re.compile(br"(\d+) ([^=]+)=")
pos = 0 pos = 0
while True: while match := regex.match(buf, pos):
match = regex.match(buf, pos)
if not match:
break
length, keyword = match.groups() length, keyword = match.groups()
length = int(length) length = int(length)
if length == 0: if length == 0:
@ -2418,10 +2414,8 @@ class TarFile(object):
"""Read through the entire archive file and look for readable """Read through the entire archive file and look for readable
members. members.
""" """
while True: while self.next() is not None:
tarinfo = self.next() pass
if tarinfo is None:
break
self._loaded = True self._loaded = True
def _check(self, mode=None): def _check(self, mode=None):

View File

@ -825,10 +825,7 @@ class FileTestCase(unittest.TestCase):
def test_read_10(self): def test_read_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
chunks = [] chunks = []
while True: while result := f.read(10):
result = f.read(10)
if not result:
break
self.assertLessEqual(len(result), 10) self.assertLessEqual(len(result), 10)
chunks.append(result) chunks.append(result)
self.assertEqual(b"".join(chunks), INPUT) self.assertEqual(b"".join(chunks), INPUT)
@ -911,10 +908,7 @@ class FileTestCase(unittest.TestCase):
def test_read1(self): def test_read1(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = [] blocks = []
while True: while result := f.read1():
result = f.read1()
if not result:
break
blocks.append(result) blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT) self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"") self.assertEqual(f.read1(), b"")
@ -926,10 +920,7 @@ class FileTestCase(unittest.TestCase):
def test_read1_10(self): def test_read1_10(self):
with LZMAFile(BytesIO(COMPRESSED_XZ)) as f: with LZMAFile(BytesIO(COMPRESSED_XZ)) as f:
blocks = [] blocks = []
while True: while result := f.read1(10):
result = f.read1(10)
if not result:
break
blocks.append(result) blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT) self.assertEqual(b"".join(blocks), INPUT)
self.assertEqual(f.read1(), b"") self.assertEqual(f.read1(), b"")
@ -937,10 +928,7 @@ class FileTestCase(unittest.TestCase):
def test_read1_multistream(self): def test_read1_multistream(self):
with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f: with LZMAFile(BytesIO(COMPRESSED_XZ * 5)) as f:
blocks = [] blocks = []
while True: while result := f.read1():
result = f.read1()
if not result:
break
blocks.append(result) blocks.append(result)
self.assertEqual(b"".join(blocks), INPUT * 5) self.assertEqual(b"".join(blocks), INPUT * 5)
self.assertEqual(f.read1(), b"") self.assertEqual(f.read1(), b"")

View File

@ -265,10 +265,7 @@ def urlretrieve(url, filename=None, reporthook=None, data=None):
if reporthook: if reporthook:
reporthook(blocknum, bs, size) reporthook(blocknum, bs, size)
while True: while block := fp.read(bs):
block = fp.read(bs)
if not block:
break
read += len(block) read += len(block)
tfp.write(block) tfp.write(block)
blocknum += 1 blocknum += 1
@ -1847,10 +1844,7 @@ class URLopener:
size = int(headers["Content-Length"]) size = int(headers["Content-Length"])
if reporthook: if reporthook:
reporthook(blocknum, bs, size) reporthook(blocknum, bs, size)
while 1: while block := fp.read(bs):
block = fp.read(bs)
if not block:
break
read += len(block) read += len(block)
tfp.write(block) tfp.write(block)
blocknum += 1 blocknum += 1

View File

@ -475,10 +475,7 @@ class SimpleHandler(BaseHandler):
from warnings import warn from warnings import warn
warn("SimpleHandler.stdout.write() should not do partial writes", warn("SimpleHandler.stdout.write() should not do partial writes",
DeprecationWarning) DeprecationWarning)
while True: while data := data[result:]:
data = data[result:]
if not data:
break
result = self.stdout.write(data) result = self.stdout.write(data)
def _flush(self): def _flush(self):

View File

@ -214,10 +214,7 @@ class InputWrapper:
return lines return lines
def __iter__(self): def __iter__(self):
while 1: while line := self.readline():
line = self.readline()
if not line:
return
yield line yield line
def close(self): def close(self):

View File

@ -224,9 +224,7 @@ class Unpacker:
def unpack_list(self, unpack_item): def unpack_list(self, unpack_item):
list = [] list = []
while 1: while (x := self.unpack_uint()) != 0:
x = self.unpack_uint()
if x == 0: break
if x != 1: if x != 1:
raise ConversionError('0 or 1 expected, got %r' % (x,)) raise ConversionError('0 or 1 expected, got %r' % (x,))
item = unpack_item() item = unpack_item()

View File

@ -200,10 +200,7 @@ class ExpatBuilder:
parser = self.getParser() parser = self.getParser()
first_buffer = True first_buffer = True
try: try:
while 1: while buffer := file.read(16*1024):
buffer = file.read(16*1024)
if not buffer:
break
parser.Parse(buffer, False) parser.Parse(buffer, False)
if first_buffer and self.document.documentElement: if first_buffer and self.document.documentElement:
self._setup_subset(buffer) self._setup_subset(buffer)

View File

@ -566,10 +566,7 @@ class ElementTree:
# it with chunks. # it with chunks.
self._root = parser._parse_whole(source) self._root = parser._parse_whole(source)
return self._root return self._root
while True: while data := source.read(65536):
data = source.read(65536)
if not data:
break
parser.feed(data) parser.feed(data)
self._root = parser.close() self._root = parser.close()
return self._root return self._root

View File

@ -120,10 +120,8 @@ class IncrementalParser(XMLReader):
file = source.getCharacterStream() file = source.getCharacterStream()
if file is None: if file is None:
file = source.getByteStream() file = source.getByteStream()
buffer = file.read(self._bufsize) while buffer := file.read(self._bufsize):
while buffer:
self.feed(buffer) self.feed(buffer)
buffer = file.read(self._bufsize)
self.close() self.close()
def feed(self, data): def feed(self, data):

View File

@ -1339,10 +1339,7 @@ class Transport:
p, u = self.getparser() p, u = self.getparser()
while 1: while data := stream.read(1024):
data = stream.read(1024)
if not data:
break
if self.verbose: if self.verbose:
print("body:", repr(data)) print("body:", repr(data))
p.feed(data) p.feed(data)

View File

@ -720,9 +720,7 @@ class ServerHTMLDoc(pydoc.HTMLDoc):
r'RFC[- ]?(\d+)|' r'RFC[- ]?(\d+)|'
r'PEP[- ]?(\d+)|' r'PEP[- ]?(\d+)|'
r'(self\.)?((?:\w|\.)+))\b') r'(self\.)?((?:\w|\.)+))\b')
while 1: while match := pattern.search(text, here):
match = pattern.search(text, here)
if not match: break
start, end = match.span() start, end = match.span()
results.append(escape(text[here:start])) results.append(escape(text[here:start]))