From 5d03fc932946f03b3b5672d4fc65de1d2c2b10d7 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Wed, 8 Sep 2010 18:21:43 +0100 Subject: [PATCH] Fixed handling of fetch parts that use square brackets and contain spaces (#55) Fetches using message parts such as ['BODY[HEADER.FIELDS (From Subject)]'] now return sane responses. Also cleaned up a few oddities in the lexer code. --- imapclient/response_lexer.py | 35 +++++++++++++------------ imapclient/test/test_response_parser.py | 4 +-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/imapclient/response_lexer.py b/imapclient/response_lexer.py index bbf5863..8b4e093 100644 --- a/imapclient/response_lexer.py +++ b/imapclient/response_lexer.py @@ -46,34 +46,34 @@ class Lexer(object): self.sources = None self.current_source = None - def parse_quote(self, stream_i, quoted, token): + def read_until(self, stream_i, end_char, escape=True): + token = '' try: for nextchar in stream_i: - if nextchar == "\\": + if escape and nextchar == "\\": escaper = nextchar nextchar = stream_i.next() - if nextchar != escaper and nextchar != quoted: + if nextchar != escaper and nextchar != end_char: token += escaper - elif nextchar == quoted: + elif nextchar == end_char: break token += nextchar else: - raise ValueError("No closing quotation") + raise ValueError("No closing %r" % end_char) except StopIteration: - # escaped char... - raise ValueError("No closing quotation") - - return quoted + token + quoted + raise ValueError("No closing %r" % end_char) + return token + end_char def read_token_stream(self, stream_i): whitespace = self.WHITESPACE wordchars = self.NON_SPECIALS - parse_quote = self.parse_quote + read_until = self.read_until while True: token = '' + # process whitespace for nextchar in stream_i: if nextchar in whitespace: continue @@ -83,10 +83,8 @@ class Lexer(object): token = nextchar elif nextchar == '"': - chunk = parse_quote(stream_i, nextchar, token) - token += chunk - yield token - token = '' + assert not token + yield nextchar + read_until(stream_i, nextchar) continue else: @@ -98,6 +96,10 @@ class Lexer(object): # non whitespace appending for nextchar in stream_i: + if nextchar == '[': + yield token + nextchar + read_until(stream_i, ']', escape=False) + break + if nextchar in wordchars: token += nextchar continue @@ -107,9 +109,8 @@ class Lexer(object): break elif nextchar == '"': - chunk = parse_quote(stream_i, nextchar, token) - token = chunk - yield token + assert not token + yield nextchar + read_until(stream_i, nextchar) break else: diff --git a/imapclient/test/test_response_parser.py b/imapclient/test/test_response_parser.py index be407c9..2ddd45b 100644 --- a/imapclient/test/test_response_parser.py +++ b/imapclient/test/test_response_parser.py @@ -147,7 +147,7 @@ class TestParseResponse(unittest.TestCase): def test_bad_quoting(self): - self._test_parse_error('"abc next', 'No closing quotation:') + self._test_parse_error('"abc next', "No closing '\"'") def _test(self, to_parse, expected, wrap=True): @@ -255,7 +255,7 @@ class TestParseFetchResponse(unittest.TestCase): def test_BODY_HEADER_FIELDS(self): header_text = 'Subject: A subject\r\nFrom: Some one \r\n\r\n' self.assertEquals(parse_fetch_response( - [('123 (UID 31710 BODY[HEADER.FIELDS (FROM SUBJECT)] {57}', header_text), ')']), + [('123 (UID 31710 BODY[HEADER.FIELDS (from subject)] {57}', header_text), ')']), { 31710: {'BODY[HEADER.FIELDS (FROM SUBJECT)]': header_text, 'SEQ': 123}}) -- 2.43.0