From 471bc1edc21b2d2fe87af123bb7f437783ef787e Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Thu, 2 Sep 2010 22:26:38 +0100 Subject: [PATCH] #49: Work around imaplib bug (Python Issue 5949) Patch imaplib on import of IMAPClient if the running Python version is known to have the problem. --- imapclient/__init__.py | 1 + imapclient/imaplib_ssl_fix.py | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 imapclient/imaplib_ssl_fix.py diff --git a/imapclient/__init__.py b/imapclient/__init__.py index e7b394f..b47a132 100644 --- a/imapclient/__init__.py +++ b/imapclient/__init__.py @@ -3,5 +3,6 @@ __author__ = 'Menno Smits ' 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 index 0000000..19d52da --- /dev/null +++ b/imapclient/imaplib_ssl_fix.py @@ -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 -- 2.43.0