diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 50771e8c17c250..06be1d66bbde51 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -787,24 +787,24 @@ def abort(self): all_errors = (Error, OSError, EOFError, ssl.SSLError) -_150_re = None - def parse150(resp): '''Parse the '150' response for a RETR request. Returns the expected transfer size or None; size is not guaranteed to be present in the 150 message. ''' - if resp[:3] != '150': + if not resp.startswith('150'): raise error_reply(resp) - global _150_re - if _150_re is None: - import re - _150_re = re.compile( - r"150 .* \((\d+) bytes\)", re.IGNORECASE | re.ASCII) - m = _150_re.match(resp) - if not m: + + start = resp.find('(') + end = resp.lower().find(' bytes)') + + if start == -1 or end == -1 or start >= end: + return None + + try: + return int(resp[start + 1:end]) + except ValueError: return None - return int(m.group(1)) _227_re = None diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst new file mode 100644 index 00000000000000..31d6799dff8742 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst @@ -0,0 +1,2 @@ +Improve speed of :func:`ftplib.parse150` by replacing :mod:`re` with +:func:`find` method. Patch by Semyon Moroz.