]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Only use the untagged post-login CAPABILITY response once
authorMenno Smits <menno@freshfoo.com>
Mon, 7 Sep 2015 22:54:54 +0000 (10:54 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 7 Sep 2015 22:54:54 +0000 (10:54 +1200)
...if sent by server in the first place.

Closes #161.

NEWS.rst
imapclient/imapclient.py
imapclient/test/test_imapclient.py

index 0399361f06b2dde780a2405cf2d5411d5ddd186d..bc82cfb62b310070fa571587738cf1907ef04131 100644 (file)
--- 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
index 3b29063e3aef5d189063b8ea0e50580f2aa72d4d..ed3d444ebda4462aaf951a52007301c47c4c3705 100644 (file)
@@ -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)
index 7cf1aff90f48c78435b6e3e4d9ce76b2fa3cf954..1bf71e274914b5b56cba09be4db49bfeb29b0212 100644 (file)
@@ -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',)