summaryrefslogtreecommitdiff
path: root/Lib/email/utils.py
diff options
context:
space:
mode:
authorMichael Selik <mike@selik.org>2019-09-19 20:25:55 -0700
committerAbhilash Raj <maxking@users.noreply.github.com>2019-09-19 20:25:55 -0700
commit2702638eabe5f7b25f36d295f0ad78cb8d4eda05 (patch)
tree1231f4f3c8ca7667c98aafd33136dab024c980a0 /Lib/email/utils.py
parent3368f3c6ae4140a0883e19350e672fd09c9db616 (diff)
downloadcpython-git-2702638eabe5f7b25f36d295f0ad78cb8d4eda05.tar.gz
bpo-34002: Minor efficiency and clarity improvements in email package. (GH-7999)
* Check intersection of two sets explicitly Comparing ``len(a) > ``len(a - b)`` is essentially looking for an intersection between the two sets. If set ``b`` does not intersect ``a`` then ``len(a - b)`` will be equal to ``len(a)``. This logic is more clearly expressed as ``a & b``. * Change while/pop to a for-loop 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. * Use set.intersection to check for intersections ``a.intersection(b)`` method is more clear of purpose than ``not a.isdisjoint(b)`` and avoids an unnecessary set construction that ``a & set(b)`` performs. * Use not isdisjoint instead of intersection While it reads slightly worse, the isdisjoint method will stop when it finds a counterexample and returns a bool, rather than looping over the entire iterable and constructing a new set.
Diffstat (limited to 'Lib/email/utils.py')
-rw-r--r--Lib/email/utils.py14
1 files changed, 3 insertions, 11 deletions
diff --git a/Lib/email/utils.py b/Lib/email/utils.py
index 858f620e25..b137ce3973 100644
--- a/Lib/email/utils.py
+++ b/Lib/email/utils.py
@@ -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:]:
+ encoded = name.endswith('*')
value = unquote(value)
mo = rfc2231_continuation.match(name)
if mo: