Skip to content

Commit 29805f0

Browse files
pythongh-91099: fix[imaplib]: call Exception with string instance (python#31823)
* 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>
1 parent 50fe49c commit 29805f0

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/imaplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ def login(self, user, password):
712712
"""
713713
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
714714
if typ != 'OK':
715-
raise self.error(dat[-1])
715+
raise self.error(dat[-1].decode('UTF-8', 'replace'))
716716
self.state = 'AUTH'
717717
return typ, dat
718718

Lib/test/test_imaplib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ def cmd_AUTHENTICATE(self, tag, args):
434434
r'\[AUTHENTICATIONFAILED\] invalid'):
435435
client.authenticate('MYAUTH', lambda x: b'fake')
436436

437+
def test_invalid_login(self):
438+
class MyServer(SimpleIMAPHandler):
439+
def cmd_LOGIN(self, tag, args):
440+
self.server.logged = args[0]
441+
self._send_tagged(tag, 'NO', '[LOGIN] failed')
442+
client, _ = self._setup(MyServer)
443+
with self.assertRaisesRegex(imaplib.IMAP4.error,
444+
r'\[LOGIN\] failed'):
445+
client.login('user', 'wrongpass')
446+
437447
def test_valid_authentication_bytes(self):
438448
class MyServer(SimpleIMAPHandler):
439449
def cmd_AUTHENTICATE(self, tag, args):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`imaplib.IMAP4.login` now raises exceptions with :class:`str` instead of
2+
:class:`bytes`. Patch by Florian Best.

0 commit comments

Comments
 (0)