Skip to content

Commit 796698a

Browse files
bpo-12910: update and correct quote docstring (GH-2568)
Fixes some mistakes and misleadings in the quote function docstring: - reserved chars are never actually used by quote code, unreserved chars are - reserved chars were wrong and incomplete - mentioned that use-case is not minimal quoting wrt. RFC, but cautious quoting (cherry picked from commit 750d74f) Co-authored-by: Jörn Hees <[email protected]>
1 parent 86f0354 commit 796698a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

Lib/urllib/parse.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -780,25 +780,32 @@ def quote(string, safe='/', encoding=None, errors=None):
780780
"""quote('abc def') -> 'abc%20def'
781781
782782
Each part of a URL, e.g. the path info, the query, etc., has a
783-
different set of reserved characters that must be quoted.
783+
different set of reserved characters that must be quoted. The
784+
quote function offers a cautious (not minimal) way to quote a
785+
string for most of these parts.
784786
785-
RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax lists
786-
the following reserved characters.
787+
RFC 3986 Uniform Resource Identifier (URI): Generic Syntax lists
788+
the following (un)reserved characters.
787789
788-
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
789-
"$" | "," | "~"
790+
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
791+
reserved = gen-delims / sub-delims
792+
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
793+
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
794+
/ "*" / "+" / "," / ";" / "="
790795
791-
Each of these characters is reserved in some component of a URL,
796+
Each of the reserved characters is reserved in some component of a URL,
792797
but not necessarily in all of them.
793798
794-
Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings.
795-
Now, "~" is included in the set of reserved characters.
799+
The quote function %-escapes all characters that are neither in the
800+
unreserved chars ("always safe") nor the additional chars set via the
801+
safe arg.
802+
803+
The default for the safe arg is '/'. The character is reserved, but in
804+
typical usage the quote function is being called on a path where the
805+
existing slash characters are to be preserved.
796806
797-
By default, the quote function is intended for quoting the path
798-
section of a URL. Thus, it will not encode '/'. This character
799-
is reserved, but in typical usage the quote function is being
800-
called on a path where the existing slash characters are used as
801-
reserved characters.
807+
Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings.
808+
Now, "~" is included in the set of unreserved characters.
802809
803810
string and safe may be either str or bytes objects. encoding and errors
804811
must not be specified if string is a bytes object.

0 commit comments

Comments
 (0)