Skip to content

Commit fafd2da

Browse files
authored
[3.9] bpo-45975: Use walrus operator for some idlelib while loops (GH-31083)
co-authored by Nick Drozd cherrypicked from 51a95be
1 parent 3c6173c commit fafd2da

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

Lib/idlelib/pyparse.py

Lines changed: 4 additions & 8 deletions
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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ def replace_all(self, event=None):
156156
first = last = None
157157
# XXX ought to replace circular instead of top-to-bottom when wrapping
158158
text.undo_block_start()
159-
while True:
160-
res = self.engine.search_forward(text, prog, line, col,
161-
wrap=False, ok=ok)
162-
if not res:
163-
break
159+
while (res := self.engine.search_forward(
160+
text, prog, line, col, wrap=False, ok=ok)):
164161
line, m = res
165162
chars = text.get("%d.0" % line, "%d.0" % (line+1))
166163
orig = m.group()

Lib/idlelib/run.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,7 @@ def read(self, size=-1):
468468
result = self._line_buffer
469469
self._line_buffer = ''
470470
if size < 0:
471-
while True:
472-
line = self.shell.readline()
473-
if not line: break
471+
while (line := self.shell.readline()):
474472
result += line
475473
else:
476474
while len(result) < size:

0 commit comments

Comments
 (0)