From d8213f4435f8f34269a4529168596c9c1cf792d2 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Thu, 29 Apr 2010 22:49:35 +0100 Subject: [PATCH] #43: numeric folder names break list_folders Further hacks to the lexer to allow the response parser to distinguish between a string with digits in it and an actual integer. This prevents folder names comprising of just digits from being returned as integers instead of strings. --- imapclient/response_parser.py | 14 +++++++++++--- livetest.py | 3 ++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index 218d7b0..259d083 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -140,16 +140,22 @@ class LiteralHandlingReader: self.literal = None def read(self, n): - # We also hack into the lexer so we get special treatment for '\\' - # chars - they are only special inside a quoted string. + # Two additional hacks: + # 1. Hack into the lexer so we get special treatment for backslash + # chars - they are only special inside a quoted string. + # 2. For quoted strings return the quotes around the string so + # that atom() can distinguish numbers from strings. Eg. "123" vs 123. + # These are stripped off before returning them to the user. assert n==1 if self.pushed is not None: ret = self.pushed self.pushed = None else: ret = self.src.read(n) - if ret=="\\" and self.lexer.state not in '"\\': + if ret == "\\" and self.lexer.state not in '"\\': self.pushed = "\\" + elif ret == '"' and self.lexer.state != '\\': + self.lexer.token += '"' return ret def close(self): @@ -207,6 +213,8 @@ def atom(src, token): raise ParseError('Expecting literal of size %d, got %d' % ( literal_len, len(literal_text))) return literal_text + elif len(token) >= 2 and (token[0] == token[-1] == '"'): + return token[1:-1] elif token.isdigit(): return int(token) else: diff --git a/livetest.py b/livetest.py index deb53a4..0e41c32 100755 --- a/livetest.py +++ b/livetest.py @@ -146,7 +146,8 @@ def test_folders(client): '"foobar"', 'foo "bar"', 'stuff & things', - u'test & \u2622'] + u'test & \u2622', + '123'] for folder in test_folders: assert not client.folder_exists(folder) -- 2.43.0