Skip to content

Commit f9d2d7b

Browse files
gh-49555: Clarify imaplib modified UTF-7 docs and comments
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 6c389c4 commit f9d2d7b

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

Doc/whatsnew/3.16.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ imaplib
263263
(Contributed by Przemysław Buczkowski and Serhiy Storchaka in :gh:`89869`.)
264264

265265
* Non-ASCII mailbox names are now automatically encoded as modified UTF-7
266-
(:rfc:`3501`, section 5.1.3), so international mailbox names can be passed
267-
as ordinary :class:`str`.
266+
(:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
267+
names can be passed as ordinary :class:`str` without enabling ``UTF8=ACCEPT``
268+
(under which they are sent as UTF-8).
268269
(Contributed by Serhiy Storchaka in :gh:`49555`.)
269270

270271

Lib/imaplib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ def _encode_mailbox(self, arg, safe):
15521552
# UTF-7 token -- one that needs no quoting, or an explicitly quoted
15531553
# string -- is passed through unchanged, so that a name obtained as raw
15541554
# bytes from LIST (and decoded as ASCII) round-trips without being
1555-
# encoded again. Pass bytes to bypass encoding entirely.
1555+
# encoded again. self._encoding is only ever 'ascii' or 'utf-8'.
15561556
if self._encoding != 'ascii':
15571557
return bytes(arg, self._encoding)
15581558
try:
@@ -1568,11 +1568,15 @@ def _encode_mailbox(self, arg, safe):
15681568
return arg.encode('utf-7-imap')
15691569

15701570
def _mailbox(self, arg):
1571+
# A str name is encoded for the wire; pass bytes to send the name
1572+
# verbatim, bypassing encoding entirely.
15711573
if isinstance(arg, str):
15721574
arg = self._encode_mailbox(arg, _non_astring_char)
15731575
return self._astring(arg)
15741576

15751577
def _list_mailbox(self, arg):
1578+
# As _mailbox(), but for a LIST/LSUB pattern; pass bytes to bypass
1579+
# encoding.
15761580
if isinstance(arg, str):
15771581
arg = self._encode_mailbox(arg, _non_list_char)
15781582
if _quoted.fullmatch(arg):
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
:mod:`imaplib` now encodes non-ASCII mailbox names as modified UTF-7
2-
(:rfc:`3501`, section 5.1.3), so international mailbox names can be passed as
3-
ordinary :class:`str`. A ``str`` that is already valid modified UTF-7, or a
4-
:class:`bytes` object, is sent unchanged.
2+
(:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
3+
names can be passed as ordinary :class:`str`; under ``UTF8=ACCEPT`` they are
4+
sent as UTF-8 instead. A name that is not valid modified UTF-7, such as one
5+
with a bare ``&``, is now encoded correctly instead of being sent as is.
6+
A ``str`` that is already valid modified UTF-7, or a :class:`bytes` object,
7+
is sent unchanged.

0 commit comments

Comments
 (0)