Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_tagged_response_with_parse_error(self):
client = self.client
client._imap.tagged_commands = {sentinel.tag: None}
client._imap._get_response = lambda: b'NOT-A-STAR 99 EXISTS'
with self.assertRaises(ProtocolError):
client._consume_until_tagged_response(sentinel.tag, b'IDLE')
def atom(src, token):
if token == b'(':
return parse_tuple(src)
elif token == b'NIL':
return None
elif token[:1] == b'{':
literal_len = int(token[1:-1])
literal_text = src.current_literal
if literal_text is None:
raise ProtocolError('No literal corresponds to %r' % token)
if len(literal_text) != literal_len:
raise ProtocolError('Expecting literal of size %d, got %d' % (
literal_len, len(literal_text)))
return literal_text
elif len(token) >= 2 and (token[:1] == token[-1:] == b'"'):
return token[1:-1]
elif token.isdigit():
return int(token)
else:
return token
def gen_parsed_response(text):
if not text:
return
src = TokenSource(text)
token = None
try:
for token in src:
yield atom(src, token)
except ProtocolError:
raise
except ValueError:
_, err, _ = sys.exc_info()
raise ProtocolError("%s: %s" % (str(err), token))
def gen_parsed_response(text):
if not text:
return
src = TokenSource(text)
token = None
try:
for token in src:
yield atom(src, token)
except ProtocolError:
raise
except ValueError:
_, err, _ = sys.exc_info()
raise ProtocolError("%s: %s" % (str(err), token))
def parse_tuple(src):
out = []
for token in src:
if token == b")":
return tuple(out)
out.append(atom(src, token))
# no terminator
raise ProtocolError('Tuple incomplete before "(%s"' % _fmt_tuple(out))
if text == [None]:
return {}
response = gen_parsed_response(text)
parsed_response = defaultdict(dict)
while True:
try:
msg_id = seq = _int_or_error(six.next(response),
'invalid message ID')
except StopIteration:
break
try:
msg_response = six.next(response)
except StopIteration:
raise ProtocolError('unexpected EOF')
if not isinstance(msg_response, tuple):
raise ProtocolError('bad response type: %s' % repr(msg_response))
if len(msg_response) % 2:
raise ProtocolError('uneven number of response items: %s' % repr(msg_response))
# always return the sequence of the message, so it is available
# even if we return keyed by UID.
msg_data = {b'SEQ': seq}
for i in xrange(0, len(msg_response), 2):
word = msg_response[i].upper()
value = msg_response[i + 1]
if word == b'UID':
uid = _int_or_error(value, 'invalid UID')
if uid_is_key:
def _int_or_error(value, error_text):
try:
return int(value)
except (TypeError, ValueError):
raise ProtocolError('%s: %s' % (error_text, repr(value)))