From 4055dbed316e0a10b71ba6a8876d16fde8a0b98b Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 04:55:53 +0400 Subject: [PATCH 1/5] Improve speed of ftplib.parse150 by replacing re --- Lib/ftplib.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 50771e8c17c250..459c8506d8bccb 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.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 From 043de739b6c67a01d2152ad8e94484ec7f31e6db Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 05:00:30 +0400 Subject: [PATCH 2/5] Add blurb NEWS.d entry --- .../next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst 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..eae82e0d1c5b37 --- /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 +*replace* method. Patch by Semyon Moroz. From 8ad9c5e556b1bd68e470e182cecc104dc518e003 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 05:06:22 +0400 Subject: [PATCH 3/5] Correct blurb entry file --- .../next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index eae82e0d1c5b37..e40edfdd850a38 100644 --- 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 @@ -1,2 +1,2 @@ Improve speed of :func:`ftplib.parse150` by replacing :mod:`re` with -*replace* method. Patch by Semyon Moroz. +``.find()`` method. Patch by Semyon Moroz. From e5dd0276f9086e28344328505937575408e9d3b9 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 16:24:05 +0400 Subject: [PATCH 4/5] Ignore case while finding --- Lib/ftplib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 459c8506d8bccb..06be1d66bbde51 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -796,7 +796,7 @@ def parse150(resp): raise error_reply(resp) start = resp.find('(') - end = resp.find(' bytes)') + end = resp.lower().find(' bytes)') if start == -1 or end == -1 or start >= end: return None From db0a29a00a4a0cc6bf101a17f4b38fc96ffeab28 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 21:52:38 +0400 Subject: [PATCH 5/5] Update NEWS.d entry markup --- .../next/Library/2025-02-18-05-00-24.gh-issue-130167.e0MXUp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index e40edfdd850a38..31d6799dff8742 100644 --- 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 @@ -1,2 +1,2 @@ Improve speed of :func:`ftplib.parse150` by replacing :mod:`re` with -``.find()`` method. Patch by Semyon Moroz. +:func:`find` method. Patch by Semyon Moroz.