From: Menno Smits Date: Wed, 29 May 2013 22:23:38 +0000 (+0100) Subject: Handle the same message appearing multiple times in a single FETCH response X-Git-Tag: 0.10~2^2~15 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9d085f7f82a2dbef0e853cce94390e99c09e166c;p=imapclient.git Handle the same message appearing multiple times in a single FETCH response This can occur when server sends unsolicited FETCH responses. --- diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index cd2fdd7..0adfc09 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -14,6 +14,7 @@ Initially inspired by http://effbot.org/zone/simple-iterator-parser.htm from __future__ import unicode_literals import sys +from collections import defaultdict from datetime import datetime from . import six @@ -69,7 +70,7 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True): return {} response = gen_parsed_response(text) - parsed_response = {} + parsed_response = defaultdict(dict) while True: try: msg_id = seq = _int_or_error(six.next(response), @@ -107,7 +108,7 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True): else: msg_data[word] = value - parsed_response[msg_id] = msg_data + parsed_response[msg_id].update(msg_data) return parsed_response diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index b3e4d4b..84c249c 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -224,6 +224,13 @@ class TestParseFetchResponse(unittest.TestCase): 7: {'FLAGS': ('Baz', 'Sneeve'), 'SEQ': 7}, }) + def test_same_message_appearing_multiple_times(self): + # This can occur when server sends unsolicited FETCH responses + # (e.g. RFC 4551) + self.assertEqual(parse_fetch_response( + ["2 (FLAGS (Foo Bar)) ", + "2 (MODSEQ 4)"]), + {2: {'FLAGS': ('Foo', 'Bar'), 'SEQ': 2, 'MODSEQ': 4}}) def test_literals(self): self.assertEqual(parse_fetch_response([('1 (RFC822.TEXT {4}', 'body'),