MCPcopy Index your code
hub / github.com/python/cpython / quote

Function quote

Lib/urllib/parse.py:1020–1072  ·  view source on GitHub ↗

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. The quote function offers a cautious (not minimal) way to quote a string for most of these parts. RFC 3986 Uniform Resource I

(string, safe='/', encoding=None, errors=None)

Source from the content-addressed store, hash-verified

1018 return res
1019
1020def quote(string, safe='/', encoding=None, errors=None):
1021 """quote('abc def') -> 'abc%20def'
1022
1023 Each part of a URL, e.g. the path info, the query, etc., has a
1024 different set of reserved characters that must be quoted. The
1025 quote function offers a cautious (not minimal) way to quote a
1026 string for most of these parts.
1027
1028 RFC 3986 Uniform Resource Identifier (URI): Generic Syntax lists
1029 the following (un)reserved characters.
1030
1031 unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
1032 reserved = gen-delims / sub-delims
1033 gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
1034 sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
1035 / "*" / "+" / "," / ";" / "="
1036
1037 Each of the reserved characters is reserved in some component of a URL,
1038 but not necessarily in all of them.
1039
1040 The quote function %-escapes all characters that are neither in the
1041 unreserved chars ("always safe") nor the additional chars set via the
1042 safe arg.
1043
1044 The default for the safe arg is '/'. The character is reserved, but in
1045 typical usage the quote function is being called on a path where the
1046 existing slash characters are to be preserved.
1047
1048 Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings.
1049 Now, "~" is included in the set of unreserved characters.
1050
1051 string and safe may be either str or bytes objects. encoding and errors
1052 must not be specified if string is a bytes object.
1053
1054 The optional encoding and errors parameters specify how to deal with
1055 non-ASCII characters, as accepted by the str.encode method.
1056 By default, encoding='utf-8' (characters are encoded with UTF-8), and
1057 errors='strict' (unsupported characters raise a UnicodeEncodeError).
1058 """
1059 if isinstance(string, str):
1060 if not string:
1061 return string
1062 if encoding is None:
1063 encoding = 'utf-8'
1064 if errors is None:
1065 errors = 'strict'
1066 string = string.encode(encoding, errors)
1067 else:
1068 if encoding is not None:
1069 raise TypeError("quote() doesn't support 'encoding' for bytes")
1070 if errors is not None:
1071 raise TypeError("quote() doesn't support 'errors' for bytes")
1072 return quote_from_bytes(string, safe)
1073
1074def quote_plus(string, safe='', encoding=None, errors=None):
1075 """Like quote(), but also replace ' ' with '+', as required for quoting

Callers 5

application_uriFunction · 0.90
request_uriFunction · 0.90
http_error_302Method · 0.90
pathname2urlFunction · 0.90
quote_plusFunction · 0.70

Calls 2

quote_from_bytesFunction · 0.85
encodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…