Created
September 22, 2013 02:35
-
-
Save syllog1sm/6656171 to your computer and use it in GitHub Desktop.
The implementation of urllib.quote, as of Python2.7.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_safe_quoters = {} | |
always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
'abcdefghijklmnopqrstuvwxyz' | |
'0123456789' '_.-') | |
_safe_map = {} | |
for i, c in zip(xrange(256), str(bytearray(xrange(256)))): | |
_safe_map[c] = c if (i < 128 and c in always_safe) else '%{:02X}'.format(i) | |
def quote(s, safe='/'): | |
"""quote('abc def') -> 'abc%20def' | |
Each part of a URL, e.g. the path info, the query, etc., has a | |
different set of reserved characters that must be quoted. | |
RFC 2396 Uniform Resource Identifiers (URI): Generic Syntax lists | |
the following reserved characters. | |
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | | |
"$" | "," | |
Each of these characters is reserved in some component of a URL, | |
but not necessarily in all of them. | |
By default, the quote function is intended for quoting the path | |
section of a URL. Thus, it will not encode '/'. This character | |
is reserved, but in typical usage the quote function is being | |
called on a path where the existing slash characters are used as | |
reserved characters. | |
""" | |
# fastpath | |
if not s: | |
if s is None: | |
raise TypeError('None object cannot be quoted') | |
return s | |
cachekey = (safe, always_safe) | |
try: | |
(quoter, safe) = _safe_quoters[cachekey] | |
except KeyError: | |
safe_map = _safe_map.copy() | |
safe_map.update([(c, c) for c in safe]) | |
quoter = safe_map.__getitem__ | |
safe = always_safe + safe | |
_safe_quoters[cachekey] = (quoter, safe) | |
if not s.rstrip(safe): | |
return s | |
return ''.join(map(quoter, s)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment