From 85dec15ded1e00b6f5bd383e51f9d9946b8df1a7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 1 Jul 2026 13:07:20 +0300 Subject: [PATCH 1/2] gh-153417: Fix BytesWarning in imaplib error messages for bytes arguments IMAP4.select() and IMAP4.uid() formatted the mailbox and command argument with %s in their error messages, which raised BytesWarning under -bb when the argument was bytes and masked the real error. Use %r instead, which is safe for bytes and also quotes the value. Co-Authored-By: Claude Opus 4.8 --- Lib/imaplib.py | 4 +-- Lib/test/test_imaplib.py | 26 +++++++++++++++++++ ...-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 7b472c302fb061..143c5856fb9bed 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -960,7 +960,7 @@ def select(self, mailbox='INBOX', readonly=False): if __debug__: if self.debug >= 1: self._dump_ur(self.untagged_responses) - raise self.readonly('%s is not writable' % mailbox) + raise self.readonly('%r is not writable' % (mailbox,)) return typ, self.untagged_responses.get('EXISTS', [None]) @@ -1085,7 +1085,7 @@ def uid(self, command, *args): """ command = command.upper() if not command in Commands: - raise self.error("Unknown IMAP4 UID command: %s" % command) + raise self.error("Unknown IMAP4 UID command: %r" % (command,)) if self.state not in Commands[command]: raise self.error("command %s illegal in state %s, " "only allowed in states %s" % diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 30297fb5afcd19..e7ec0689236a27 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -995,6 +995,32 @@ def cmd_CAPABILITY(self, tag, args): client.login('user', 'pass') self.assertIn('ENABLE', client.capabilities) + def test_readonly_error_reports_mailbox(self): + # The read-only error reports the mailbox via repr(), which also + # avoids BytesWarning for a bytes mailbox under -bb. + class ReadOnlyHandler(SimpleIMAPHandler): + def cmd_SELECT(self, tag, args): + self._send_line(b'* 2 EXISTS') + self._send_tagged(tag, 'OK', '[READ-ONLY] SELECT completed.') + client, _ = self._setup(ReadOnlyHandler) + client.login('user', 'pass') + for mailbox, expected in [('INBOX', "'INBOX'"), (b'INBOX', r"b'INBOX'")]: + with self.subTest(mailbox=mailbox): + with self.assertRaisesRegex(imaplib.IMAP4.readonly, + r"%s is not writable" % expected): + client.select(mailbox) + + def test_uid_unknown_command_reports_command(self): + # The unknown-UID-command error reports the command via repr(), which + # also avoids BytesWarning for a bytes command under -bb. + client, _ = self._setup(SimpleIMAPHandler) + for command, expected in [('BOGUS', "'BOGUS'"), (b'BOGUS', r"b'BOGUS'")]: + with self.subTest(command=command): + with self.assertRaisesRegex( + imaplib.IMAP4.error, + r"Unknown IMAP4 UID command: %s" % expected): + client.uid(command, '1') + def test_logout(self): client, _ = self._setup(SimpleIMAPHandler) typ, data = client.login('user', 'pass') diff --git a/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst b/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst new file mode 100644 index 00000000000000..faee7b5c5ad183 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst @@ -0,0 +1,3 @@ +Error messages from :meth:`imaplib.IMAP4.select` and :meth:`imaplib.IMAP4.uid` +no longer raise :exc:`BytesWarning` under :option:`-bb` +when the mailbox or command argument is :class:`bytes`. From e14a56b146937de0bd5952855903e8492ec4a3db Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 9 Jul 2026 16:11:57 +0300 Subject: [PATCH 2/2] Fix broken -bb cross-reference in the NEWS entry There is no -bb option target (only .. option:: -b), so use :option:`!-bb` to keep the literal text without a dangling cross-reference. Co-Authored-By: Claude Opus 4.8 --- .../next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst b/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst index faee7b5c5ad183..ebc36fa7c228d0 100644 --- a/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst +++ b/Misc/NEWS.d/next/Library/2026-07-09-14-00-00.gh-issue-153417.Bw7Rq2.rst @@ -1,3 +1,3 @@ Error messages from :meth:`imaplib.IMAP4.select` and :meth:`imaplib.IMAP4.uid` -no longer raise :exc:`BytesWarning` under :option:`-bb` +no longer raise :exc:`BytesWarning` under :option:`!-bb` when the mailbox or command argument is :class:`bytes`.