https://github.com/python/cpython/commit/d5ebac697f3b3d364680ee1e99dd4555a230594c
commit: d5ebac697f3b3d364680ee1e99dd4555a230594c
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: bitdancer <[email protected]>
date: 2026-06-03T09:12:27-04:00
summary:

[3.14] gh-91099: fix[imaplib]: call Exception with string instance (GH-31823) 
(#150810)

* bpo-46943: fix[imaplib]: call Exception with string instance

Adjust the behavior of 'login' to be similar to `authenticate()`,
where self.error is called with a str() instance.
(cherry picked from commit 29805f00a1b65163230d17584c30e2b955086abb)

Co-authored-by: Florian Best <[email protected]>
Co-authored-by: Oleg Iarygin <[email protected]>

files:
A Misc/NEWS.d/next/Library/2023-02-26-14-07-18.gh-issue-91099._QPbEL.rst
M Lib/imaplib.py
M Lib/test/test_imaplib.py

diff --git a/Lib/imaplib.py b/Lib/imaplib.py
index cbe129b3e7c2145..e84ffb2eecfbd38 100644
--- a/Lib/imaplib.py
+++ b/Lib/imaplib.py
@@ -706,7 +706,7 @@ def login(self, user, password):
         """
         typ, dat = self._simple_command('LOGIN', user, self._quote(password))
         if typ != 'OK':
-            raise self.error(dat[-1])
+            raise self.error(dat[-1].decode('UTF-8', 'replace'))
         self.state = 'AUTH'
         return typ, dat
 
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
index a03d7b8bb2a42c1..f0b463949cad2f9 100644
--- a/Lib/test/test_imaplib.py
+++ b/Lib/test/test_imaplib.py
@@ -435,6 +435,16 @@ def cmd_AUTHENTICATE(self, tag, args):
                 r'\[AUTHENTICATIONFAILED\] invalid'):
             client.authenticate('MYAUTH', lambda x: b'fake')
 
+    def test_invalid_login(self):
+        class MyServer(SimpleIMAPHandler):
+            def cmd_LOGIN(self, tag, args):
+                self.server.logged = args[0]
+                self._send_tagged(tag, 'NO', '[LOGIN] failed')
+        client, _ = self._setup(MyServer)
+        with self.assertRaisesRegex(imaplib.IMAP4.error,
+                r'\[LOGIN\] failed'):
+            client.login('user', 'wrongpass')
+
     def test_valid_authentication_bytes(self):
         class MyServer(SimpleIMAPHandler):
             def cmd_AUTHENTICATE(self, tag, args):
diff --git 
a/Misc/NEWS.d/next/Library/2023-02-26-14-07-18.gh-issue-91099._QPbEL.rst 
b/Misc/NEWS.d/next/Library/2023-02-26-14-07-18.gh-issue-91099._QPbEL.rst
new file mode 100644
index 000000000000000..d886e8ac6032a4a
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-02-26-14-07-18.gh-issue-91099._QPbEL.rst
@@ -0,0 +1,2 @@
+:meth:`imaplib.IMAP4.login` now raises exceptions with :class:`str` instead of
+:class:`bytes`. Patch by Florian Best.

_______________________________________________
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