Skip to content

Commit d5ebac6

Browse files
miss-islingtonspaceonearhadthedev
authored
[3.14] gh-91099: fix[imaplib]: call Exception with string instance (GH-31823) (#150810)
* 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 29805f0) Co-authored-by: Florian Best <spaceone@users.noreply.github.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
1 parent e7a9369 commit d5ebac6

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

Lib/test/test_imaplib.py

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

438+
def test_invalid_login(self):
439+
class MyServer(SimpleIMAPHandler):
440+
def cmd_LOGIN(self, tag, args):
441+
self.server.logged = args[0]
442+
self._send_tagged(tag, 'NO', '[LOGIN] failed')
443+
client, _ = self._setup(MyServer)
444+
with self.assertRaisesRegex(imaplib.IMAP4.error,
445+
r'\[LOGIN\] failed'):
446+
client.login('user', 'wrongpass')
447+
438448
def test_valid_authentication_bytes(self):
439449
class MyServer(SimpleIMAPHandler):
440450
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)