Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit024ac54

Browse files
authored
bpo-45975: Simplify some while-loops with walrus operator (GH-29347)
1 parent25bc115 commit024ac54

28 files changed

+41
-153
lines changed

‎Lib/_pyio.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,7 @@ def read(self, size=-1):
638638
defreadall(self):
639639
"""Read until EOF, using multiple read() call."""
640640
res=bytearray()
641-
whileTrue:
642-
data=self.read(DEFAULT_BUFFER_SIZE)
643-
ifnotdata:
644-
break
641+
whiledata:=self.read(DEFAULT_BUFFER_SIZE):
645642
res+=data
646643
ifres:
647644
returnbytes(res)

‎Lib/base64.py‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -508,25 +508,16 @@ def b85decode(b):
508508

509509
defencode(input,output):
510510
"""Encode a file; input and output are binary files."""
511-
whileTrue:
512-
s=input.read(MAXBINSIZE)
513-
ifnots:
514-
break
515-
whilelen(s)<MAXBINSIZE:
516-
ns=input.read(MAXBINSIZE-len(s))
517-
ifnotns:
518-
break
511+
whiles:=input.read(MAXBINSIZE):
512+
whilelen(s)<MAXBINSIZEand (ns:=input.read(MAXBINSIZE-len(s))):
519513
s+=ns
520514
line=binascii.b2a_base64(s)
521515
output.write(line)
522516

523517

524518
defdecode(input,output):
525519
"""Decode a file; input and output are binary files."""
526-
whileTrue:
527-
line=input.readline()
528-
ifnotline:
529-
break
520+
whileline:=input.readline():
530521
s=binascii.a2b_base64(line)
531522
output.write(s)
532523

‎Lib/ctypes/_aix.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,8 @@ def get_ld_headers(file):
108108
p=Popen(["/usr/bin/dump",f"-X{AIX_ABI}","-H",file],
109109
universal_newlines=True,stdout=PIPE,stderr=DEVNULL)
110110
# be sure to read to the end-of-file - getting all entries
111-
whileTrue:
112-
ld_header=get_ld_header(p)
113-
ifld_header:
114-
ldr_headers.append((ld_header,get_ld_header_info(p)))
115-
else:
116-
break
111+
whileld_header:=get_ld_header(p):
112+
ldr_headers.append((ld_header,get_ld_header_info(p)))
117113
p.stdout.close()
118114
p.wait()
119115
returnldr_headers

‎Lib/email/parser.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def parse(self, fp, headersonly=False):
4949
feedparser=FeedParser(self._class,policy=self.policy)
5050
ifheadersonly:
5151
feedparser._set_headersonly()
52-
whileTrue:
53-
data=fp.read(8192)
54-
ifnotdata:
55-
break
52+
whiledata:=fp.read(8192):
5653
feedparser.feed(data)
5754
returnfeedparser.close()
5855

‎Lib/ftplib.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
434434
"""
435435
self.voidcmd('TYPE I')
436436
withself.transfercmd(cmd,rest)asconn:
437-
while1:
438-
data=conn.recv(blocksize)
439-
ifnotdata:
440-
break
437+
whiledata:=conn.recv(blocksize):
441438
callback(data)
442439
# shutdown ssl layer
443440
if_SSLSocketisnotNoneandisinstance(conn,_SSLSocket):
@@ -496,10 +493,7 @@ def storbinary(self, cmd, fp, blocksize=8192, callback=None, rest=None):
496493
"""
497494
self.voidcmd('TYPE I')
498495
withself.transfercmd(cmd,rest)asconn:
499-
while1:
500-
buf=fp.read(blocksize)
501-
ifnotbuf:
502-
break
496+
whilebuf:=fp.read(blocksize):
503497
conn.sendall(buf)
504498
ifcallback:
505499
callback(buf)

‎Lib/http/client.py‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,7 @@ def _read_chunked(self, amt=None):
578578
assertself.chunked!=_UNKNOWN
579579
value= []
580580
try:
581-
whileTrue:
582-
chunk_left=self._get_chunk_left()
583-
ifchunk_leftisNone:
584-
break
585-
581+
while (chunk_left:=self._get_chunk_left())isnotNone:
586582
ifamtisnotNoneandamt<=chunk_left:
587583
value.append(self._safe_read(amt))
588584
self.chunk_left=chunk_left-amt
@@ -998,10 +994,7 @@ def send(self, data):
998994
encode=self._is_textIO(data)
999995
ifencodeandself.debuglevel>0:
1000996
print("encoding file using iso-8859-1")
1001-
while1:
1002-
datablock=data.read(self.blocksize)
1003-
ifnotdatablock:
1004-
break
997+
whiledatablock:=data.read(self.blocksize):
1005998
ifencode:
1006999
datablock=datablock.encode("iso-8859-1")
10071000
sys.audit("http.client.send",self,datablock)
@@ -1031,10 +1024,7 @@ def _read_readable(self, readable):
10311024
encode=self._is_textIO(readable)
10321025
ifencodeandself.debuglevel>0:
10331026
print("encoding file using iso-8859-1")
1034-
whileTrue:
1035-
datablock=readable.read(self.blocksize)
1036-
ifnotdatablock:
1037-
break
1027+
whiledatablock:=readable.read(self.blocksize):
10381028
ifencode:
10391029
datablock=datablock.encode("iso-8859-1")
10401030
yielddatablock

‎Lib/http/cookiejar.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,9 +1915,7 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
19151915
"comment","commenturl")
19161916

19171917
try:
1918-
while1:
1919-
line=f.readline()
1920-
ifline=="":break
1918+
while (line:=f.readline())!="":
19211919
ifnotline.startswith(header):
19221920
continue
19231921
line=line[len(header):].strip()
@@ -2017,12 +2015,9 @@ def _really_load(self, f, filename, ignore_discard, ignore_expires):
20172015
filename)
20182016

20192017
try:
2020-
while1:
2021-
line=f.readline()
2018+
while (line:=f.readline())!="":
20222019
rest= {}
20232020

2024-
ifline=="":break
2025-
20262021
# httponly is a cookie flag as defined in rfc6265
20272022
# when encoded in a netscape cookie file,
20282023
# the line is prepended with "#HttpOnly_"

‎Lib/mailbox.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,10 +1956,7 @@ def readlines(self, sizehint=None):
19561956

19571957
def__iter__(self):
19581958
"""Iterate over lines."""
1959-
whileTrue:
1960-
line=self.readline()
1961-
ifnotline:
1962-
return
1959+
whileline:=self.readline():
19631960
yieldline
19641961

19651962
deftell(self):

‎Lib/mailcap.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ def _readmailcapfile(fp, lineno):
9090
the viewing command is stored with the key "view".
9191
"""
9292
caps= {}
93-
while1:
94-
line=fp.readline()
95-
ifnotline:break
93+
whileline:=fp.readline():
9694
# Ignore comments and blank lines
9795
ifline[0]=='#'orline.strip()=='':
9896
continue

‎Lib/mimetypes.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,7 @@ def readfp(self, fp, strict=True):
217217
list of standard types, else to the list of non-standard
218218
types.
219219
"""
220-
while1:
221-
line=fp.readline()
222-
ifnotline:
223-
break
220+
whileline:=fp.readline():
224221
words=line.split()
225222
foriinrange(len(words)):
226223
ifwords[i][0]=='#':

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp