Kaydet (Commit) f819886a authored tarafından Jeremy Hylton's avatar Jeremy Hylton

Simplify a few complicated expressions.

üst a4de60a1
...@@ -479,12 +479,17 @@ def quote_plus(string, safe='', encoding=None, errors=None): ...@@ -479,12 +479,17 @@ def quote_plus(string, safe='', encoding=None, errors=None):
HTML form values. Plus signs in the original string are escaped unless HTML form values. Plus signs in the original string are escaped unless
they are included in safe. It also does not have safe default to '/'. they are included in safe. It also does not have safe default to '/'.
""" """
# Check if ' ' in string, where string may either be a str or bytes # Check if ' ' in string, where string may either be a str or bytes. If
if ' ' in string if isinstance(string, str) else b' ' in string: # there are no spaces, the regular quote will produce the right answer.
string = quote(string, if ((isinstance(string, str) and ' ' not in string) or
safe + ' ' if isinstance(safe, str) else safe + b' ') (isinstance(string, bytes) and b' ' not in string)):
return string.replace(' ', '+') return quote(string, safe, encoding, errors)
return quote(string, safe, encoding, errors) if isinstance(safe, str):
space = ' '
else:
space = b' '
string = quote(string, safe + space)
return string.replace(' ', '+')
def quote_from_bytes(bs, safe='/'): def quote_from_bytes(bs, safe='/'):
"""Like quote(), but accepts a bytes object rather than a str, and does """Like quote(), but accepts a bytes object rather than a str, and does
...@@ -502,7 +507,7 @@ def quote_from_bytes(bs, safe='/'): ...@@ -502,7 +507,7 @@ def quote_from_bytes(bs, safe='/'):
except KeyError: except KeyError:
quoter = Quoter(safe) quoter = Quoter(safe)
_safe_quoters[cachekey] = quoter _safe_quoters[cachekey] = quoter
return ''.join(map(quoter.__getitem__, bs)) return ''.join([quoter[char] for char in bs])
def urlencode(query, doseq=0): def urlencode(query, doseq=0):
"""Encode a sequence of two-element tuples or dictionary into a URL query string. """Encode a sequence of two-element tuples or dictionary into a URL query string.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment