Skip to content

Commit 2644102

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 0b20bff commit 2644102

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
@@ -703,7 +703,7 @@ def login(self, user, password):
703703
"""
704704
typ, dat = self._simple_command('LOGIN', user, self._quote(password))
705705
if typ != 'OK':
706-
raise self.error(dat[-1])
706+
raise self.error(dat[-1].decode('UTF-8', 'replace'))
707707
self.state = 'AUTH'
708708
return typ, dat
709709

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):

0 commit comments

Comments
 (0)