Package: python3-imaplib2
Version: 2.57-2
Severity: grave

Hi,

Using this test program and Gmail as the IMAP server:
    M = imaplib2.IMAP4_SSL(host=IMAP_SERVER, debug=IMAP_DEBUG)
    M.login(IMAP_USERNAME, IMAP_PASSWORD)
    if (compress):
        M.enable_compression()

    M.SELECT(mailbox=IMAP_MAILBOX, readonly=True)
    M.FETCH('1:10', '(UID FLAGS)')
    M.LOGOUT()

...the Python3 version of this package immediately throws an exception
after connecting and thus seems entirely broken:
  imaplib2.error: IMAP4 protocol error: program error: <class 'TypeError'> - 
cannot use a bytes pattern on a string-like object

Even after fixing this, there is an additional issue that shows up only
when enabling compression:
  imaplib2.abort: command: EXAMINE => socket error: <class 'TypeError'> - a 
bytes-like object is required, not 'str'

Note that even stretch's 2.55 presents the latter issue, but not the
former.

The attached patch fixes both of those issues and makes the above simple
test case work, although I haven't tried this any more complicated cases or
with different IMAP servers.

I went to upstream's GitHub in order to see if anyone else had reported
this or to submit a PR, but to my surprise, it seems like upstream's
repository has two different versions of imaplib2, one of them
designated as "py3" and reporting itself as v3.05. That version does not
exhibit any of these two issues above.

Regards,
Faidon
--- x/usr/lib/python3/dist-packages/imaplib2.py	2017-03-05 03:23:58.000000000 +0200
+++ /usr/lib/python3/dist-packages/imaplib2.py	2018-06-30 15:36:18.645224464 +0300
@@ -302,8 +302,8 @@
 
 
     # These must be encoded according to utf8 setting in _mode_xxx():
-    _literal = br'.*{(?P<size>\d+)}$'
-    _untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
+    _literal = r'.*{(?P<size>\d+)}$'
+    _untagged_status = r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
 
     continuation_cre = re.compile(r'\+( (?P<data>.*))?')
     mapCRLF_cre = re.compile(r'\r\n|\r|\n')
@@ -2215,13 +2215,13 @@
         """send(data)
         Send 'data' to remote."""
 
+        if bytes != str:
+            data = bytes(data, 'utf8')
+
         if self.compressor is not None:
             data = self.compressor.compress(data)
             data += self.compressor.flush(zlib.Z_SYNC_FLUSH)
 
-        if bytes != str:
-            data = bytes(data, 'utf8')
-
         if hasattr(self.sock, "sendall"):
             self.sock.sendall(data)
         else:

Reply via email to