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)
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)
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',)