From 320f81c148c044becb53671b2427f165c08c5be5 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 8 Sep 2015 10:54:54 +1200 Subject: [PATCH] Only use the untagged post-login CAPABILITY response once ...if sent by server in the first place. Closes #161. --- NEWS.rst | 2 ++ imapclient/imapclient.py | 40 +++++++++++++++++++++++++----- imapclient/test/test_imapclient.py | 1 + 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 0399361..bc82cfb 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -148,6 +148,8 @@ Other * The embedded six package has been removed in favour of using an externally installed instance. * Fixed handling of literals in STATUS responses. + * Only use the untagged post-login CAPABILITY response once (if sent + by server). ====== 0.13 diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 3b29063..ed3d444 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -293,19 +293,18 @@ class IMAPClient(object): If the session is not yet authenticated, the cached capabilities determined at connection time will be returned. """ - # if a capability response has been cached, use that + # If a capability response has been cached, use that. if self._cached_capabilities: return self._cached_capabilities - # If server returned an untagged CAPABILITY response (during - # authentication), cache it and return that. - - untagged = normalise_untagged_responses(self._imap.untagged_responses) + # If the server returned an untagged CAPABILITY response + # (during authentication), cache it and return that. + untagged = _dict_bytes_normaliser(self._imap.untagged_responses) response = untagged.pop(b'CAPABILITY', None) if response: return self._save_capabilities(response[0]) - # if authenticated, but don't have a capability reponse, ask for one + # If authenticated, but don't have a capability reponse, ask for one if self._imap.state in ('SELECTED', 'AUTH'): response = self._command_and_check('capability', unpack=True) return self._save_capabilities(response) @@ -1370,3 +1369,32 @@ def _iter_with_last(items): last_i = len(items) - 1 for i, item in enumerate(items): yield item, i == last_i + + +_not_present = object() + + +class _dict_bytes_normaliser(object): + """Wrap a dict with unicode/bytes keys and normalise the keys to + bytes. + """ + + def __init__(self, d): + self._d = d + + def pop(self, ink, default=_not_present): + for k in self._gen_keys(ink): + try: + return self._d.pop(k) + except KeyError: + pass + if default == _not_present: + raise KeyError(ink) + return default + + def _gen_keys(self, k): + yield k + if isinstance(k, binary_type): + yield to_unicode(k) + else: + yield to_bytes(k) diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 7cf1aff..1bf71e2 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -477,6 +477,7 @@ class TestCapabilities(IMAPClientTest): self.assertEqual(self.client._cached_capabilities, None) self.assertEqual(self.client.capabilities(), (b'FOO', b'MORE')) self.assertEqual(self.client._cached_capabilities, (b'FOO', b'MORE')) + self.assertEqual(self.client._imap.untagged_responses, {}) def test_caching(self): self.client._imap.capabilities = ('FOO',) -- 2.43.0