Skip to content

Commit 51a95be

Browse files
authored
bpo-45975: Use walrus operator for some idlelib while loops (GH-31083)
1 parent 164a017 commit 51a95be

File tree

5 files changed

+9
-24
lines changed

5 files changed

+9
-24
lines changed

Lib/idlelib/idle_test/test_sidebar.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,7 @@ def get_shell_line_y_coords(self):
474474
index = text.index("@0,0")
475475
if index.split('.', 1)[1] != '0':
476476
index = text.index(f"{index} +1line linestart")
477-
while True:
478-
lineinfo = text.dlineinfo(index)
479-
if lineinfo is None:
480-
break
477+
while (lineinfo := text.dlineinfo(index)) is not None:
481478
y_coords.append(lineinfo[1])
482479
index = text.index(f"{index} +1line")
483480
return y_coords

Lib/idlelib/pyparse.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,10 @@ def find_good_parse_start(self, is_char_in_string):
179179
# Peeking back worked; look forward until _synchre no longer
180180
# matches.
181181
i = pos + 1
182-
while 1:
183-
m = _synchre(code, i)
184-
if m:
185-
s, i = m.span()
186-
if not is_char_in_string(s):
187-
pos = s
188-
else:
189-
break
182+
while (m := _synchre(code, i)):
183+
s, i = m.span()
184+
if not is_char_in_string(s):
185+
pos = s
190186
return pos
191187

192188
def set_lo(self, lo):

Lib/idlelib/replace.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,8 @@ def replace_all(self, event=None):
158158
first = last = None
159159
# XXX ought to replace circular instead of top-to-bottom when wrapping
160160
text.undo_block_start()
161-
while True:
162-
res = self.engine.search_forward(text, prog, line, col,
163-
wrap=False, ok=ok)
164-
if not res:
165-
break
161+
while (res := self.engine.search_forward(
162+
text, prog, line, col, wrap=False, ok=ok)):
166163
line, m = res
167164
chars = text.get("%d.0" % line, "%d.0" % (line+1))
168165
orig = m.group()

Lib/idlelib/run.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,7 @@ def read(self, size=-1):
482482
result = self._line_buffer
483483
self._line_buffer = ''
484484
if size < 0:
485-
while True:
486-
line = self.shell.readline()
487-
if not line: break
485+
while (line := self.shell.readline()):
488486
result += line
489487
else:
490488
while len(result) < size:

Lib/idlelib/sidebar.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ def update_sidebar(self):
471471
index = text.index("@0,0")
472472
if index.split('.', 1)[1] != '0':
473473
index = text.index(f'{index}+1line linestart')
474-
while True:
475-
lineinfo = text.dlineinfo(index)
476-
if lineinfo is None:
477-
break
474+
while (lineinfo := text.dlineinfo(index)) is not None:
478475
y = lineinfo[1]
479476
prev_newline_tagnames = text_tagnames(f"{index} linestart -1c")
480477
prompt = (

0 commit comments

Comments
 (0)