-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
bpo-34002: Minor efficiency and clarity improvements in email package. #7999
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
Changes from 1 commit
d8bc2c0
51e14f8
c6168d1
b7e4749
4c61d82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Copying the list, then repeatedly popping the first element was unnecessarily slow. I also cleaned up a couple other inefficiencies. There's no need to unpack a tuple, then re-pack and append it. The list can be created with the first element instead of empty. Secondly, the ``endswith`` method returns a bool, so there's no need for an if- statement to set ``encoding`` to True or False.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,21 +259,13 @@ def decode_params(params): | |
|
||
params is a sequence of 2-tuples containing (param name, string value). | ||
""" | ||
# Copy params so we don't mess with the original | ||
params = params[:] | ||
new_params = [] | ||
new_params = [params[0]] | ||
# Map parameter's name to a list of continuations. The values are a | ||
# 3-tuple of the continuation number, the string value, and a flag | ||
# specifying whether a particular segment is %-encoded. | ||
rfc2231_params = {} | ||
name, value = params.pop(0) | ||
new_params.append((name, value)) | ||
while params: | ||
name, value = params.pop(0) | ||
if name.endswith('*'): | ||
encoded = True | ||
else: | ||
encoded = False | ||
for name, value in params[1:]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. The real benefit is avoiding repeated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the code can be made more optimal if avoid a copy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks to me that the final value of 'encoded' only depends on the final value of name. If so, the loop is equivalent to
Otherwise, one can skip-iterate without copying with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each iteration of the loop uses the
|
||
encoded = name.endswith('*') | ||
value = unquote(value) | ||
mo = rfc2231_continuation.match(name) | ||
if mo: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
params[0]
is not a 2-tuple?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's possible for the first params element, though the others are implicitly checked by the unpacking for-loop.
Looking at the usage of
decode_params
, it's only use is inemail.message._get_params_preserve
which in turn has 3 call sites. All 3 calls unpack the elements as(k, v)
pairs. The bad data would cause an immediate error, given how the method is used. The error would be the same as today, though the traceback would not be as deep.If that's not satisfactory, I'll revert to: