bpo-36348: IMAP4.logout() doesn't ignore exc (GH-12411) · python/cpython@74125a6

@@ -272,6 +272,9 @@ def __enter__(self):

272272

return self

273273274274

def __exit__(self, *args):

275+

if self.state == "LOGOUT":

276+

return

277+275278

try:

276279

self.logout()

277280

except OSError:

@@ -625,11 +628,8 @@ def logout(self):

625628

Returns server 'BYE' response.

626629

"""

627630

self.state = 'LOGOUT'

628-

try: typ, dat = self._simple_command('LOGOUT')

629-

except: typ, dat = 'NO', ['%s: %s' % sys.exc_info()[:2]]

631+

typ, dat = self._simple_command('LOGOUT')

630632

self.shutdown()

631-

if 'BYE' in self.untagged_responses:

632-

return 'BYE', self.untagged_responses['BYE']

633633

return typ, dat

634634635635

@@ -1012,16 +1012,17 @@ def _command(self, name, *args):

101210121013101310141014

def _command_complete(self, name, tag):

1015+

logout = (name == 'LOGOUT')

10151016

# BYE is expected after LOGOUT

1016-

if name != 'LOGOUT':

1017+

if not logout:

10171018

self._check_bye()

10181019

try:

1019-

typ, data = self._get_tagged_response(tag)

1020+

typ, data = self._get_tagged_response(tag, expect_bye=logout)

10201021

except self.abort as val:

10211022

raise self.abort('command: %s => %s' % (name, val))

10221023

except self.error as val:

10231024

raise self.error('command: %s => %s' % (name, val))

1024-

if name != 'LOGOUT':

1025+

if not logout:

10251026

self._check_bye()

10261027

if typ == 'BAD':

10271028

raise self.error('%s command error: %s %s' % (name, typ, data))

@@ -1117,17 +1118,23 @@ def _get_response(self):

11171118

return resp

11181119111911201120-

def _get_tagged_response(self, tag):

1121+

def _get_tagged_response(self, tag, expect_bye=False):

1121112211221123

while 1:

11231124

result = self.tagged_commands[tag]

11241125

if result is not None:

11251126

del self.tagged_commands[tag]

11261127

return result

112711281129+

if expect_bye:

1130+

typ = 'BYE'

1131+

bye = self.untagged_responses.pop(typ, None)

1132+

if bye is not None:

1133+

# Server replies to the "LOGOUT" command with "BYE"

1134+

return (typ, bye)

1135+11281136

# If we've seen a BYE at this point, the socket will be

11291137

# closed, so report the BYE now.

1130-11311138

self._check_bye()

1132113911331140

# Some have reported "unexpected response" exceptions.