From: Menno Smits Date: Wed, 9 Nov 2011 20:40:19 +0000 (+0000) Subject: Fix incorrect msg_id for UID fetch when use_uid is False (#99) X-Git-Tag: 0.8~6 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=fed4f8ec408323a676f1cc650db9a3fc29cabfca;p=imapclient.git Fix incorrect msg_id for UID fetch when use_uid is False (#99) Don't use the UID as the key for FETCH responses where use_uid is False but UID is a fetch item. The UID was being used as the key instead of the message sequence. --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 8d301a8..c69d0c6 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -673,7 +673,7 @@ class IMAPClient(object): typ, data = self._imap._command_complete('FETCH', tag) self._checkok('fetch', typ, data) typ, data = self._imap._untagged_response(typ, data, 'FETCH') - return parse_fetch_response(data, self.normalise_times) + return parse_fetch_response(data, self.normalise_times, self.use_uid) def append(self, folder, msg, flags=(), msg_time=None): """Append a message to *folder*. diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index ce50413..d2f3d3c 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -51,7 +51,7 @@ def gen_parsed_response(text): raise ParseError("%s: %s" % (str(err), token)) -def parse_fetch_response(text, normalise_times=True): +def parse_fetch_response(text, normalise_times=True, uid_is_key=True): """Pull apart IMAP FETCH responses as returned by imaplib. Returns a dictionary, keyed by message ID. Each value a dictionary @@ -86,7 +86,11 @@ def parse_fetch_response(text, normalise_times=True): value = msg_response[i+1] if word == 'UID': - msg_id = _int_or_error(value, 'invalid UID') + uid = _int_or_error(value, 'invalid UID') + if uid_is_key: + msg_id = uid + else: + msg_data[word] = uid elif word == 'INTERNALDATE': msg_data[word] = _convert_INTERNALDATE(value, normalise_times) elif word in ('BODY', 'BODYSTRUCTURE'): diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index b1f0e8e..0707f26 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -197,16 +197,10 @@ class TestParseFetchResponse(unittest.TestCase): {76: {'SEQ': 23}}) - def test_repeated_UID(self): - self.assertEquals(parse_fetch_response(['23 (UID 76 FOO 123 UID 76 GOO 321)']), - {76: {'FOO': 123, - 'GOO': 321, + def test_not_uid_is_key(self): + self.assertEquals(parse_fetch_response(['23 (UID 76)'], uid_is_key=False), + {23: {'UID': 76, 'SEQ': 23}}) - self.assertEquals(parse_fetch_response(['23 (UID 76 FOO 123', 'UID 76 GOO 321)']), - {76: {'FOO': 123, - 'GOO': 321, - 'SEQ': 23}}) - def test_bad_UID(self): self.assertRaises(ParseError, parse_fetch_response, '(UID X)')