Skip to content

Commit f4171c0

Browse files
committed
bpo-46943: fix[imaplib]: call Exception with string instance
Adjust the behavior similar to `authenticate()` where self.error is called with a str() instance. Especially for Python3 with strict bytes mode (-bb) this is helpful and prevents: Traceback (most recent call last): in "<stdin>" self.login(email, password) File "/usr/lib/python3.7/imaplib.py", line 598, in login raise self.error(dat[-1]) imaplib.error: <exception str() failed> During handling of the above exception, another exception occurred: Traceback (most recent call last): in "<stdin>" str(exc) BytesWarning: str() on a bytes instance
1 parent f3d877a commit f4171c0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Lib/imaplib.py

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

Lib/test/test_imaplib.py

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

418+
def test_invalid_login(self):
419+
class MyServer(SimpleIMAPHandler):
420+
def cmd_LOGIN(self, tag, args):
421+
self.server.logged = args[0]
422+
self._send_tagged(tag, 'NO', '[LOGIN] failed')
423+
client, _ = self._setup(MyServer)
424+
with self.assertRaisesRegex(imaplib.IMAP4.error,
425+
r'\[LOGIN\] failed'):
426+
client.login('user', 'wrongpass')
427+
418428
def test_valid_authentication_bytes(self):
419429
class MyServer(SimpleIMAPHandler):
420430
def cmd_AUTHENTICATE(self, tag, args):

0 commit comments

Comments
 (0)