Decode byte pattern into string using proper encoding

The IMAP response is `str` and a `re.match()` needs to use a `str`
type pattern. Depending on the encoding supported by the IMAP
response, the literal patterns need to be decoded using the
correct (supported) encoding.

Signed-off-by: Nicolas Bock <nicolasbock@gmail.com>
This commit is contained in:
Nicolas Bock 2020-01-22 14:43:44 -07:00
parent 564930725e
commit 94b5e357c1
No known key found for this signature in database
GPG Key ID: ACE86353C0EF022A
1 changed files with 4 additions and 4 deletions

View File

@ -435,8 +435,8 @@ class IMAP4(object):
self.utf8_enabled = False
self._encoding = 'ascii'
if bytes != str:
self.literal_cre = re.compile(self._literal, re.ASCII)
self.untagged_status_cre = re.compile(self._untagged_status, re.ASCII)
self.literal_cre = re.compile(self._literal.decode("ASCII"), re.ASCII)
self.untagged_status_cre = re.compile(self._untagged_status.decode("ASCII"), re.ASCII)
else:
self.literal_cre = re.compile(self._literal)
self.untagged_status_cre = re.compile(self._untagged_status)
@ -446,8 +446,8 @@ class IMAP4(object):
self.utf8_enabled = True
self._encoding = 'utf-8'
if bytes != str:
self.literal_cre = re.compile(self._literal)
self.untagged_status_cre = re.compile(self._untagged_status)
self.literal_cre = re.compile(self._literal.decode(self._encoding))
self.untagged_status_cre = re.compile(self._untagged_status.decode(self._encoding))
else:
self.literal_cre = re.compile(self._literal, re.UNICODE)
self.untagged_status_cre = re.compile(self._untagged_status, re.UNICODE)