]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-91099: fix[imaplib]: call Exception with string instance (GH-31823) (#150809)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 3 Jun 2026 13:12:00 +0000 (15:12 +0200)
committerGitHub <noreply@github.com>
Wed, 3 Jun 2026 13:12:00 +0000 (09:12 -0400)
* 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 <spaceone@users.noreply.github.com>
Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Lib/imaplib.py
Lib/test/test_imaplib.py
Misc/NEWS.d/next/Library/2023-02-26-14-07-18.gh-issue-91099._QPbEL.rst [new file with mode: 0644]

index 141e639894487381b583ac97b0c6af5894a1adbf..db16f3c802c6c3bd87fdf50a2d3e70b8592b1310 100644 (file)
@@ -616,7 +616,7 @@ class IMAP4:
         """
         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
 
index 9f1f682d02e8ef4daa8156a4a5ecb1061465e134..6573b4e7f304919710a416f78fb78e9536f78d11 100644 (file)
@@ -387,6 +387,16 @@ class NewIMAPTestsMixin:
                 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 (file)
index 0000000..d886e8a
--- /dev/null
@@ -0,0 +1,2 @@
+:meth:`imaplib.IMAP4.login` now raises exceptions with :class:`str` instead of
+:class:`bytes`. Patch by Florian Best.