Skip to content

Commit 050a84b

Browse files
gh-49555: Clarify imaplib modified UTF-7 docs and comments (GH-153485)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 357c650 commit 050a84b

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
* The :meth:`~imaplib.IMAP4.copy`, :meth:`~imaplib.IMAP4.move`,

Lib/imaplib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ def _encode_mailbox(self, arg, safe):
16231623
# UTF-7 token -- one that needs no quoting, or an explicitly quoted
16241624
# string -- is passed through unchanged, so that a name obtained as raw
16251625
# bytes from LIST (and decoded as ASCII) round-trips without being
1626-
# encoded again. Pass bytes to bypass encoding entirely.
1626+
# encoded again. self._encoding is only ever 'ascii' or 'utf-8'.
16271627
if self._encoding != 'ascii':
16281628
return bytes(arg, self._encoding)
16291629
try:
@@ -1639,11 +1639,15 @@ def _encode_mailbox(self, arg, safe):
16391639
return arg.encode('utf-7-imap')
16401640

16411641
def _mailbox(self, arg):
1642+
# A str name is encoded for the wire; pass bytes to send the name
1643+
# verbatim, bypassing encoding entirely.
16421644
if isinstance(arg, str):
16431645
arg = self._encode_mailbox(arg, _non_astring_char)
16441646
return self._astring(arg)
16451647

16461648
def _list_mailbox(self, arg):
1649+
# As _mailbox(), but for a LIST/LSUB pattern; pass bytes to bypass
1650+
# encoding.
16471651
if isinstance(arg, str):
16481652
arg = self._encode_mailbox(arg, _non_list_char)
16491653
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)