Skip to content

Commit 073ed7d

Browse files
committed
gh-85014: Fix the description of email.utils.encode_rfc2231()
The docstring and the documentation said that the string "is returned as-is" when neither charset nor language is given. The function always percent-encodes the string, as RFC 2231 requires for characters such as the space. Without charset and language it only leaves out the charset'language' prefix. The behavior is correct and is not changed.
1 parent 0ada998 commit 073ed7d

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎Doc/library/email.utils.rst‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ of the new API.
203203
.. function:: encode_rfc2231(s, charset=None, language=None)
204204

205205
Encode the string *s* according to :rfc:`2231`. Optional *charset* and
206-
*language*, if given, are the character set name and language name to use. If
207-
neither is given, *s* is returned as-is. If *charset* is given but *language*
208-
is not, the string is encoded using the empty string for *language*.
206+
*language*, if given, are the character set name and language name to use.
207+
The string is always percent-encoded. If neither is given, the encoded
208+
string is returned without the character set and language prefix. If
209+
*charset* is given but *language* is not, the string is encoded using the
210+
empty string for *language*.
209211

210212

211213
.. function:: collapse_rfc2231_value(value, errors='replace', fallback_charset='us-ascii')

‎Lib/email/utils.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ def decode_rfc2231(s):
387387
def encode_rfc2231(s, charset=None, language=None):
388388
"""Encode string according to RFC 2231.
389389
390-
If neither charset nor language is given, then s is returned as-is. If
391-
charset is given but not language, the string is encoded using the empty
392-
string for language.
390+
The string is always percent-encoded. If neither charset nor language is
391+
given, then the encoded string is returned without the charset and
392+
language prefix. If charset is given but not language, the string is
393+
encoded using the empty string for language.
393394
"""
394395
s = urllib.parse.quote(s, safe='', encoding=charset or 'ascii')
395396
if charset is None and language is None:

0 commit comments

Comments
 (0)