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_None(self):
self.assertRaises(InvalidCriteriaError, _normalise_search_criteria, None, None)
def test_nested_empty(self):
self.assertRaises(InvalidCriteriaError, self.client.search, [[]])
def test_empty(self):
self.assertRaises(InvalidCriteriaError, _normalise_search_criteria, '', None)
def _normalise_search_criteria(criteria, charset=None):
if not criteria:
raise exceptions.InvalidCriteriaError('no criteria specified')
if not charset:
charset = 'us-ascii'
if isinstance(criteria, (text_type, binary_type)):
return [to_bytes(criteria, charset)]
out = []
for item in criteria:
if isinstance(item, int):
out.append(str(item).encode('ascii'))
elif isinstance(item, (datetime, date)):
out.append(format_criteria_date(item))
elif isinstance(item, (list, tuple)):
# Process nested criteria list and wrap in parens.
inner = _normalise_search_criteria(item)
inner[0] = b'(' + inner[0]
def _search(self, criteria, charset):
args = []
if charset:
args.extend([b'CHARSET', to_bytes(charset)])
args.extend(_normalise_search_criteria(criteria, charset))
try:
data = self._raw_command_untagged(b'SEARCH', args)
except imaplib.IMAP4.error as e:
# Make BAD IMAP responses easier to understand to the user, with a link to the docs
m = re.match(r'SEARCH command error: BAD \[(.+)\]', str(e))
if m:
raise exceptions.InvalidCriteriaError(
'{original_msg}\n\n'
'This error may have been caused by a syntax error in the criteria: '
'{criteria}\nPlease refer to the documentation for more information '
'about search criteria syntax..\n'
'https://imapclient.readthedocs.io/en/master/#imapclient.IMAPClient.search'
.format(
original_msg=m.group(1),
criteria='"%s"' % criteria if not isinstance(criteria, list) else criteria
)
)
# If the exception is not from a BAD IMAP response, re-raise as-is
raise
return parse_message_list(data)