Skip to content

gh-130167: Improve speed of ftplib.parse150 by replacing re #130243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Lib/ftplib.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve speed of :func:`ftplib.parse150` by replacing :mod:`re` with
:func:`find` method. Patch by Semyon Moroz.