From 423785725bcaee2e2a093683c5b58b71abfbbc48 Mon Sep 17 00:00:00 2001 From: Thomas De Schampheleire Date: Mon, 12 Oct 2020 08:52:20 +0200 Subject: [PATCH 1/2] IMAP.py: server responses are in bytes, not string Following error is seen when parsing server responses for sent mail: 2020-10-12 08:19:11 WARNING: Can't parse FETCH response, we awaited string: b' UID 26855)' 2020-10-12 08:19:11 WARNING: savemessage: Searching mails for new Message-ID failed. Could not determine new UID on Sent. The comparison with 'type("")' means comparing with 'string' type in Python 3, but the left-hand side is a bytes object. In case a tuple was received (first case in the code), the input is already decoded from bytes to strings, but in case a single input was received it was not. Note that the comparison with 'type("")' is a bit odd, a more logical way seems to be: if isinstance(item, bytes) Signed-off-by: Thomas De Schampheleire --- offlineimap/folder/IMAP.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index 342c102..a23511e 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -482,7 +482,8 @@ class IMAPFolder(BaseFolder): item[1], flags=re.IGNORECASE): found = item[0] elif found is not None: - if type(item) == type(""): + if type(item) == type(b""): + item = item.decode('utf-8') uid = re.search("UID\s+(\d+)", item, flags=re.IGNORECASE) if uid: return int(uid.group(1)) From c00af919909e506537aaca1fd5362979c9248d1c Mon Sep 17 00:00:00 2001 From: Thomas De Schampheleire Date: Mon, 12 Oct 2020 08:57:36 +0200 Subject: [PATCH 2/2] accounts: decode output of hooks When a hook is configured, the output is a bytes object. This is then printed on the console/logfile as: 2020-10-12 08:36:17 INFO: Hook stdout: b'Processed 3 total files in almost no time.\nAdded 3 new messages to the database.\n' Hook stderr:b'' Decode the output so that it is printed nicely, as: 2020-10-12 08:36:17 INFO: Hook stdout: Processed 3 total files in almost no time. Added 3 new messages to the database. Hook stderr: Signed-off-by: Thomas De Schampheleire --- offlineimap/accounts.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index 00342e1..4cb15ab 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -459,8 +459,9 @@ class SyncableAccount(Account): p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) - r = p.communicate() - self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n" % r) + stdout, stderr = p.communicate() + self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n" + % (stdout.decode('utf-8'), stderr.decode('utf-8'))) self.ui.callhook("Hook return code: %d" % p.returncode) except (KeyboardInterrupt, SystemExit): raise