bpo-36348: IMAP4.logout() doesn't ignore exc (GH-12411) · python/cpython@74125a6
@@ -272,6 +272,9 @@ def __enter__(self):
272272return self
273273274274def __exit__(self, *args):
275+if self.state == "LOGOUT":
276+return
277+275278try:
276279self.logout()
277280except OSError:
@@ -625,11 +628,8 @@ def logout(self):
625628 Returns server 'BYE' response.
626629 """
627630self.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')
630632self.shutdown()
631-if 'BYE' in self.untagged_responses:
632-return 'BYE', self.untagged_responses['BYE']
633633return typ, dat
634634635635@@ -1012,16 +1012,17 @@ def _command(self, name, *args):
101210121013101310141014def _command_complete(self, name, tag):
1015+logout = (name == 'LOGOUT')
10151016# BYE is expected after LOGOUT
1016-if name != 'LOGOUT':
1017+if not logout:
10171018self._check_bye()
10181019try:
1019-typ, data = self._get_tagged_response(tag)
1020+typ, data = self._get_tagged_response(tag, expect_bye=logout)
10201021except self.abort as val:
10211022raise self.abort('command: %s => %s' % (name, val))
10221023except self.error as val:
10231024raise self.error('command: %s => %s' % (name, val))
1024-if name != 'LOGOUT':
1025+if not logout:
10251026self._check_bye()
10261027if typ == 'BAD':
10271028raise self.error('%s command error: %s %s' % (name, typ, data))
@@ -1117,17 +1118,23 @@ def _get_response(self):
11171118return resp
11181119111911201120-def _get_tagged_response(self, tag):
1121+def _get_tagged_response(self, tag, expect_bye=False):
1121112211221123while 1:
11231124result = self.tagged_commands[tag]
11241125if result is not None:
11251126del self.tagged_commands[tag]
11261127return 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-11311138self._check_bye()
1132113911331140# Some have reported "unexpected response" exceptions.