]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91099: fix[imaplib]: call Exception with string instance (#31823)
authorFlorian Best <spaceone@users.noreply.github.com>
Tue, 2 Jun 2026 20:42:04 +0000 (22:42 +0200)
committerGitHub <noreply@github.com>
Tue, 2 Jun 2026 20:42:04 +0000 (16:42 -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.

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 2fafd9322c609eea0686ace234fec61838457dc8..497b5a60cecb083538dc1dd51a4bf0f3bc32fc26 100644 (file)
@@ -712,7 +712,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 0b704d62655762cb4154813fa3de9a1a77630ce1..fb256fb7cbcd344e11f0218d4cc38376561ce772 100644 (file)
@@ -434,6 +434,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.