8000 #23040: Clarify treatment of encoding and errors when component is by… · python/cpython@8c4e112 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c4e112

Browse files
committed
#23040: Clarify treatment of encoding and errors when component is bytes.
Patch by Wojtek Ruszczewski.
1 parent a54f075 commit 8c4e112

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

Doc/library/urllib.parse.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ task isn't already covered by the URL parsing functions above.
517517
.. function:: urlencode(query, doseq=False, safe='', encoding=None, errors=None)
518518

519519
Convert a mapping object or a sequence of two-element tuples, which may
520-
either be a :class:`str` or a :class:`bytes`, to a "percent-encoded"
520+
contain :class:`str` or :class:`bytes` objects, to a "percent-encoded"
521521
string. If the resultant string is to be used as a *data* for POST
522522
operation with :func:`~urllib.request.urlopen` function, then it should be
523523
properly encoded to bytes, otherwise it would result in a :exc:`TypeError`.
@@ -532,8 +532,9 @@ task isn't already covered by the URL parsing functions above.
532532
the value sequence for the key. The order of parameters in the encoded
533533
string will match the order of parameter tuples in the sequence.
534534

535-
When *query* parameter is a :class:`str`, the *safe*, *encoding* and *error*
536-
parameters are passed down to :func:`quote_plus` for encoding.
535+
The *safe*, *encoding*, and *errors* parameters are passed down to
536+
:func:`quote_plus` (the *encoding* and *errors* parameters are only passed
537+
when a query element is a :class:`str`).
537538

538539
To reverse this encoding process, :func:`parse_qs` and :func:`parse_qsl` are
539540
provided in this module to parse query strings into Python data structures.

Lib/urllib/parse.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ def quote(string, safe='/', encoding=None, errors=None):
670670
called on a path where the existing slash characters are used as
671671
reserved characters.
672672
673-
string and safe may be either str or bytes objects. encoding must
674-
not be specified if string is a str.
673+
string and safe may be either str or bytes objects. encoding and errors
674+
must not be specified if string is a bytes object.
675675
676676
The optional encoding and errors parameters specify how to deal with
677677
non-ASCII characters, as accepted by the str.encode method.
@@ -743,8 +743,9 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None):
743743
input.
744744
745745
The components of a query arg may each be either a string or a bytes type.
746-
When a component is a string, the safe, encoding and error parameters are
747-
sent to the quote_plus function for encoding.
746+
747+
The safe, encoding, and errors parameters are passed down to quote_plus()
748+
(encoding and errors only if a component is a str).
748749
"""
749750

750751
if hasattr(query, "items"):

0 commit comments

Comments
 (0)
0