]> khamidou — GitHub mirrors - imapclient.git/commitdiff
#49: Work around imaplib bug (Python Issue 5949)
authorMenno Smits <menno@freshfoo.com>
Thu, 2 Sep 2010 21:26:38 +0000 (22:26 +0100)
committerMenno Smits <menno@freshfoo.com>
Thu, 2 Sep 2010 21:26:38 +0000 (22:26 +0100)
Patch imaplib on import of IMAPClient if the running Python version is known to
have the problem.

imapclient/__init__.py
imapclient/imaplib_ssl_fix.py [new file with mode: 0644]

index e7b394fb900fc1bc6f86f8c45601ff4314becae7..b47a132ccc60e2a106f222bebac1ff7eb1c4cc34 100644 (file)
@@ -3,5 +3,6 @@ __author__ = 'Menno Smits <menno@freshfoo.com>'
 
 from imapclient import *
 from response_parser import *
+import imaplib_ssl_fix
 
 
diff --git a/imapclient/imaplib_ssl_fix.py b/imapclient/imaplib_ssl_fix.py
new file mode 100644 (file)
index 0000000..19d52da
--- /dev/null
@@ -0,0 +1,44 @@
+"""
+Work-around for Python Issue 5943 (http://bugs.python.org/issue5949).
+
+This will patch imaplib's IMAP4_SSL.readline method with the fixed
+verion in Python versions that are known to have the problem.
+
+The problem definitely exists in Python 2.5 and 2.6 up until but not
+including 2.6.5. It was also fixed in Python 2.7 alpha 2 so no attempt
+is made to patch 2.7 versions.
+
+Please let me know if there's more Python versions that should be
+patched.
+
+Efforts are made to only perform the patch once.
+"""
+
+import sys
+import imaplib
+
+def _is_affected_version(sys_version):
+    sys_version = sys_version[:3]
+    if sys_version < (2, 5, 0):
+        # Not sure whether these old versions are affected so being
+        # conservative and not patching.
+        return False
+    elif sys_version < (2, 6, 5):
+        return True
+    return False
+
+def _fixed_readline(self):
+    """Read line from remote."""
+    line = []
+    while 1:
+        char = self.sslobj.read(1)
+        line.append(char)
+        if char in ("\n", ""): return ''.join(line)
+
+_fixed_readline.patched = True    # Marker to indicate patched version 
+
+
+ssl_class = imaplib.IMAP4_SSL
+
+if _is_affected_version(sys.version_info) and not hasattr(ssl_class.readline, 'patched'):
+    ssl_class.readline = _fixed_readline