https://github.com/python/cpython/commit/050a84bbc047bee089506104f370e0e6068aefeb
commit: 050a84bbc047bee089506104f370e0e6068aefeb
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-07-14T15:47:32+03:00
summary:

gh-49555: Clarify imaplib modified UTF-7 docs and comments (GH-153485)

Co-authored-by: Claude Opus 4.8 <[email protected]>

files:
M Doc/whatsnew/3.16.rst
M Lib/imaplib.py
M Misc/NEWS.d/next/Library/2026-07-09-12-00-00.gh-issue-49555.Ka9Lm2.rst

diff --git a/Doc/whatsnew/3.16.rst b/Doc/whatsnew/3.16.rst
index da98f1ad6b9bc3b..fad723ba3f4cb63 100644
--- a/Doc/whatsnew/3.16.rst
+++ b/Doc/whatsnew/3.16.rst
@@ -263,8 +263,9 @@ imaplib
   (Contributed by Przemysław Buczkowski and Serhiy Storchaka in :gh:`89869`.)
 
 * Non-ASCII mailbox names are now automatically encoded as modified UTF-7
-  (:rfc:`3501`, section 5.1.3), so international mailbox names can be passed
-  as ordinary :class:`str`.
+  (:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
+  names can be passed as ordinary :class:`str` without enabling ``UTF8=ACCEPT``
+  (under which they are sent as UTF-8).
   (Contributed by Serhiy Storchaka in :gh:`49555`.)
 
 * The :meth:`~imaplib.IMAP4.copy`, :meth:`~imaplib.IMAP4.move`,
diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index ed5b528976448a7..538a858b33e8ae3 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -1623,7 +1623,7 @@ def _encode_mailbox(self, arg, safe):
         # UTF-7 token -- one that needs no quoting, or an explicitly quoted
         # string -- is passed through unchanged, so that a name obtained as raw
         # bytes from LIST (and decoded as ASCII) round-trips without being
-        # encoded again.  Pass bytes to bypass encoding entirely.
+        # encoded again.  self._encoding is only ever 'ascii' or 'utf-8'.
         if self._encoding != 'ascii':
             return bytes(arg, self._encoding)
         try:
@@ -1639,11 +1639,15 @@ def _encode_mailbox(self, arg, safe):
         return arg.encode('utf-7-imap')
 
     def _mailbox(self, arg):
+        # A str name is encoded for the wire; pass bytes to send the name
+        # verbatim, bypassing encoding entirely.
         if isinstance(arg, str):
             arg = self._encode_mailbox(arg, _non_astring_char)
         return self._astring(arg)
 
     def _list_mailbox(self, arg):
+        # As _mailbox(), but for a LIST/LSUB pattern; pass bytes to bypass
+        # encoding.
         if isinstance(arg, str):
             arg = self._encode_mailbox(arg, _non_list_char)
         if _quoted.fullmatch(arg):
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-09-12-00-00.gh-issue-49555.Ka9Lm2.rst 
b/Misc/NEWS.d/next/Library/2026-07-09-12-00-00.gh-issue-49555.Ka9Lm2.rst
index 87236e98a079015..56860e53db88627 100644
--- a/Misc/NEWS.d/next/Library/2026-07-09-12-00-00.gh-issue-49555.Ka9Lm2.rst
+++ b/Misc/NEWS.d/next/Library/2026-07-09-12-00-00.gh-issue-49555.Ka9Lm2.rst
@@ -1,4 +1,7 @@
 :mod:`imaplib` now encodes non-ASCII mailbox names as modified UTF-7
-(:rfc:`3501`, section 5.1.3), so international mailbox names can be passed as
-ordinary :class:`str`.  A ``str`` that is already valid modified UTF-7, or a
-:class:`bytes` object, is sent unchanged.
+(:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
+names can be passed as ordinary :class:`str`; under ``UTF8=ACCEPT`` they are
+sent as UTF-8 instead.  A name that is not valid modified UTF-7, such as one
+with a bare ``&``, is now encoded correctly instead of being sent as is.
+A ``str`` that is already valid modified UTF-7, or a :class:`bytes` object,
+is sent unchanged.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to