From d847324dee403650cc7159356c95822a0bc73433 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 03:17:29 +0200 Subject: [PATCH 1/6] WIP: Use assign expr in list comprehension Modify for loops and list comprehensions to use assignment expressions. --- Lib/mailbox.py | 6 +++--- Lib/nntplib.py | 8 +++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Lib/mailbox.py b/Lib/mailbox.py index 056251dce0ada3..dc61a3a8b6ee95 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -1341,9 +1341,9 @@ def _generate_toc(self): if len(stops) < len(starts): stops.append(line_pos - len(linesep)) starts.append(next_pos) - labels = [label.strip() for label - in self._file.readline()[1:].split(b',') - if label.strip()] + labels = [slabel for label + in self._file.readline()[1:].split(b',') + if (slabel := label.strip())] label_lists.append(labels) elif line == b'\037' or line == b'\037' + linesep: if len(stops) < len(starts): diff --git a/Lib/nntplib.py b/Lib/nntplib.py index 5961a28ab7d9bc..a5d13e35be0e6e 100644 --- a/Lib/nntplib.py +++ b/Lib/nntplib.py @@ -843,11 +843,9 @@ def xgtitle(self, group, *, file=None): DeprecationWarning, 2) line_pat = re.compile('^([^ \t]+)[ \t]+(.*)$') resp, raw_lines = self._longcmdstring('XGTITLE ' + group, file) - lines = [] - for raw_line in raw_lines: - match = line_pat.search(raw_line.strip()) - if match: - lines.append(match.group(1, 2)) + lines = [match.group(1, 2) + for raw_line in raw_lines + if (match := line_pat.search(raw_line.strip()))] return resp, lines def xpath(self, id): From 8e015648443536f3c9791ae3af10d64655a65dc6 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 15:31:41 +0200 Subject: [PATCH 2/6] WIP: Use assign expr with "elif" Replace: else: var = expr if var: ... with: elif (var := expr): ... --- Lib/copy.py | 17 ++++++----------- Lib/zipfile.py | 8 +++----- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Lib/copy.py b/Lib/copy.py index f86040a33c5547..1775f68c425ca0 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -87,19 +87,14 @@ def copy(x): if copier: return copier(x) - reductor = dispatch_table.get(cls) - if reductor: + if (reductor := dispatch_table.get(cls)): rv = reductor(x) + elif (reductor := getattr(x, "__reduce_ex__", None)): + rv = reductor(4) + elif (reductor := getattr(x, "__reduce__", None)): + rv = reductor() else: - reductor = getattr(x, "__reduce_ex__", None) - if reductor: - rv = reductor(4) - else: - reductor = getattr(x, "__reduce__", None) - if reductor: - rv = reductor() - else: - raise Error("un(shallow)copyable object of type %s" % cls) + raise Error("un(shallow)copyable object of type %s" % cls) if isinstance(rv, str): return x diff --git a/Lib/zipfile.py b/Lib/zipfile.py index b90b60f72e2bcd..9c265b297f71dd 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -681,12 +681,10 @@ def _get_decompressor(compress_type): return bz2.BZ2Decompressor() elif compress_type == ZIP_LZMA: return LZMADecompressor() + elif descr := compressor_names.get(compress_type): + raise NotImplementedError("compression type %d (%s)" % (compress_type, descr)) else: - descr = compressor_names.get(compress_type) - if descr: - raise NotImplementedError("compression type %d (%s)" % (compress_type, descr)) - else: - raise NotImplementedError("compression type %d" % (compress_type,)) + raise NotImplementedError("compression type %d" % (compress_type,)) class _SharedFile: From b51e0a5b27bb53bb54a530f5305b99086b882bb7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 16:55:37 +0200 Subject: [PATCH 3/6] WIP: use assign expr on if Replace: var = expr if var: ... with: if (var := expr): ... Don't replace when: * var is used after the if * Code like "var = regex.match(); if var: return var.group(1)" is left unchanged since it's covered by a dedicated PR. Sometimes, var is only used in the condition and so has been removed in this change. Maybe such kind of pattern should be addressed in a different pull request. This patch is restricted to the simplest test "if var:", other conditions like "if var > 0:" are left unchaned to keep this change reviewable (short enough). --- Lib/_markupbase.py | 12 ++-- Lib/_pydecimal.py | 112 +++++++++++---------------------- Lib/_pyio.py | 3 +- Lib/asynchat.py | 28 ++++----- Lib/asyncio/format_helpers.py | 3 +- Lib/asyncio/selector_events.py | 12 ++-- Lib/asyncio/sslproto.py | 3 +- Lib/asyncio/unix_events.py | 10 ++- Lib/bdb.py | 6 +- Lib/binhex.py | 3 +- Lib/codecs.py | 21 +++---- Lib/copy.py | 31 ++++----- Lib/copyreg.py | 3 +- Lib/csv.py | 6 +- Lib/dataclasses.py | 14 ++--- Lib/datetime.py | 21 +++---- Lib/difflib.py | 3 +- Lib/doctest.py | 16 ++--- Lib/fileinput.py | 3 +- Lib/fractions.py | 9 +-- Lib/getpass.py | 6 +- Lib/gettext.py | 3 +- Lib/gzip.py | 3 +- Lib/imaplib.py | 6 +- Lib/imghdr.py | 3 +- Lib/inspect.py | 12 ++-- Lib/keyword.py | 3 +- Lib/locale.py | 6 +- Lib/logging/__init__.py | 6 +- Lib/logging/config.py | 25 +++----- Lib/logging/handlers.py | 6 +- Lib/mailcap.py | 3 +- Lib/modulefinder.py | 12 ++-- Lib/multiprocessing/queues.py | 3 +- Lib/nntplib.py | 3 +- Lib/pdb.py | 12 ++-- Lib/pickletools.py | 3 +- Lib/platform.py | 3 +- Lib/pprint.py | 3 +- Lib/py_compile.py | 3 +- Lib/pydoc.py | 3 +- Lib/selectors.py | 12 ++-- Lib/site.py | 3 +- Lib/smtpd.py | 3 +- Lib/smtplib.py | 3 +- Lib/sndhdr.py | 6 +- Lib/socket.py | 6 +- Lib/socketserver.py | 9 +-- Lib/sre_parse.py | 12 ++-- Lib/sunau.py | 3 +- Lib/sysconfig.py | 15 ++--- Lib/telnetlib.py | 3 +- Lib/tempfile.py | 3 +- Lib/token.py | 3 +- Lib/tokenize.py | 15 ++--- Lib/trace.py | 6 +- Lib/tracemalloc.py | 3 +- Lib/uuid.py | 6 +- Lib/wave.py | 6 +- Lib/weakref.py | 3 +- Lib/webbrowser.py | 3 +- Lib/zipfile.py | 8 +-- 62 files changed, 200 insertions(+), 377 deletions(-) diff --git a/Lib/_markupbase.py b/Lib/_markupbase.py index 2af5f1c23b6066..ea886baaecd0da 100644 --- a/Lib/_markupbase.py +++ b/Lib/_markupbase.py @@ -49,8 +49,7 @@ def updatepos(self, i, j): if i >= j: return j rawdata = self.rawdata - nlines = rawdata.count("\n", i, j) - if nlines: + if (nlines := rawdata.count("\n", i, j)): self.lineno = self.lineno + nlines pos = rawdata.rindex("\n", i, j) # Should not fail self.offset = j-(pos+1) @@ -291,8 +290,7 @@ def _parse_doctype_attlist(self, i, declstartpos): if not c: return -1 if c in "'\"": - m = _declstringlit_match(rawdata, j) - if m: + if (m := _declstringlit_match(rawdata, j)): j = m.end() else: return -1 @@ -359,8 +357,7 @@ def _parse_doctype_entity(self, i, declstartpos): if not c: return -1 if c in "'\"": - m = _declstringlit_match(rawdata, j) - if m: + if (m := _declstringlit_match(rawdata, j)): j = m.end() else: return -1 # incomplete @@ -378,8 +375,7 @@ def _scan_name(self, i, declstartpos): n = len(rawdata) if i == n: return None, -1 - m = _declname_match(rawdata, i) - if m: + if (m := _declname_match(rawdata, i)): s = m.group() name = s.strip() if (i + len(s)) == n: diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 359690003fe160..5a47f2cd412fff 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -886,8 +886,7 @@ def __lt__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other - ans = self._compare_check_nans(other, context) - if ans: + if self._compare_check_nans(other, context): return False return self._cmp(other) < 0 @@ -895,8 +894,7 @@ def __le__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other - ans = self._compare_check_nans(other, context) - if ans: + if self._compare_check_nans(other, context): return False return self._cmp(other) <= 0 @@ -904,8 +902,7 @@ def __gt__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other - ans = self._compare_check_nans(other, context) - if ans: + if self._compare_check_nans(other, context): return False return self._cmp(other) > 0 @@ -913,8 +910,7 @@ def __ge__(self, other, context=None): self, other = _convert_for_comparison(self, other) if other is NotImplemented: return other - ans = self._compare_check_nans(other, context) - if ans: + if self._compare_check_nans(other, context): return False return self._cmp(other) >= 0 @@ -930,8 +926,7 @@ def compare(self, other, context=None): # Compare(NaN, NaN) = NaN if (self._is_special or other and other._is_special): - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans return Decimal(self._cmp(other)) @@ -1090,10 +1085,8 @@ def __neg__(self, context=None): Rounds, if it has reason. """ - if self._is_special: - ans = self._check_nans(context=context) - if ans: - return ans + if self._is_special and (ans := self._check_nans(context=context)): + return ans if context is None: context = getcontext() @@ -1112,10 +1105,8 @@ def __pos__(self, context=None): Rounds the number (if more than precision digits) """ - if self._is_special: - ans = self._check_nans(context=context) - if ans: - return ans + if self._is_special and (ans := self._check_nans(context=context)): + return ans if context is None: context = getcontext() @@ -1138,10 +1129,8 @@ def __abs__(self, round=True, context=None): if not round: return self.copy_abs() - if self._is_special: - ans = self._check_nans(context=context) - if ans: - return ans + if self._is_special and (ans := self._check_nans(context=context)): + return ans if self._sign: ans = self.__neg__(context=context) @@ -1163,8 +1152,7 @@ def __add__(self, other, context=None): context = getcontext() if self._is_special or other._is_special: - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if self._isinfinity(): @@ -1244,10 +1232,9 @@ def __sub__(self, other, context=None): if other is NotImplemented: return other - if self._is_special or other._is_special: - ans = self._check_nans(other, context=context) - if ans: - return ans + if (self._is_special or other._is_special + and (ans := self._check_nans(other, context=context))): + return ans # self - other is computed as self + other.copy_negate() return self.__add__(other.copy_negate(), context=context) @@ -1275,8 +1262,7 @@ def __mul__(self, other, context=None): resultsign = self._sign ^ other._sign if self._is_special or other._is_special: - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if self._isinfinity(): @@ -1329,8 +1315,7 @@ def __truediv__(self, other, context=None): sign = self._sign ^ other._sign if self._is_special or other._is_special: - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if self._isinfinity() and other._isinfinity(): @@ -1427,8 +1412,7 @@ def __divmod__(self, other, context=None): if context is None: context = getcontext() - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return (ans, ans) sign = self._sign ^ other._sign @@ -1470,8 +1454,7 @@ def __mod__(self, other, context=None): if context is None: context = getcontext() - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if self._isinfinity(): @@ -1502,8 +1485,7 @@ def remainder_near(self, other, context=None): other = _convert_other(other, raiseit=True) - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans # self == +/-infinity -> InvalidOperation @@ -1577,8 +1559,7 @@ def __floordiv__(self, other, context=None): if context is None: context = getcontext() - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if self._isinfinity(): @@ -2316,8 +2297,7 @@ def __pow__(self, other, modulo=None, context=None): context = getcontext() # either argument is a NaN => result is NaN - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans # 0**0 = NaN (!), x**0 = 1 for nonzero x (including +/-Infinity) @@ -2511,8 +2491,7 @@ def normalize(self, context=None): context = getcontext() if self._is_special: - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans dup = self._fix(context) @@ -2542,8 +2521,7 @@ def quantize(self, exp, rounding=None, context=None): rounding = context.rounding if self._is_special or exp._is_special: - ans = self._check_nans(exp, context) - if ans: + if (ans := self._check_nans(exp, context)): return ans if exp._isinfinity() or self._isinfinity(): @@ -2673,8 +2651,7 @@ def to_integral_exact(self, rounding=None, context=None): this method except that it doesn't raise Inexact or Rounded. """ if self._is_special: - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans return Decimal(self) if self._exp >= 0: @@ -2698,8 +2675,7 @@ def to_integral_value(self, rounding=None, context=None): if rounding is None: rounding = context.rounding if self._is_special: - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans return Decimal(self) if self._exp >= 0: @@ -2716,8 +2692,7 @@ def sqrt(self, context=None): context = getcontext() if self._is_special: - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans if self._isinfinity() and self._sign == 0: @@ -2923,8 +2898,7 @@ def compare_signal(self, other, context=None): NaNs taking precedence over quiet NaNs. """ other = _convert_other(other, raiseit = True) - ans = self._compare_check_nans(other, context) - if ans: + if (ans := self._compare_check_nans(other, context)): return ans return self.compare(other, context=context) @@ -3036,8 +3010,7 @@ def exp(self, context=None): context = getcontext() # exp(NaN) = NaN - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans # exp(-Infinity) = 0 @@ -3192,8 +3165,7 @@ def ln(self, context=None): context = getcontext() # ln(NaN) = NaN - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans # ln(0.0) == -Infinity @@ -3272,8 +3244,7 @@ def log10(self, context=None): context = getcontext() # log10(NaN) = NaN - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans # log10(0.0) == -Infinity @@ -3325,8 +3296,7 @@ def logb(self, context=None): without limiting the resulting exponent). """ # logb(NaN) = NaN - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans if context is None: @@ -3496,8 +3466,7 @@ def next_minus(self, context=None): if context is None: context = getcontext() - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans if self._isinfinity() == -1: @@ -3519,8 +3488,7 @@ def next_plus(self, context=None): if context is None: context = getcontext() - ans = self._check_nans(context=context) - if ans: + if (ans := self._check_nans(context=context)): return ans if self._isinfinity() == 1: @@ -3551,8 +3519,7 @@ def next_toward(self, other, context=None): if context is None: context = getcontext() - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans comparison = self._cmp(other) @@ -3636,8 +3603,7 @@ def rotate(self, other, context=None): other = _convert_other(other, raiseit=True) - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if other._exp != 0: @@ -3669,8 +3635,7 @@ def scaleb(self, other, context=None): other = _convert_other(other, raiseit=True) - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if other._exp != 0: @@ -3694,8 +3659,7 @@ def shift(self, other, context=None): other = _convert_other(other, raiseit=True) - ans = self._check_nans(other, context) - if ans: + if (ans := self._check_nans(other, context)): return ans if other._exp != 0: diff --git a/Lib/_pyio.py b/Lib/_pyio.py index f0d4f4ed27a243..a3162015440cf1 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1080,8 +1080,7 @@ def _peek_unlocked(self, n=0): have = len(self._read_buf) - self._read_pos if have < want or have <= 0: to_read = self.buffer_size - have - current = self.raw.read(to_read) - if current: + if (current := self.raw.read(to_read)): self._read_buf = self._read_buf[self._read_pos:] + current self._read_pos = 0 return self._read_buf[self._read_pos:] diff --git a/Lib/asynchat.py b/Lib/asynchat.py index fc1146adbb10dc..686a0d12714300 100644 --- a/Lib/asynchat.py +++ b/Lib/asynchat.py @@ -158,8 +158,7 @@ def handle_read(self): # 3) end of buffer does not match any prefix: # collect data terminator_len = len(terminator) - index = self.ac_in_buffer.find(terminator) - if index != -1: + if (index := self.ac_in_buffer.find(terminator)) != -1: # we found the terminator if index > 0: # don't bother reporting the empty string @@ -169,19 +168,17 @@ def handle_read(self): # This does the Right Thing if the terminator # is changed here. self.found_terminator() + # check for a prefix of the terminator + elif (index := find_prefix_at_end(self.ac_in_buffer, terminator)): + if index != lb: + # we found a prefix, collect up to the prefix + self.collect_incoming_data(self.ac_in_buffer[:-index]) + self.ac_in_buffer = self.ac_in_buffer[-index:] + break else: - # check for a prefix of the terminator - index = find_prefix_at_end(self.ac_in_buffer, terminator) - if index: - if index != lb: - # we found a prefix, collect up to the prefix - self.collect_incoming_data(self.ac_in_buffer[:-index]) - self.ac_in_buffer = self.ac_in_buffer[-index:] - break - else: - # no prefix, collect it all - self.collect_incoming_data(self.ac_in_buffer) - self.ac_in_buffer = b'' + # no prefix, collect it all + self.collect_incoming_data(self.ac_in_buffer) + self.ac_in_buffer = b'' def handle_write(self): self.initiate_send() @@ -236,8 +233,7 @@ def initiate_send(self): try: data = first[:obs] except TypeError: - data = first.more() - if data: + if (data := first.more()): self.producer_fifo.appendleft(data) else: del self.producer_fifo[0] diff --git a/Lib/asyncio/format_helpers.py b/Lib/asyncio/format_helpers.py index 27d11fd4fa9553..a00344c339b2ea 100644 --- a/Lib/asyncio/format_helpers.py +++ b/Lib/asyncio/format_helpers.py @@ -21,8 +21,7 @@ def _get_function_source(func): def _format_callback_source(func, args): func_repr = _format_callback(func, args, None) - source = _get_function_source(func) - if source: + if (source := _get_function_source(func)): func_repr += f' at {source[0]}:{source[1]}' return func_repr diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py index 116c08d6ff7fdd..6ceccb680210b4 100644 --- a/Lib/asyncio/selector_events.py +++ b/Lib/asyncio/selector_events.py @@ -618,17 +618,15 @@ def __repr__(self): info.append(f'fd={self._sock_fd}') # test if the transport was closed if self._loop is not None and not self._loop.is_closed(): - polling = _test_selector_event(self._loop._selector, - self._sock_fd, selectors.EVENT_READ) - if polling: + if _test_selector_event(self._loop._selector, + self._sock_fd, selectors.EVENT_READ): info.append('read=polling') else: info.append('read=idle') - polling = _test_selector_event(self._loop._selector, - self._sock_fd, - selectors.EVENT_WRITE) - if polling: + if _test_selector_event(self._loop._selector, + self._sock_fd, + selectors.EVENT_WRITE): state = 'polling' else: state = 'idle' diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py index 12fdb0d1c5ec10..a7804e017ff9cc 100644 --- a/Lib/asyncio/sslproto.py +++ b/Lib/asyncio/sslproto.py @@ -562,8 +562,7 @@ def eof_received(self): self._wakeup_waiter(ConnectionResetError) if not self._in_handshake: - keep_open = self._app_protocol.eof_received() - if keep_open: + if self._app_protocol.eof_received(): logger.warning('returning true from eof_received() ' 'has no effect when using ssl') finally: diff --git a/Lib/asyncio/unix_events.py b/Lib/asyncio/unix_events.py index 7cad7e3637a11f..619a7f6fbff904 100644 --- a/Lib/asyncio/unix_events.py +++ b/Lib/asyncio/unix_events.py @@ -461,9 +461,8 @@ def __repr__(self): info.append(f'fd={self._fileno}') selector = getattr(self._loop, '_selector', None) if self._pipe is not None and selector is not None: - polling = selector_events._test_selector_event( - selector, self._fileno, selectors.EVENT_READ) - if polling: + if selector_events._test_selector_event(selector, self._fileno, + selectors.EVENT_READ): info.append('polling') else: info.append('idle') @@ -594,9 +593,8 @@ def __repr__(self): info.append(f'fd={self._fileno}') selector = getattr(self._loop, '_selector', None) if self._pipe is not None and selector is not None: - polling = selector_events._test_selector_event( - selector, self._fileno, selectors.EVENT_WRITE) - if polling: + if selector_events._test_selector_event(selector, self._fileno, + selectors.EVENT_WRITE): info.append('polling') else: info.append('idle') diff --git a/Lib/bdb.py b/Lib/bdb.py index 880ff5daf9953d..4ff7052c7909c8 100644 --- a/Lib/bdb.py +++ b/Lib/bdb.py @@ -558,8 +558,7 @@ def format_stack_entry(self, frame_lineno, lprefix=': '): rv = frame.f_locals['__return__'] s += '->' s += reprlib.repr(rv) - line = linecache.getline(filename, lineno, frame.f_globals) - if line: + if (line := linecache.getline(filename, lineno, frame.f_globals)): s += lprefix + line.strip() return s @@ -818,8 +817,7 @@ def effective(file, line, frame): # Ignore count applies only to those bpt hits where the # condition evaluates to true. try: - val = eval(b.cond, frame.f_globals, frame.f_locals) - if val: + if eval(b.cond, frame.f_globals, frame.f_locals): if b.ignore > 0: b.ignore -= 1 # continue diff --git a/Lib/binhex.py b/Lib/binhex.py index 56b5f852c0038a..9db06270aec257 100644 --- a/Lib/binhex.py +++ b/Lib/binhex.py @@ -466,8 +466,7 @@ def hexbin(inp, out): ofp.write(d) ifp.close_data() - d = ifp.read_rsrc(128000) - if d: + if (d := ifp.read_rsrc(128000)): ofp = openrsrc(out, 'wb') ofp.write(d) while True: diff --git a/Lib/codecs.py b/Lib/codecs.py index a70ed20f2bc794..13b67de1869917 100644 --- a/Lib/codecs.py +++ b/Lib/codecs.py @@ -565,8 +565,7 @@ def readline(self, size=None, keepends=True): data += self.read(size=1, chars=1) line += data - lines = line.splitlines(keepends=True) - if lines: + if (lines := line.splitlines(keepends=True)): if len(lines) > 1: # More than one line result; the first line is a full line # to return @@ -642,8 +641,7 @@ def seek(self, offset, whence=0): def __next__(self): """ Return the next decoded line from the input stream.""" - line = self.readline() - if line: + if (line := self.readline()): return line raise StopIteration @@ -1021,11 +1019,9 @@ def iterencode(iterator, encoding, errors='strict', **kwargs): """ encoder = getincrementalencoder(encoding)(errors, **kwargs) for input in iterator: - output = encoder.encode(input) - if output: + if (output := encoder.encode(input)): yield output - output = encoder.encode("", True) - if output: + if (output := encoder.encode("", True)): yield output def iterdecode(iterator, encoding, errors='strict', **kwargs): @@ -1039,11 +1035,9 @@ def iterdecode(iterator, encoding, errors='strict', **kwargs): """ decoder = getincrementaldecoder(encoding)(errors, **kwargs) for input in iterator: - output = decoder.decode(input) - if output: + if (output := decoder.decode(input)): yield output - output = decoder.decode(b"", True) - if output: + if (output := decoder.decode(b"", True)): yield output ### Helpers for charmap-based codecs @@ -1099,8 +1093,7 @@ def make_encoding_map(decoding_map): # Tell modulefinder that using codecs probably needs the encodings # package -_false = 0 -if _false: +if (_false := 0): import encodings ### Tests diff --git a/Lib/copy.py b/Lib/copy.py index 1775f68c425ca0..7a46a2cbff25a7 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -71,8 +71,7 @@ def copy(x): cls = type(x) - copier = _copy_dispatch.get(cls) - if copier: + if (copier := _copy_dispatch.get(cls)): return copier(x) try: @@ -83,8 +82,7 @@ def copy(x): # treat it as a regular class: return _copy_immutable(x) - copier = getattr(cls, "__copy__", None) - if copier: + if (copier := getattr(cls, "__copy__", None)): return copier(x) if (reductor := dispatch_table.get(cls)): @@ -140,8 +138,7 @@ def deepcopy(x, memo=None, _nil=[]): cls = type(x) - copier = _deepcopy_dispatch.get(cls) - if copier: + if (copier := _deepcopy_dispatch.get(cls)): y = copier(x, memo) else: try: @@ -151,24 +148,18 @@ def deepcopy(x, memo=None, _nil=[]): if issc: y = _deepcopy_atomic(x, memo) else: - copier = getattr(x, "__deepcopy__", None) - if copier: + if (copier := getattr(x, "__deepcopy__", None)): y = copier(memo) else: - reductor = dispatch_table.get(cls) - if reductor: + if (reductor := dispatch_table.get(cls)): rv = reductor(x) + elif (reductor := getattr(x, "__reduce_ex__", None)): + rv = reductor(4) + elif (reductor := getattr(x, "__reduce__", None)): + rv = reductor() else: - reductor = getattr(x, "__reduce_ex__", None) - if reductor: - rv = reductor(4) - else: - reductor = getattr(x, "__reduce__", None) - if reductor: - rv = reductor() - else: - raise Error( - "un(deep)copyable object of type %s" % cls) + raise Error("un(deep)copyable object of type %s" % cls) + if isinstance(rv, str): y = x else: diff --git a/Lib/copyreg.py b/Lib/copyreg.py index bbe1af4e2e7e71..77c0e7fbc2363e 100644 --- a/Lib/copyreg.py +++ b/Lib/copyreg.py @@ -128,8 +128,7 @@ class found there. (This assumes classes don't modify their continue # mangled names elif name.startswith('__') and not name.endswith('__'): - stripped = c.__name__.lstrip('_') - if stripped: + if (stripped := c.__name__.lstrip('_')): names.append('_%s%s' % (stripped, name)) else: names.append(name) diff --git a/Lib/csv.py b/Lib/csv.py index 58624af9053493..2043afe376b557 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -220,8 +220,7 @@ def _guess_quote_and_delimiter(self, data, delimiters): r'(?P[^\w\n"\'])(?P ?)(?P["\']).*?(?P=quote)(?:$|\n)', # ,".*?" r'(?:^|\n)(?P["\']).*?(?P=quote)(?:$|\n)'): # ".*?" (no delim, no space) regexp = re.compile(restr, re.DOTALL | re.MULTILINE) - matches = regexp.findall(data) - if matches: + if (matches := regexp.findall(data)): break if not matches: @@ -233,8 +232,7 @@ def _guess_quote_and_delimiter(self, data, delimiters): groupindex = regexp.groupindex for m in matches: n = groupindex['quote'] - 1 - key = m[n] - if key: + if (key := m[n]): quotes[key] = quotes.get(key, 0) + 1 try: n = groupindex['delim'] - 1 diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index ad7bf0f7593a13..3fac6f19ab691a 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -599,8 +599,7 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate): # a eval() penalty for every single field of every dataclass # that's defined. It was judged not worth it. - match = _MODULE_IDENTIFIER_RE.match(annotation) - if match: + if (match := _MODULE_IDENTIFIER_RE.match(annotation)): ns = None module_name = match.group(1) if not module_name: @@ -656,8 +655,7 @@ def _get_field(cls, a_name, a_type): # annotation to be a ClassVar. So, only look for ClassVar if # typing has been imported by any module (not necessarily cls's # module). - typing = sys.modules.get('typing') - if typing: + if (typing := sys.modules.get('typing')): if (_is_classvar(a_type, typing) or (isinstance(f.type, str) and _is_type(f.type, cls, typing, typing.ClassVar, @@ -774,8 +772,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen): for b in cls.__mro__[-1:0:-1]: # Only process classes that have been processed by our # decorator. That is, they have a _FIELDS attribute. - base_fields = getattr(b, _FIELDS, None) - if base_fields: + if (base_fields := getattr(b, _FIELDS, None)): has_dataclass_bases = True for f in base_fields.values(): fields[f.name] = f @@ -914,11 +911,10 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen): f'in class {cls.__name__}') # Decide if/how we're going to create a hash function. - hash_action = _hash_action[bool(unsafe_hash), + if (hash_action := _hash_action[bool(unsafe_hash), bool(eq), bool(frozen), - has_explicit_hash] - if hash_action: + has_explicit_hash]): # No need to call _set_new_attribute here, since by the time # we're here the overwriting is unconditional. cls.__hash__ = hash_action(cls, field_list) diff --git a/Lib/datetime.py b/Lib/datetime.py index 5e922c80b017ff..d4e007fecad578 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -227,8 +227,7 @@ def _wrap_strftime(object, format, timetuple): h, rest = divmod(offset, timedelta(hours=1)) m, rest = divmod(rest, timedelta(minutes=1)) s = rest.seconds - u = offset.microseconds - if u: + if (u := offset.microseconds): zreplace = '%c%02d%02d%02d.%06d' % (sign, h, m, s, u) elif s: zreplace = '%c%02d%02d%02d' % (sign, h, m, s) @@ -1131,13 +1130,11 @@ def fromutc(self, dt): # Pickle support. def __reduce__(self): - getinitargs = getattr(self, "__getinitargs__", None) - if getinitargs: + if (getinitargs := getattr(self, "__getinitargs__", None)): args = getinitargs() else: args = () - getstate = getattr(self, "__getstate__", None) - if getstate: + if (getstate := getattr(self, "__getstate__", None)): state = getstate() else: state = getattr(self, "__dict__", None) or None @@ -1352,8 +1349,7 @@ def isoformat(self, timespec='auto'): """ s = _format_time(self._hour, self._minute, self._second, self._microsecond, timespec) - tz = self._tzstr() - if tz: + if (tz := self._tzstr()): s += tz return s @@ -1707,8 +1703,7 @@ def timestamp(self): def utctimetuple(self): "Return UTC time tuple compatible with time.gmtime()." - offset = self.utcoffset() - if offset: + if (offset := self.utcoffset()): self -= offset y, m, d = self.year, self.month, self.day hh, mm, ss = self.hour, self.minute, self.second @@ -1821,8 +1816,7 @@ def isoformat(self, sep='T', timespec='auto'): self._microsecond, timespec)) off = self.utcoffset() - tz = _format_offset(off) - if tz: + if (tz := _format_offset(off)): s += tz return s @@ -2195,8 +2189,7 @@ def _name_from_offset(delta): hours, rest = divmod(delta, timedelta(hours=1)) minutes, rest = divmod(rest, timedelta(minutes=1)) seconds = rest.seconds - microseconds = rest.microseconds - if microseconds: + if (microseconds := rest.microseconds): return (f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}' f'.{microseconds:06d}') if seconds: diff --git a/Lib/difflib.py b/Lib/difflib.py index 887c3c26cae458..7ffa4630dd974d 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -314,8 +314,7 @@ def __chain_b(self): # Purge junk elements self.bjunk = junk = set() - isjunk = self.isjunk - if isjunk: + if (isjunk := self.isjunk): for elt in b2j.keys(): if isjunk(elt): junk.add(elt) diff --git a/Lib/doctest.py b/Lib/doctest.py index c1d8a1db111ddd..77351b788a8368 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -275,15 +275,14 @@ def _ellipsis_match(want, got): # Deal with exact matches possibly needed at one or both ends. startpos, endpos = 0, len(got) - w = ws[0] - if w: # starts with exact match + if (w := ws[0]): # starts with exact match if got.startswith(w): startpos = len(w) del ws[0] else: return False - w = ws[-1] - if w: # ends with exact match + + if (w := ws[-1]): # ends with exact match if got.endswith(w): endpos -= len(w) del ws[-1] @@ -311,8 +310,7 @@ def _ellipsis_match(want, got): def _comment_line(line): "Return a commented form of the given line" - line = line.rstrip() - if line: + if (line := line.rstrip()): return '# '+line else: return '#' @@ -715,8 +713,7 @@ def _parse_example(self, m, name, lineno): want = '\n'.join([wl[indent:] for wl in want_lines]) # If `want` contains a traceback message, then extract it. - m = self._EXCEPTION_RE.match(want) - if m: + if (m := self._EXCEPTION_RE.match(want)): exc_msg = m.group('msg') else: exc_msg = None @@ -2571,8 +2568,7 @@ def script_from_examples(s): # Add the example's source code (strip trailing NL) output.append(piece.source[:-1]) # Add the expected output: - want = piece.want - if want: + if (want := piece.want): output.append('# Expected:') output += ['## '+l for l in want.split('\n')[:-1]] else: diff --git a/Lib/fileinput.py b/Lib/fileinput.py index c6fc9a1981a1fa..34d0b14fc1b89b 100644 --- a/Lib/fileinput.py +++ b/Lib/fileinput.py @@ -249,8 +249,7 @@ def __iter__(self): def __next__(self): while True: - line = self._readline() - if line: + if (line := self._readline()): self._filelineno += 1 return line if not self._file: diff --git a/Lib/fractions.py b/Lib/fractions.py index 8330202d7037b3..7a5d957a3713bd 100644 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -137,18 +137,15 @@ def __new__(cls, numerator=0, denominator=None, *, _normalize=True): raise ValueError('Invalid literal for Fraction: %r' % numerator) numerator = int(m.group('num') or '0') - denom = m.group('denom') - if denom: + if (denom := m.group('denom')): denominator = int(denom) else: denominator = 1 - decimal = m.group('decimal') - if decimal: + if (decimal := m.group('decimal')): scale = 10**len(decimal) numerator = numerator * scale + int(decimal) denominator *= scale - exp = m.group('exp') - if exp: + if (exp := m.group('exp')): exp = int(exp) if exp >= 0: numerator *= 10**exp diff --git a/Lib/getpass.py b/Lib/getpass.py index 36e17e4cb6965d..fd616bec5f09f9 100644 --- a/Lib/getpass.py +++ b/Lib/getpass.py @@ -132,8 +132,7 @@ def _raw_input(prompt="", stream=None, input=None): stream = sys.stderr if not input: input = sys.stdin - prompt = str(prompt) - if prompt: + if (prompt := str(prompt)): try: stream.write(prompt) except UnicodeEncodeError: @@ -160,8 +159,7 @@ def getuser(): """ for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'): - user = os.environ.get(name) - if user: + if (user := os.environ.get(name)): return user # If this fails, the exception will "explain" why diff --git a/Lib/gettext.py b/Lib/gettext.py index 4c3b80b0239b0a..ffd691bbfd8610 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -478,8 +478,7 @@ def find(domain, localedir=None, languages=None, all=False): if languages is None: languages = [] for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): - val = os.environ.get(envar) - if val: + if (val := os.environ.get(envar)): languages = val.split(':') break if 'C' not in languages: diff --git a/Lib/gzip.py b/Lib/gzip.py index ddc7bda1fecbbd..9d145014abf078 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -313,8 +313,7 @@ def close(self): elif self.mode == READ: self._buffer.close() finally: - myfileobj = self.myfileobj - if myfileobj: + if (myfileobj := self.myfileobj): self.myfileobj = None myfileobj.close() diff --git a/Lib/imaplib.py b/Lib/imaplib.py index e1cece0b283f2b..87095efc45fefc 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -924,8 +924,7 @@ def _append_untagged(self, typ, dat): def _check_bye(self): - bye = self.untagged_responses.get('BYE') - if bye: + if (bye := self.untagged_responses.get('BYE')): raise self.abort(bye[-1].decode(self._encoding, 'replace')) @@ -1389,8 +1388,7 @@ def encode(self, inp): else: t = inp inp = b'' - e = binascii.b2a_base64(t) - if e: + if (e := binascii.b2a_base64(t)): oup = oup + e[:-1] return oup diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 76e8abb2d5833d..df072d82d66766 100644 --- a/Lib/imghdr.py +++ b/Lib/imghdr.py @@ -20,8 +20,7 @@ def what(file, h=None): h = file.read(32) file.seek(location) for tf in tests: - res = tf(h, f) - if res: + if (res := tf(h, f)): return res finally: if f: f.close() diff --git a/Lib/inspect.py b/Lib/inspect.py index 717518614fc6d7..b40d54bdf6ec4c 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -777,8 +777,7 @@ def findsource(object): if not (file.startswith('<') and file.endswith('>')): raise OSError('source code not available') - module = getmodule(object, file) - if module: + if (module := getmodule(object, file)): lines = linecache.getlines(file, module.__dict__) else: lines = linecache.getlines(file) @@ -796,8 +795,7 @@ def findsource(object): # that's most probably not inside a function definition. candidates = [] for i in range(len(lines)): - match = pat.match(lines[i]) - if match: + if (match := pat.match(lines[i])): # if it's at toplevel, it's already the best one if lines[i][0] == 'c': return lines, i @@ -1978,10 +1976,8 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): module = None module_dict = {} - module_name = getattr(obj, '__module__', None) - if module_name: - module = sys.modules.get(module_name, None) - if module: + if (module_name := getattr(obj, '__module__', None)): + if (module := sys.modules.get(module_name, None)): module_dict = module.__dict__ sys_module_dict = sys.modules diff --git a/Lib/keyword.py b/Lib/keyword.py index 431991dcf4ace6..855e961d41536f 100755 --- a/Lib/keyword.py +++ b/Lib/keyword.py @@ -74,8 +74,7 @@ def main(): lines = [] for line in fp: if '{1, "' in line: - match = strprog.search(line) - if match: + if (match := strprog.search(line)): lines.append(" '" + match.group(1) + "'," + nl) lines.sort() diff --git a/Lib/locale.py b/Lib/locale.py index f3d3973d038c51..0f554b5c8c18fc 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -311,13 +311,11 @@ def delocalize(string): conv = localeconv() #First, get rid of the grouping - ts = conv['thousands_sep'] - if ts: + if (ts := conv['thousands_sep']): string = string.replace(ts, '') #next, replace the decimal point with a dot - dd = conv['decimal_point'] - if dd: + if (dd := conv['decimal_point']): string = string.replace(dd, '.') return string diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 29a7d464decf98..7cced25e2a9082 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1849,8 +1849,7 @@ def basicConfig(**kwargs): # basicConfig() from multiple threads _acquireLock() try: - force = kwargs.pop('force', False) - if force: + if kwargs.pop('force', False): for h in root.handlers[:]: root.removeHandler(h) h.close() @@ -2002,8 +2001,7 @@ def shutdown(handlerList=_handlerList): #errors might occur, for example, if files are locked #we just ignore them if raiseExceptions is not set try: - h = wr() - if h: + if (h := wr()): try: h.acquire() h.flush() diff --git a/Lib/logging/config.py b/Lib/logging/config.py index fa1a398aee2a2b..537efdd12a8df4 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -113,8 +113,7 @@ def _create_formatters(cp): dfs = cp.get(sectname, "datefmt", raw=True, fallback=None) stl = cp.get(sectname, "style", raw=True, fallback='%') c = logging.Formatter - class_name = cp[sectname].get("class") - if class_name: + if (class_name := cp[sectname].get("class")): c = _resolve(class_name) f = c(fs, dfs, stl) formatters[form] = f @@ -425,6 +424,7 @@ def cfg_convert(self, value): d = d[n] except TypeError: d = d[idx] + if m: rest = rest[m.end():] else: @@ -450,12 +450,10 @@ def convert(self, value): value = ConvertingTuple(value) value.configurator = self elif isinstance(value, str): # str for py3k - m = self.CONVERT_PATTERN.match(value) - if m: + if (m := self.CONVERT_PATTERN.match(value)): d = m.groupdict() prefix = d['prefix'] - converter = self.value_converters.get(prefix, None) - if converter: + if (converter := self.value_converters.get(prefix, None)): suffix = d['suffix'] converter = getattr(self, converter) value = converter(suffix) @@ -509,8 +507,7 @@ def configure(self): try: handler = logging._handlers[name] handler_config = handlers[name] - level = handler_config.get('level', None) - if level: + if (level := handler_config.get('level', None)): handler.setLevel(logging._checkLevel(level)) except Exception as e: raise ValueError('Unable to configure handler ' @@ -522,8 +519,7 @@ def configure(self): except Exception as e: raise ValueError('Unable to configure logger ' '%r' % name) from e - root = config.get('root', None) - if root: + if (root := config.get('root', None)): try: self.configure_root(root, True) except Exception as e: @@ -635,8 +631,7 @@ def configure(self): disable_existing) # And finally, do the root logger - root = config.get('root', None) - if root: + if (root := config.get('root', None)): try: self.configure_root(root) except Exception as e: @@ -772,11 +767,9 @@ def common_logger_config(self, logger, config, incremental=False): #Remove any existing handlers for h in logger.handlers[:]: logger.removeHandler(h) - handlers = config.get('handlers', None) - if handlers: + if (handlers := config.get('handlers', None)): self.add_handlers(logger, handlers) - filters = config.get('filters', None) - if filters: + if (filters := config.get('filters', None)): self.add_filters(logger, filters) def configure_logger(self, name, config, incremental=False): diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index 974c089d40ec34..7db0bc3df3b6a1 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -586,8 +586,7 @@ def makePickle(self, record): Pickles the record in binary format with a length prefix, and returns it ready for transmission across the socket. """ - ei = record.exc_info - if ei: + if record.exc_info: # just to get traceback text into record.exc_text ... dummy = self.format(record) # See issue #14436: If msg or args are objects, they may not be @@ -638,8 +637,7 @@ def close(self): """ self.acquire() try: - sock = self.sock - if sock: + if (sock := self.sock): self.sock = None sock.close() logging.Handler.close(self) diff --git a/Lib/mailcap.py b/Lib/mailcap.py index bd0fc0981c8c6d..2284b38b940694 100644 --- a/Lib/mailcap.py +++ b/Lib/mailcap.py @@ -250,8 +250,7 @@ def test(): print("No viewer found for", type) else: print("Executing:", command) - sts = os.system(command) - if sts: + if (sts := os.system(command)): print("Exit status:", sts) def show(caps): diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index 10320a74d94249..32d9988c2cb0fa 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -174,15 +174,13 @@ def find_head_package(self, parent, name): qname = "%s.%s" % (parent.__name__, head) else: qname = head - q = self.import_module(head, qname, parent) - if q: + if (q := self.import_module(head, qname, parent)): self.msgout(4, "find_head_package ->", (q, tail)) return q, tail if parent: qname = head parent = None - q = self.import_module(head, qname, parent) - if q: + if (q := self.import_module(head, qname, parent)): self.msgout(4, "find_head_package ->", (q, tail)) return q, tail self.msgout(4, "raise ImportError: No module named", qname) @@ -208,8 +206,7 @@ def ensure_fromlist(self, m, fromlist, recursive=0): for sub in fromlist: if sub == "*": if not recursive: - all = self.find_all_submodules(m) - if all: + if (all := self.find_all_submodules(m)): self.ensure_fromlist(m, all, 1) elif not hasattr(m, sub): subname = "%s.%s" % (m.__name__, sub) @@ -408,8 +405,7 @@ def scan_code(self, co, m): def load_package(self, fqname, pathname): self.msgin(2, "load_package", fqname, pathname) - newname = replacePackageMap.get(fqname) - if newname: + if (newname := replacePackageMap.get(fqname)): fqname = newname m = self.add_module(fqname) m.__file__ = pathname diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py index 715a9b0e12900e..eeeaba2734e22d 100644 --- a/Lib/multiprocessing/queues.py +++ b/Lib/multiprocessing/queues.py @@ -133,8 +133,7 @@ def close(self): try: self._reader.close() finally: - close = self._close - if close: + if (close := self._close): self._close = None close() diff --git a/Lib/nntplib.py b/Lib/nntplib.py index a5d13e35be0e6e..01acab4453fc82 100644 --- a/Lib/nntplib.py +++ b/Lib/nntplib.py @@ -948,8 +948,7 @@ def login(self, user=None, password=None, usenetrc=True): if usenetrc and not user: import netrc credentials = netrc.netrc() - auth = credentials.authenticators(self.host) - if auth: + if (auth := credentials.authenticators(self.host)): user = auth[0] password = auth[2] except OSError: diff --git a/Lib/pdb.py b/Lib/pdb.py index 60bdb7675c8131..a8e047f880f7ed 100755 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -326,8 +326,7 @@ def _cmdloop(self): # Called before loop, handles display expressions def preloop(self): - displaying = self.displaying.get(self.curframe) - if displaying: + if (displaying := self.displaying.get(self.curframe)): for expr, oldvalue in displaying.items(): newvalue = self._getval_except(expr) # check for identity first; this prevents custom __eq__ to @@ -674,11 +673,9 @@ def do_break(self, arg, temporary = 0): if not filename: filename = self.defaultFile() # Check for reasonable breakpoint - line = self.checkline(filename, lineno) - if line: + if (line := self.checkline(filename, lineno)): # now set the break point - err = self.set_break(filename, line, temporary, cond, funcname) - if err: + if (err := self.set_break(filename, line, temporary, cond, funcname)): self.error(err) else: bp = self.get_breaks(filename, line)[-1] @@ -733,8 +730,7 @@ def lineinfo(self, identifier): else: # More than one part. # First is module, second is method/class - f = self.lookupmodule(parts[0]) - if f: + if (f := self.lookupmodule(parts[0])): fname = f item = parts[1] answer = find_function(item, fname) diff --git a/Lib/pickletools.py b/Lib/pickletools.py index 8486cbf8436878..85f6c4af0474ff 100644 --- a/Lib/pickletools.py +++ b/Lib/pickletools.py @@ -216,8 +216,7 @@ def read_uint1(f): 255 """ - data = f.read(1) - if data: + if (data := f.read(1)): return data[0] raise ValueError("not enough data in stream to read uint1") diff --git a/Lib/platform.py b/Lib/platform.py index 6051f2b590195f..1544ad508be82a 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -516,8 +516,7 @@ def system_alias(system, release, version): # These releases use the old name SunOS return system, release, version # Modify release (marketing release = SunOS release - 3) - l = release.split('.') - if l: + if (l := release.split('.')): try: major = int(l[0]) except ValueError: diff --git a/Lib/pprint.py b/Lib/pprint.py index bcf2eedebe6b54..f9eef85d01cb1a 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -182,8 +182,7 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level): write('{') if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') - length = len(object) - if length: + if len(object): items = sorted(object.items(), key=_safe_tuple) self._format_dict_items(items, stream, indent, allowance + 1, context, level) diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 16dc0a011ffab2..92eca001888712 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -142,8 +142,7 @@ def compile(file, cfile=None, dfile=None, doraise=False, optimize=-1, sys.stderr.write(py_exc.msg + '\n') return try: - dirname = os.path.dirname(cfile) - if dirname: + if (dirname := os.path.dirname(cfile)): os.makedirs(dirname) except FileExistsError: pass diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 199745ca6fa8b0..ea4b5d8414a7ff 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -1242,8 +1242,7 @@ def makename(c, m=object.__module__): if argspec and argspec != '()': push(name + argspec + '\n') - doc = getdoc(object) - if doc: + if (doc := getdoc(object)): push(doc + '\n') # List the mro, if non-trivial. diff --git a/Lib/selectors.py b/Lib/selectors.py index a9a0801ef0713b..b8c4f918e97d36 100644 --- a/Lib/selectors.py +++ b/Lib/selectors.py @@ -332,8 +332,7 @@ def select(self, timeout=None): if fd in w: events |= EVENT_WRITE - key = self._key_from_fd(fd) - if key: + if (key := self._key_from_fd(fd)): ready.append((key, events & key.events)) return ready @@ -422,8 +421,7 @@ def select(self, timeout=None): if event & ~self._EVENT_WRITE: events |= EVENT_READ - key = self._key_from_fd(fd) - if key: + if (key := self._key_from_fd(fd)): ready.append((key, events & key.events)) return ready @@ -475,8 +473,7 @@ def select(self, timeout=None): if event & ~select.EPOLLOUT: events |= EVENT_READ - key = self._key_from_fd(fd) - if key: + if (key := self._key_from_fd(fd)): ready.append((key, events & key.events)) return ready @@ -567,8 +564,7 @@ def select(self, timeout=None): if flag == select.KQ_FILTER_WRITE: events |= EVENT_WRITE - key = self._key_from_fd(fd) - if key: + if (key := self._key_from_fd(fd)): ready.append((key, events & key.events)) return ready diff --git a/Lib/site.py b/Lib/site.py index 4595244e2f5a78..26290420a65b03 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -243,8 +243,7 @@ def check_enableusersite(): # Copy of sysconfig._getuserbase() def _getuserbase(): - env_base = os.environ.get("PYTHONUSERBASE", None) - if env_base: + if (env_base := os.environ.get("PYTHONUSERBASE", None)): return env_base def joinuser(*args): diff --git a/Lib/smtpd.py b/Lib/smtpd.py index 8103ca9af0d7b3..683abdc60b1475 100755 --- a/Lib/smtpd.py +++ b/Lib/smtpd.py @@ -549,8 +549,7 @@ def smtp_MAIL(self, arg): elif smtputf8 is not False: self.push('501 Error: SMTPUTF8 takes no arguments') return - size = params.pop('SIZE', None) - if size: + if (size := params.pop('SIZE', None)): if not size.isdigit(): self.push(syntaxerr) return diff --git a/Lib/smtplib.py b/Lib/smtplib.py index b679875fd2c539..72e7ea773fde5e 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -460,8 +460,7 @@ def ehlo(self, name=''): # 1) Else our SMTP feature parser gets confused. # 2) There are some servers that only advertise the auth methods we # support using the old style. - auth_match = OLDSTYLE_AUTH.match(each) - if auth_match: + if (auth_match := OLDSTYLE_AUTH.match(each)): # This doesn't remove duplicates, but that's no problem self.esmtp_features["auth"] = self.esmtp_features.get("auth", "") \ + " " + auth_match.groups(0)[0] diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py index 594353136f5c37..11acbb987dd243 100644 --- a/Lib/sndhdr.py +++ b/Lib/sndhdr.py @@ -60,8 +60,7 @@ def whathdr(filename): with open(filename, 'rb') as f: h = f.read(512) for tf in tests: - res = tf(h, f) - if res: + if (res := tf(h, f)): return SndHeaders(*res) return None @@ -129,8 +128,7 @@ def test_au(h, f): def test_hcom(h, f): if h[65:69] != b'FSSD' or h[128:132] != b'HCOM': return None - divisor = get_long_be(h[144:148]) - if divisor: + if (divisor := get_long_be(h[144:148])): rate = 22050 / divisor else: rate = 0 diff --git a/Lib/socket.py b/Lib/socket.py index cfa605a22ada92..1b6164633aa434 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -174,14 +174,12 @@ def __repr__(self): self.proto) if not closed: try: - laddr = self.getsockname() - if laddr: + if (laddr := self.getsockname()): s += ", laddr=%s" % str(laddr) except error: pass try: - raddr = self.getpeername() - if raddr: + if (raddr := self.getpeername()): s += ", raddr=%s" % str(raddr) except error: pass diff --git a/Lib/socketserver.py b/Lib/socketserver.py index 9dfd21bab9b6f9..f02b8114c84b54 100644 --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -229,8 +229,7 @@ def serve_forever(self, poll_interval=0.5): selector.register(self, selectors.EVENT_READ) while not self.__shutdown_request: - ready = selector.select(poll_interval) - if ready: + if selector.select(poll_interval): self._handle_request_noblock() self.service_actions() @@ -288,8 +287,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: @@ -597,8 +595,7 @@ def service_actions(self): def process_request(self, request, client_address): """Fork a new subprocess to process the request.""" - pid = os.fork() - if pid: + if (pid := os.fork()): # Parent process if self.active_children is None: self.active_children = set() diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 7a172ff2fb14d5..3b0c3fa90498f5 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -294,11 +294,9 @@ def error(self, msg, offset=0): def _class_escape(source, escape): # handle escape code inside character class - code = ESCAPES.get(escape) - if code: + if (code := ESCAPES.get(escape)): return code - code = CATEGORIES.get(escape) - if code and code[0] is IN: + if (code := CATEGORIES.get(escape)) and code[0] is IN: return code try: c = escape[1:2] @@ -354,11 +352,9 @@ def _class_escape(source, escape): def _escape(source, escape, state): # handle escape code in expression - code = CATEGORIES.get(escape) - if code: + if (code := CATEGORIES.get(escape)): return code - code = ESCAPES.get(escape) - if code: + if (code := ESCAPES.get(escape)): return code try: c = escape[1:2] diff --git a/Lib/sunau.py b/Lib/sunau.py index 129502b0b4177a..9d3ae461c5d093 100644 --- a/Lib/sunau.py +++ b/Lib/sunau.py @@ -298,8 +298,7 @@ def setpos(self, pos): self._soundpos = pos def close(self): - file = self._file - if file: + if (file := self._file): self._file = None if self._opened: file.close() diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 9ee4d3185a7127..48ce7c764b6c5f 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -181,8 +181,7 @@ def _get_default_scheme(): # NOTE: site.py has copy of this function. # Sync it when modify this function. def _getuserbase(): - env_base = os.environ.get("PYTHONUSERBASE", None) - if env_base: + if (env_base := os.environ.get("PYTHONUSERBASE", None)): return env_base def joinuser(*args): @@ -224,8 +223,7 @@ def _parse_makefile(filename, vars=None): for line in lines: if line.startswith('#') or line.strip() == '': continue - m = _variable_rx.match(line) - if m: + if (m := _variable_rx.match(line)): n, v = m.group(1, 2) v = v.strip() # `$$' is a literal `$' in make @@ -449,8 +447,7 @@ def parse_config_h(fp, vars=None): line = fp.readline() if not line: break - m = define_rx.match(line) - if m: + if (m := define_rx.match(line)): n, v = m.group(1, 2) try: v = int(v) @@ -458,8 +455,7 @@ def parse_config_h(fp, vars=None): pass vars[n] = v else: - m = undef_rx.match(line) - if m: + if (m := undef_rx.match(line)): vars[m.group(1)] = 0 return vars @@ -659,8 +655,7 @@ def get_platform(): osname = "cygwin" import re rel_re = re.compile(r'[\d.]+') - m = rel_re.match(release) - if m: + if (m := rel_re.match(release)): release = m.group() elif osname[:6] == "darwin": import _osx_support diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py index b9d45b48e79fb6..baf9874536e0e2 100644 --- a/Lib/telnetlib.py +++ b/Lib/telnetlib.py @@ -617,8 +617,7 @@ def expect(self, list, timeout=None): while not self.eof: self.process_rawq() for i in indices: - m = list[i].search(self.cookedq) - if m: + if (m := list[i].search(self.cookedq)): e = m.end() text = self.cookedq[:e] self.cookedq = self.cookedq[e:] diff --git a/Lib/tempfile.py b/Lib/tempfile.py index e6fb3c8e9ad856..ccce05b30f30a5 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -164,8 +164,7 @@ def _candidate_tempdir_list(): # First, try the environment. for envname in 'TMPDIR', 'TEMP', 'TMP': - dirname = _os.getenv(envname) - if dirname: dirlist.append(dirname) + if (dirname := _os.getenv(envname)): dirlist.append(dirname) # Failing that, try OS-specific locations. if _os.name == 'nt': diff --git a/Lib/token.py b/Lib/token.py index ba132059abf5ee..93d8cd587484fc 100644 --- a/Lib/token.py +++ b/Lib/token.py @@ -115,8 +115,7 @@ def _main(): tokens = {} prev_val = None for line in lines: - match = prog.match(line) - if match: + if (match := prog.match(line)): name, val = match.group(1, 2) val = int(val) tokens[val] = {'token': name} # reverse so we can sort them... diff --git a/Lib/tokenize.py b/Lib/tokenize.py index c78d9f7e9ee5af..78b0a351516ccd 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -422,8 +422,7 @@ def find_cookie(line): if not first: return default, [] - encoding = find_cookie(first) - if encoding: + if (encoding := find_cookie(first)): return encoding, [first] if not blank_re.match(first): return default, [first] @@ -432,8 +431,7 @@ def find_cookie(line): if not second: return default, [first] - encoding = find_cookie(second) - if encoding: + if (encoding := find_cookie(second)): return encoding, [first, second] return default, [first, second] @@ -506,8 +504,7 @@ def _tokenize(readline, encoding): if contstr: # continued string if not line: raise TokenError("EOF in multi-line string", strstart) - endmatch = endprog.match(line) - if endmatch: + if (endmatch := endprog.match(line)): pos = end = endmatch.end(0) yield TokenInfo(STRING, contstr + line[:end], strstart, (lnum, end), contline + line) @@ -569,8 +566,7 @@ def _tokenize(readline, encoding): continued = 0 while pos < max: - pseudomatch = _compile(PseudoToken).match(line, pos) - if pseudomatch: # scan for tokens + if (pseudomatch := _compile(PseudoToken).match(line, pos)): # scan for tokens start, end = pseudomatch.span(1) spos, epos, pos = (lnum, start), (lnum, end), end if start == end: @@ -592,8 +588,7 @@ def _tokenize(readline, encoding): elif token in triple_quoted: endprog = _compile(endpats[token]) - endmatch = endprog.match(line, pos) - if endmatch: # all on one line + if (endmatch := endprog.match(line, pos)): # all on one line pos = endmatch.end(0) token = line[start:pos] yield TokenInfo(STRING, token, spos, (lnum, pos), line) diff --git a/Lib/trace.py b/Lib/trace.py index 16c3494689dd9c..2d9ede8da4deb3 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -471,8 +471,7 @@ def runfunc(self, func, *args, **kw): def file_module_function_of(self, frame): code = frame.f_code - filename = code.co_filename - if filename: + if (filename := code.co_filename): modulename = _modname(filename) else: modulename = None @@ -538,8 +537,7 @@ def globaltrace_lt(self, frame, why, arg): """ if why == 'call': code = frame.f_code - filename = frame.f_globals.get('__file__', None) - if filename: + if (filename := frame.f_globals.get('__file__', None)): # XXX _modname() doesn't work right for packages, so # the ignore support won't work right for packages modulename = _modname(filename) diff --git a/Lib/tracemalloc.py b/Lib/tracemalloc.py index 2c1ac3b39b0784..1605ac593f4588 100644 --- a/Lib/tracemalloc.py +++ b/Lib/tracemalloc.py @@ -226,8 +226,7 @@ def format(self, limit=None, most_recent_first=False): for frame in frame_slice: lines.append(' File "%s", line %s' % (frame.filename, frame.lineno)) - line = linecache.getline(frame.filename, frame.lineno).strip() - if line: + if (line := linecache.getline(frame.filename, frame.lineno).strip()): lines.append(' %s' % line) return lines diff --git a/Lib/uuid.py b/Lib/uuid.py index 66383218e70c0d..0d79bfec859edf 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -393,16 +393,14 @@ def _ifconfig_getnode(): # This works on Linux ('' or '-a'), Tru64 ('-av'), but not all Unixes. keywords = (b'hwaddr', b'ether', b'address:', b'lladdr') for args in ('', '-a', '-av'): - mac = _find_mac('ifconfig', args, keywords, lambda i: i+1) - if mac: + if (mac := _find_mac('ifconfig', args, keywords, lambda i: i+1)): return mac return None def _ip_getnode(): """Get the hardware address on Unix by running ip.""" # This works on Linux with iproute2. - mac = _find_mac('ip', 'link', [b'link/ether'], lambda i: i+1) - if mac: + if (mac := _find_mac('ip', 'link', [b'link/ether'], lambda i: i+1)): return mac return None diff --git a/Lib/wave.py b/Lib/wave.py index f155879a9a76ab..c726ac58416c1d 100644 --- a/Lib/wave.py +++ b/Lib/wave.py @@ -188,8 +188,7 @@ def rewind(self): def close(self): self._file = None - file = self._i_opened_the_file - if file: + if (file := self._i_opened_the_file): self._i_opened_the_file = None file.close() @@ -448,8 +447,7 @@ def close(self): self._file.flush() finally: self._file = None - file = self._i_opened_the_file - if file: + if (file := self._i_opened_the_file): self._i_opened_the_file = None file.close() diff --git a/Lib/weakref.py b/Lib/weakref.py index 99de2eab741439..e06f79880c4f46 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -576,8 +576,7 @@ def atexit(self): @atexit.setter def atexit(self, value): - info = self._registry.get(self) - if info: + if (info := self._registry.get(self)): info.atexit = bool(value) def __repr__(self): diff --git a/Lib/webbrowser.py b/Lib/webbrowser.py index d717193d05c21f..50f3ed56a3882a 100755 --- a/Lib/webbrowser.py +++ b/Lib/webbrowser.py @@ -206,8 +206,7 @@ def _invoke(self, args, remote, autoraise): if remote and self.raise_opts: # use autoraise argument only for remote invocation autoraise = int(autoraise) - opt = self.raise_opts[autoraise] - if opt: raise_opt = [opt] + if (opt := self.raise_opts[autoraise]): raise_opt = [opt] cmdline = [self.name] + raise_opt + args diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 9c265b297f71dd..5ad444aa521c57 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -898,11 +898,9 @@ def read1(self, n): buf = self._readbuffer[self._offset:] self._readbuffer = b'' self._offset = 0 - while not self._eof: - data = self._read1(self.MAX_N) - if data: - buf += data - break + while not self._eof and (data := self._read1(self.MAX_N)): + buf += data + break return buf end = n + self._offset From 057640af191b5290d5db4d90ad687651e387e428 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 17:22:00 +0200 Subject: [PATCH 4/6] Fix zipfile --- Lib/zipfile.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 5ad444aa521c57..9fdc0f9002d4eb 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -898,9 +898,10 @@ def read1(self, n): buf = self._readbuffer[self._offset:] self._readbuffer = b'' self._offset = 0 - while not self._eof and (data := self._read1(self.MAX_N)): - buf += data - break + while not self._eof: + if (data := self._read1(self.MAX_N)): + buf += data + break return buf end = n + self._offset From d94168648a987d83c21a91c45afbda3caf2aa3b5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 03:07:14 +0200 Subject: [PATCH 5/6] WIP: Use assign expr for match/group Replace: m = regex.match(text) if m: return m.group(1) with: if (m := regex.match(text)): return m.group(1) Notes: * Similar code using "if not m: return" is unchanged * When the condition was more complex than "if m", the code is left unchanged --- Lib/_osx_support.py | 3 +-- Lib/configparser.py | 6 ++---- Lib/imaplib.py | 7 ++++--- Lib/nntplib.py | 3 +-- Lib/smtplib.py | 3 +-- Lib/string.py | 3 +-- Lib/sysconfig.py | 5 ++--- Lib/tarfile.py | 3 +-- Lib/token.py | 8 +++----- 9 files changed, 16 insertions(+), 25 deletions(-) diff --git a/Lib/_osx_support.py b/Lib/_osx_support.py index e37852e2536c33..655288e95a4138 100644 --- a/Lib/_osx_support.py +++ b/Lib/_osx_support.py @@ -287,8 +287,7 @@ def _check_for_unavailable_sdk(_config_vars): # to /usr and /System/Library by either a standalone CLT # package or the CLT component within Xcode. cflags = _config_vars.get('CFLAGS', '') - m = re.search(r'-isysroot\s+(\S+)', cflags) - if m is not None: + if (m := re.search(r'-isysroot\s+(\S+)', cflags)) is not None: sdk = m.group(1) if not os.path.exists(sdk): for cv in _UNIVERSAL_CONFIG_VARS: diff --git a/Lib/configparser.py b/Lib/configparser.py index 4a16101c7a3ab6..657fcf8e511cea 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -1059,8 +1059,7 @@ def _read(self, fp, fpname): else: indent_level = cur_indent_level # is it a section header? - mo = self.SECTCRE.match(value) - if mo: + if (mo := self.SECTCRE.match(value)): sectname = mo.group('header') if sectname in self._sections: if self._strict and sectname in elements_added: @@ -1082,8 +1081,7 @@ def _read(self, fp, fpname): raise MissingSectionHeaderError(fpname, lineno, line) # an option line? else: - mo = self._optcre.match(value) - if mo: + if (mo := self._optcre.match(value)): optname, vi, optval = mo.group('option', 'vi', 'value') if not optname: e = self._handle_error(e, fpname, lineno, line) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 87095efc45fefc..4016e5c4aa7478 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -1577,9 +1577,10 @@ def run(cmd, args): run(cmd, args) for ml in run('list', ('/tmp/', 'yy%')): - mo = re.match(r'.*"([^"]+)"$', ml) - if mo: path = mo.group(1) - else: path = ml.split()[-1] + if (mo := re.match(r'.*"([^"]+)"$', ml)): + path = mo.group(1) + else: + path = ml.split()[-1] run('delete', (path,)) for cmd,args in test_seq2: diff --git a/Lib/nntplib.py b/Lib/nntplib.py index 01acab4453fc82..e0ad04c62a62b8 100644 --- a/Lib/nntplib.py +++ b/Lib/nntplib.py @@ -619,8 +619,7 @@ def _getdescriptions(self, group_pattern, return_all): resp, lines = self._longcmdstring('XGTITLE ' + group_pattern) groups = {} for raw_line in lines: - match = line_pat.search(raw_line.strip()) - if match: + if (match := line_pat.search(raw_line.strip())): name, desc = match.group(1, 2) if not return_all: return desc diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 72e7ea773fde5e..0f665cfdddb78d 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -470,8 +470,7 @@ def ehlo(self, name=''): # It's actually stricter, in that only spaces are allowed between # parameters, but were not going to check for that here. Note # that the space isn't present if there are no parameters. - m = re.match(r'(?P[A-Za-z0-9][A-Za-z0-9\-]*) ?', each) - if m: + if (m := re.match(r'(?P[A-Za-z0-9][A-Za-z0-9\-]*) ?', each)): feature = m.group("feature").lower() params = m.string[m.end("feature"):].strip() if feature == "auth": diff --git a/Lib/string.py b/Lib/string.py index b9d6f5eb5675f6..e04574fc4c72e4 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -120,8 +120,7 @@ def substitute(*args, **kws): # Helper function for .sub() def convert(mo): # Check the most common path first. - named = mo.group('named') or mo.group('braced') - if named is not None: + if (named := mo.group('named') or mo.group('braced')) is not None: return str(mapping[named]) if mo.group('escaped') is not None: return self.delimiter diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 48ce7c764b6c5f..0f7640eb674d63 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -454,9 +454,8 @@ def parse_config_h(fp, vars=None): except ValueError: pass vars[n] = v - else: - if (m := undef_rx.match(line)): - vars[m.group(1)] = 0 + elif (m := undef_rx.match(line)): + vars[m.group(1)] = 0 return vars diff --git a/Lib/tarfile.py b/Lib/tarfile.py index 59f044cc5a00dc..a00215da00fda9 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1200,8 +1200,7 @@ def _proc_pax(self, tarfile): # these fields are UTF-8 encoded but since POSIX.1-2008 tar # implementations are allowed to store them as raw binary strings if # the translation to UTF-8 fails. - match = re.search(br"\d+ hdrcharset=([^\n]+)\n", buf) - if match is not None: + if (match := re.search(br"\d+ hdrcharset=([^\n]+)\n", buf)) is not None: pax_headers["hdrcharset"] = match.group(1).decode("utf-8") # For the time being, we don't care about anything other than "BINARY". diff --git a/Lib/token.py b/Lib/token.py index 93d8cd587484fc..353217ff23285a 100644 --- a/Lib/token.py +++ b/Lib/token.py @@ -120,11 +120,9 @@ def _main(): val = int(val) tokens[val] = {'token': name} # reverse so we can sort them... prev_val = val - else: - comment_match = comment_regex.match(line) - if comment_match and prev_val is not None: - comment = comment_match.group(1) - tokens[prev_val]['comment'] = comment + elif (comment_match := comment_regex.match(line)) and prev_val is not None: + comment = comment_match.group(1) + tokens[prev_val]['comment'] = comment keys = sorted(tokens.keys()) # load the output skeleton from the target: try: From d3806cc592711dd8098cdad2bc5e984c4a7dd697 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 5 Jul 2018 00:10:01 +0200 Subject: [PATCH 6/6] WIP: Use assign expr for "while True: ... break" Replace: while True: x = expr if not x: break ... with: while (x := expr): ... Notes: * Corner case: (x := expr) and (x != NUL), use x in the second test, whereas x is set in the first test * asyncio/sslproto.py change avoids adding an empty chunk to appdata. * some changes remove comments * some lines are longer than 80 characters to keep everything on the same line --- Lib/_pydecimal.py | 8 ++------ Lib/_pyio.py | 5 +---- Lib/aifc.py | 5 +---- Lib/asyncio/sslproto.py | 5 +---- Lib/asyncio/streams.py | 5 +---- Lib/asyncio/windows_events.py | 5 +---- Lib/base64.py | 15 +++------------ Lib/binhex.py | 16 ++++------------ Lib/chunk.py | 5 +---- Lib/fnmatch.py | 5 +---- Lib/ftplib.py | 10 ++-------- Lib/getpass.py | 5 +---- Lib/gzip.py | 5 +---- Lib/mailbox.py | 23 ++++++----------------- Lib/mailcap.py | 4 +--- Lib/mimetypes.py | 5 +---- Lib/nntplib.py | 10 ++-------- Lib/platform.py | 5 +---- Lib/posixpath.py | 5 +---- Lib/py_compile.py | 5 +---- Lib/pydoc.py | 4 +--- Lib/quopri.py | 9 ++------- Lib/re.py | 8 +------- Lib/shlex.py | 5 +---- Lib/shutil.py | 12 +++--------- Lib/smtplib.py | 5 +---- Lib/sre_compile.py | 5 +---- Lib/sre_parse.py | 21 ++++++--------------- Lib/sysconfig.py | 5 +---- Lib/tarfile.py | 34 +++++++++------------------------- Lib/telnetlib.py | 5 +---- Lib/xdrlib.py | 4 +--- 32 files changed, 61 insertions(+), 207 deletions(-) diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 5a47f2cd412fff..35ead8a1275619 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -2753,12 +2753,8 @@ def sqrt(self, context=None): # find n = floor(sqrt(c)) using Newton's method n = 10**prec - while True: - q = c//n - if n <= q: - break - else: - n = n + q >> 1 + while (q := c//n) < n: + n = n + q >> 1 exact = exact and n*n == c if exact: diff --git a/Lib/_pyio.py b/Lib/_pyio.py index a3162015440cf1..68718e8984b8e8 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -588,10 +588,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) diff --git a/Lib/aifc.py b/Lib/aifc.py index 1916e7ef8e7eab..afe96dff758ef7 100644 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -943,9 +943,6 @@ def openfp(f, mode=None): print("Writing", gn) with open(gn, 'w') as g: g.setparams(f.getparams()) - while 1: - data = f.readframes(1024) - if not data: - break + while (data := f.readframes(1024)): g.writeframes(data) print("Done.") diff --git a/Lib/asyncio/sslproto.py b/Lib/asyncio/sslproto.py index a7804e017ff9cc..8e2acff9c1e938 100644 --- a/Lib/asyncio/sslproto.py +++ b/Lib/asyncio/sslproto.py @@ -196,11 +196,8 @@ def feed_ssldata(self, data, only_handshake=False): if self._state == _WRAPPED: # Main state: read data from SSL until close_notify - while True: - chunk = self._sslobj.read(self.max_size) + while (chunk := self._sslobj.read(self.max_size)): appdata.append(chunk) - if not chunk: # close_notify - break elif self._state == _SHUTDOWN: # Call shutdown() until it doesn't raise anymore. diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index d6531f88a74d79..74dc48572e6975 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -629,10 +629,7 @@ async def read(self, n=-1): # deadlock if the subprocess sends more than self.limit # bytes. So just call self.read(self._limit) until EOF. blocks = [] - while True: - block = await self.read(self._limit) - if not block: - break + while (block := await self.read(self._limit)): blocks.append(block) return b''.join(blocks) diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py index 2ec542764375e9..8d5c26992167d9 100644 --- a/Lib/asyncio/windows_events.py +++ b/Lib/asyncio/windows_events.py @@ -699,10 +699,7 @@ def _poll(self, timeout=None): if ms >= INFINITE: raise ValueError("timeout too big") - while True: - status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms) - if status is None: - break + while (status := _overlapped.GetQueuedCompletionStatus(self._iocp, ms)) is not None: ms = 0 err, transferred, key, address = status diff --git a/Lib/base64.py b/Lib/base64.py index eb8f258a2d1977..9de758f8affc70 100755 --- a/Lib/base64.py +++ b/Lib/base64.py @@ -490,14 +490,8 @@ 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) @@ -505,10 +499,7 @@ def encode(input, output): 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) diff --git a/Lib/binhex.py b/Lib/binhex.py index 9db06270aec257..05b4486d471e70 100644 --- a/Lib/binhex.py +++ b/Lib/binhex.py @@ -237,16 +237,12 @@ def binhex(inp, out): with io.open(inp, 'rb') as ifp: # XXXX Do textfile translation on non-mac systems - while True: - d = ifp.read(128000) - if not d: break + while (d := ifp.read(128000)): ofp.write(d) ofp.close_data() ifp = openrsrc(inp, 'rb') - while True: - d = ifp.read(128000) - if not d: break + while (d := ifp.read(128000)): ofp.write_rsrc(d) ofp.close() ifp.close() @@ -460,18 +456,14 @@ def hexbin(inp, out): with io.open(out, 'wb') as ofp: # XXXX Do translation on non-mac systems - while True: - d = ifp.read(128000) - if not d: break + while (d := ifp.read(128000)): ofp.write(d) ifp.close_data() if (d := ifp.read_rsrc(128000)): ofp = openrsrc(out, 'wb') ofp.write(d) - while True: - d = ifp.read_rsrc(128000) - if not d: break + while (d := ifp.read_rsrc(128000)): ofp.write(d) ofp.close() diff --git a/Lib/chunk.py b/Lib/chunk.py index 870c39fe7f5037..a48b20abfb1ff9 100644 --- a/Lib/chunk.py +++ b/Lib/chunk.py @@ -31,10 +31,7 @@ except EOFError: break chunktype = chunk.getname() - while True: - data = chunk.read(nbytes) - if not data: - pass + while (data := chunk.read(nbytes)): # do something with data The interface is file-like. The implemented methods are: diff --git a/Lib/fnmatch.py b/Lib/fnmatch.py index b98e6413295e1c..efcfd43aed0ca0 100644 --- a/Lib/fnmatch.py +++ b/Lib/fnmatch.py @@ -103,10 +103,7 @@ def translate(pat): else: chunks = [] k = i+2 if pat[i] == '!' else i+1 - while True: - k = pat.find('-', k, j) - if k < 0: - break + while (k := pat.find('-', k, j)) >= 0: chunks.append(pat[i:k]) i = k+1 k = k+3 diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 05840d492360a0..69b407c709d793 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -440,10 +440,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): @@ -502,10 +499,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) diff --git a/Lib/getpass.py b/Lib/getpass.py index fd616bec5f09f9..f604952bb47d73 100644 --- a/Lib/getpass.py +++ b/Lib/getpass.py @@ -102,10 +102,7 @@ def win_getpass(prompt='Password: ', stream=None): for c in prompt: msvcrt.putwch(c) pw = "" - while 1: - c = msvcrt.getwch() - if c == '\r' or c == '\n': - break + while (c := msvcrt.getwch()) not in ('\r', '\n'): if c == '\003': raise KeyboardInterrupt if c == '\b': diff --git a/Lib/gzip.py b/Lib/gzip.py index 9d145014abf078..791edbf1f52134 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -559,10 +559,7 @@ def _test(): else: f = builtins.open(arg, "rb") g = open(arg + ".gz", "wb") - while True: - chunk = f.read(1024) - if not chunk: - break + while (chunk := f.read(1024)): g.write(chunk) if g is not sys.stdout.buffer: g.close() diff --git a/Lib/mailbox.py b/Lib/mailbox.py index dc61a3a8b6ee95..00dcea898a80ea 100644 --- a/Lib/mailbox.py +++ b/Lib/mailbox.py @@ -679,11 +679,8 @@ def flush(self): self._file.seek(start) self._pre_message_hook(new_file) new_start = new_file.tell() - while True: - buffer = self._file.read(min(4096, - stop - self._file.tell())) - if not buffer: - break + while (buffer := self._file.read(min(4096, + stop - self._file.tell()))): new_file.write(buffer) new_toc[key] = (new_start, new_file.tell()) self._post_message_hook(new_file) @@ -1422,10 +1419,8 @@ def _install_message(self, message): self._file.write(line.replace(b'\n', linesep)) if line == b'\n' or not line: break - while True: - buffer = orig_buffer.read(4096) # Buffer size is arbitrary. - if not buffer: - break + # Buffer size is arbitrary. + while (buffer := orig_buffer.read(4096)): self._file.write(buffer.replace(b'\n', linesep)) elif isinstance(message, (bytes, str, io.StringIO)): if isinstance(message, io.StringIO): @@ -1465,10 +1460,7 @@ def _install_message(self, message): message.seek(original_pos) else: break - while True: - line = message.readline() - if not line: - break + while (line := message.readline()): # Universal newline support. if line.endswith(b'\r\n'): line = line[:-2] + linesep @@ -1953,10 +1945,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): diff --git a/Lib/mailcap.py b/Lib/mailcap.py index 2284b38b940694..4477f28fd64bec 100644 --- a/Lib/mailcap.py +++ b/Lib/mailcap.py @@ -78,9 +78,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 diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index f25872102b8c27..da2253e189daa8 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -212,10 +212,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] == '#': diff --git a/Lib/nntplib.py b/Lib/nntplib.py index e0ad04c62a62b8..3948d7fec1f357 100644 --- a/Lib/nntplib.py +++ b/Lib/nntplib.py @@ -481,19 +481,13 @@ def _getlongresp(self, file=None): if file is not None: # XXX lines = None instead? terminators = (b'.' + _CRLF, b'.\n') - while 1: - line = self._getline(False) - if line in terminators: - break + while (line := self._getline(False)) not in terminators: if line.startswith(b'..'): line = line[1:] file.write(line) else: terminator = b'.' - while 1: - line = self._getline() - if line == terminator: - break + while (line := self._getline()) != terminator: if line.startswith(b'..'): line = line[1:] lines.append(line) diff --git a/Lib/platform.py b/Lib/platform.py index 1544ad508be82a..1a0858d3004c5f 100755 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -571,10 +571,7 @@ def _platform(*args): platform = platform.replace('unknown', '') # Fold '--'s and remove trailing '-' - while 1: - cleaned = platform.replace('--', '-') - if cleaned == platform: - break + while (cleaned := platform.replace('--', '-')) != platform: platform = cleaned while platform[-1] == '-': platform = platform[:-1] diff --git a/Lib/posixpath.py b/Lib/posixpath.py index e92186c64e0ddb..e203682739f1f9 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -301,10 +301,7 @@ def expandvars(path): end = '}' environ = os.environ i = 0 - while True: - m = search(path, i) - if not m: - break + while (m := search(path, i)): i, j = m.span(0) name = m.group(1) if name.startswith(start) and name.endswith(end): diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 92eca001888712..7efb767fcc1945 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -177,10 +177,7 @@ def main(args=None): args = sys.argv[1:] rv = 0 if args == ['-']: - while True: - filename = sys.stdin.readline() - if not filename: - break + while (filename := sys.stdin.readline()): filename = filename.rstrip('\n') try: compile(filename, doraise=True) diff --git a/Lib/pydoc.py b/Lib/pydoc.py index ea4b5d8414a7ff..5a8959091e1ac5 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -595,9 +595,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])) diff --git a/Lib/quopri.py b/Lib/quopri.py index cbd979abdffc89..8b52c06596346c 100755 --- a/Lib/quopri.py +++ b/Lib/quopri.py @@ -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'' @@ -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 diff --git a/Lib/re.py b/Lib/re.py index 94d486579e08b8..0e5794c5798415 100644 --- a/Lib/re.py +++ b/Lib/re.py @@ -349,13 +349,7 @@ def scan(self, string): append = result.append match = self.scanner.scanner(string).match i = 0 - while True: - m = match() - if not m: - break - j = m.end() - if i == j: - break + while (m := match()) and (j := m.end()) == i: action = self.lexicon[m.lastindex-1][1] if callable(action): self.match = m diff --git a/Lib/shlex.py b/Lib/shlex.py index 2c9786c517a350..4d1d14e1e90a16 100644 --- a/Lib/shlex.py +++ b/Lib/shlex.py @@ -320,10 +320,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__': diff --git a/Lib/shutil.py b/Lib/shutil.py index a4aa0dfdd10b09..8015c752741822 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -178,11 +178,8 @@ def _copyfileobj_readinto(fsrc, fdst, length=COPY_BUFSIZE): fsrc_readinto = fsrc.readinto fdst_write = fdst.write with memoryview(bytearray(length)) as mv: - while True: - n = fsrc_readinto(mv) - if not n: - break - elif n < length: + while (n := fsrc_readinto(mv)): + if n < length: with mv[:n] as smv: fdst.write(smv) else: @@ -193,10 +190,7 @@ def copyfileobj(fsrc, fdst, length=COPY_BUFSIZE): # 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): diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 0f665cfdddb78d..9c895adb2d3d84 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -1101,10 +1101,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)) diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py index e5216b792f6b49..a0b01044b7d275 100644 --- a/Lib/sre_compile.py +++ b/Lib/sre_compile.py @@ -333,10 +333,7 @@ def _optimize_charset(charset, iscased=None, fixup=None, fixes=None): # compress character map runs = [] q = 0 - while True: - p = charmap.find(1, q) - if p < 0: - break + while (p := charmap.find(1, q)) >= 0: if len(runs) >= 2: runs = None break diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 3b0c3fa90498f5..c5369e8acde9c3 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -503,13 +503,8 @@ def _parse(source, state, verbose, nested, first=False): _len = len _ord = ord - while True: - - this = source.next - if this is None: - break # end of pattern - if this in "|)": - break # end of subpattern + # test for end of pattern or end of subpattern + while (this := source.next) is not None and (this not in "|)"): sourceget() if verbose: @@ -517,10 +512,8 @@ def _parse(source, state, verbose, nested, first=False): if this in WHITESPACE: continue if this == "#": - while True: - this = sourceget() - if this is None or this == "\n": - break + while (this := sourceget()) is not None and (this != "\n"): + pass continue if this[0] == "\\": @@ -986,10 +979,8 @@ def addgroup(index, pos): groups.append((len(literals), index)) literals.append(None) groupindex = pattern.groupindex - while True: - this = sget() - if this is None: - break # end of replacement string + # check for end of replacement string + while (this := sget()) is not None: if this[0] == "\\": # group c = this[1] diff --git a/Lib/sysconfig.py b/Lib/sysconfig.py index 0f7640eb674d63..1707bf7f977ccd 100644 --- a/Lib/sysconfig.py +++ b/Lib/sysconfig.py @@ -443,10 +443,7 @@ def parse_config_h(fp, vars=None): define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n") undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n") - while True: - line = fp.readline() - if not line: - break + while (line := fp.readline()): if (m := define_rx.match(line)): n, v = m.group(1, 2) try: diff --git a/Lib/tarfile.py b/Lib/tarfile.py index a00215da00fda9..a4f8c4f53cc0cb 100755 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -483,15 +483,11 @@ def _init_read_gz(self): xlen = ord(self.__read(1)) + 256 * ord(self.__read(1)) self.read(xlen) if flag & 8: - while True: - s = self.__read(1) - if not s or s == NUL: - break + while (s := self.__read(1)) and s != NUL: + pass if flag & 16: - while True: - s = self.__read(1) - if not s or s == NUL: - break + while (s := self.__read(1)) and (s != NUL): + pass if flag & 2: self.__read(2) @@ -520,10 +516,7 @@ def read(self, size=None): """ if size is None: t = [] - while True: - buf = self._read(self.bufsize) - if not buf: - break + while (buf := self._read(self.bufsize)): t.append(buf) buf = b"".join(t) else: @@ -997,10 +990,7 @@ def _create_pax_generic_header(cls, pax_headers, type, encoding): l = len(keyword) + len(value) + 3 # ' ' + '=' + '\n' n = p = 0 - while True: - n = l + len(str(p)) - if n == p: - break + while (n := l + len(str(p))) != p: p = n records += bytes(str(p), "ascii") + b" " + keyword + b"=" + value + b"\n" @@ -1218,11 +1208,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) value = buf[match.end(2) + 1:match.start(1) + length - 1] @@ -2338,10 +2324,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 (tarinfo := self.next()) is not None: + pass self._loaded = True def _check(self, mode=None): diff --git a/Lib/telnetlib.py b/Lib/telnetlib.py index baf9874536e0e2..b6d371b8f127cf 100644 --- a/Lib/telnetlib.py +++ b/Lib/telnetlib.py @@ -562,10 +562,7 @@ def mt_interact(self): """Multithreaded version of interact().""" import _thread _thread.start_new_thread(self.listener, ()) - while 1: - line = sys.stdin.readline() - if not line: - break + while (line := sys.stdin.readline()): self.write(line.encode('ascii')) def listener(self): diff --git a/Lib/xdrlib.py b/Lib/xdrlib.py index d6e1aeb527266a..71e613ccbd13ca 100644 --- a/Lib/xdrlib.py +++ b/Lib/xdrlib.py @@ -221,9 +221,7 @@ def unpack_string(self): def unpack_list(self, unpack_item): list = [] - while 1: - x = self.unpack_uint() - if x == 0: break + while (x := self.unpack_uint()) != 0: if x != 1: raise ConversionError('0 or 1 expected, got %r' % (x,)) item = unpack_item()