From 4f230b3d268f5251eba8a3ed22b586c945457712 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Thu, 24 Apr 2014 00:50:17 +1200 Subject: [PATCH] Correctly handle nested BODYSTRUCTURE responses Nested BodyData elements are now generated for each subpart. This means that when multipart sections exist in other multipart sections, a list of subparts is now (correctly) generated just like what was already done for the outmost parts. Thanks to Brandon Rhodes for reporting this bug. --- imapclient/response_parser.py | 2 +- imapclient/test/test_response_parser.py | 33 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index a329421..eb5288c 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -138,7 +138,7 @@ class BodyData(tuple): for i, part in enumerate(response): if isinstance(part, six.string_types): break - return cls((list(response[:i]),) + response[i:]) + return cls(([cls.create(part) for part in response[:i]],) + response[i:]) else: return cls(response) diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index c844492..1bfce08 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -258,10 +258,11 @@ class TestParseFetchResponse(unittest.TestCase): def test_BODY(self): self.check_BODYish_single_part('BODY') self.check_BODYish_multipart('BODY') + self.check_BODYish_nested_multipart('BODY') def test_BODYSTRUCTURE(self): self.check_BODYish_single_part('BODYSTRUCTURE') - self.check_BODYish_multipart('BODYSTRUCTURE') + self.check_BODYish_nested_multipart('BODYSTRUCTURE') def check_BODYish_single_part(self, respType): text = '123 (UID 317 %s ("TEXT" "PLAIN" ("CHARSET" "us-ascii") NIL NIL "7BIT" 16 1))' % respType @@ -283,6 +284,36 @@ class TestParseFetchResponse(unittest.TestCase): }) self.assertTrue(parsed[269][respType].is_multipart) + def check_BODYish_nested_multipart(self, respType): + text = '1 (%s (' \ + '(' \ + '("text" "html" ("charset" "utf-8") NIL NIL "7bit" 97 3 NIL NIL NIL NIL)' \ + '("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 62 3 NIL NIL NIL NIL)' \ + '"alternative" ("boundary" "===============8211050864078048428==") NIL NIL NIL' \ + ')' \ + '("text" "plain" ("charset" "utf-8") NIL NIL "7bit" 16 1 NIL ("attachment" ("filename" "attachment.txt")) NIL NIL) ' \ + '"mixed" ("boundary" "===============0373402508605428821==") NIL NIL NIL))' \ + % respType + + parsed = parse_fetch_response([text]) + self.assertEqual(parsed, {1: { + respType: ( + [ + ( + [ + ('text', 'html', ('charset', 'utf-8'), None, None, '7bit', 97, 3, None, None, None, None), + ('text', 'plain', ('charset', 'utf-8'), None, None, '7bit', 62, 3, None, None, None, None) + ], 'alternative', ('boundary', '===============8211050864078048428=='), None, None, None + ), + ('text', 'plain', ('charset', 'utf-8'), None, None, '7bit', 16, 1, None, ('attachment', ('filename', 'attachment.txt')), None, None) + ], 'mixed', ('boundary', '===============0373402508605428821=='), None, None, None, + ), + 'SEQ': 1, + }}) + self.assertTrue(parsed[1][respType].is_multipart) + self.assertTrue(parsed[1][respType][0][0].is_multipart) + self.assertFalse(parsed[1][respType][0][0][0][0].is_multipart) + def test_partial_fetch(self): body = '01234567890123456789' self.assertEqual(parse_fetch_response( -- 2.43.0