From a311faf183cfdcc4434806b9480825a1322b30b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolfo=20Garc=C3=ADa=20Pe=C3=B1as=20=28kix=29?= Date: Sun, 8 Nov 2020 15:47:51 +0100 Subject: [PATCH] IMAP search now works fine This patch converts the search results from bytes to strings I add a bit comment about it here: In Py2, with IMAP, imaplib2 returned a list of one element string. ['1, 2, 3, ...'] -> in Py3 is [b'1 2 3,...'] In Py2, with Davmail, imaplib2 returned a list of strings. ['1', '2', '3', ...] -> in Py3 should be [b'1', b'2', b'3',...] In my tests with Py3, I get a list with one element: [b'1 2 3 ...'] Then I convert the values to string and I get ['1 2 3 ...'] With Davmail, it should be [b'1', b'2', b'3',...] When I convert the values to string, I get ['1', '2', '3',...] --- offlineimap/folder/IMAP.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 2ca5a04..464caa7 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -221,9 +221,24 @@ class IMAPFolder(BaseFolder): "Search string was '%s'. Error: %s" % \ (self.getrepository(), self, search_cond, str(e)) raise OfflineImapError(msg, OfflineImapError.ERROR.FOLDER) - # Davmail returns list instead of list of one element string. - # On first run the first element is empty. - if b' ' in res_data[0] or res_data[0] == b'': + + """ + In Py2, with IMAP, imaplib2 returned a list of one element string. + ['1, 2, 3, ...'] -> in Py3 is [b'1 2 3,...'] + In Py2, with Davmail, imaplib2 returned a list of strings. + ['1', '2', '3', ...] -> in Py3 should be [b'1', b'2', b'3',...] + + In my tests with Py3, I get a list with one element: [b'1 2 3 ...'] + Then I convert the values to string and I get ['1 2 3 ...'] + + With Davmail, it should be [b'1', b'2', b'3',...] + When I convert the values to string, I get ['1', '2', '3',...] + """ + res_data = [x.decode('utf-8') for x in res_data] + + # Then, I can do the check in the same way than Python 2 + # with string comparison: + if ' ' in res_data[0] or res_data[0] == '': res_data = res_data[0].split() # Some servers are broken. if 0 in res_data: