Skip to content

Commit 71e8132

Browse files
committed
bpo-12910: update and correct quote docstring
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
1 parent fae8f4a commit 71e8132

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
@@ -746,25 +746,32 @@ def quote(string, safe='/', encoding=None, errors=None):
746746
"""quote('abc def') -> 'abc%20def'
747747
748748
Each part of a URL, e.g. the path info, the query, etc., has a
749-
different set of reserved characters that must be quoted.
749+
different set of reserved characters that must be quoted. The
750+
quote function offers a cautious (not minimal) way to quote a
751+
string for most of these parts.
750752
751-
RFC 3986 Uniform Resource Identifiers (URI): Generic Syntax lists
752-
the following reserved characters.
753+
RFC 3986 Uniform Resource Identifier (URI): Generic Syntax lists
754+
the following (un)reserved characters.
753755
754-
reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
755-
"$" | "," | "~"
756+
unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
757+
reserved = gen-delims / sub-delims
758+
gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
759+
sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
760+
/ "*" / "+" / "," / ";" / "="
756761
757-
Each of these characters is reserved in some component of a URL,
762+
Each of the reserved characters is reserved in some component of a URL,
758763
but not necessarily in all of them.
759764
760-
Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings.
761-
Now, "~" is included in the set of reserved characters.
765+
The quote function %-escapes all characters that are neither in the
766+
unreserved chars ("always safe") nor the additional chars set via the
767+
safe arg.
768+
769+
The default for the safe arg is '/'. The character is reserved, but in
770+
typical usage the quote function is being called on a path where the
771+
existing slash characters are to be preserved.
762772
763-
By default, the quote function is intended for quoting the path
764-
section of a URL. Thus, it will not encode '/'. This character
765-
is reserved, but in typical usage the quote function is being
766-
called on a path where the existing slash characters are used as
767-
reserved characters.
773+
Python 3.7 updates from using RFC 2396 to RFC 3986 to quote URL strings.
774+
Now, "~" is included in the set of unreserved characters.
768775
769776
string and safe may be either str or bytes objects. encoding and errors
770777
must not be specified if string is a bytes object.

0 commit comments

Comments
 (0)