root/imapclient/imaplib_ssl_fix.py

Revision 304:239f24fbd17c, 1.4 KB (checked in by Menno Smits <menno@…>, 2 weeks ago)

updated copyright date to 2012

Line 
1# Copyright (c) 2012, Menno Smits
2# Released subject to the New BSD License
3# Please see http://en.wikipedia.org/wiki/BSD_licenses
4
5"""
6Work-around for Python Issue 5943 (http://bugs.python.org/issue5949).
7
8This will patch imaplib's IMAP4_SSL.readline method with the fixed
9verion in Python versions that are known to have the problem.
10
11The problem definitely exists in Python 2.5 and 2.6 up until but not
12including 2.6.5. It was also fixed in Python 2.7 alpha 2 so no attempt
13is made to patch 2.7 versions.
14
15Please let me know if there's more Python versions that should be
16patched.
17
18Efforts are made to only perform the patch once.
19"""
20
21import sys
22import imaplib
23
24def _is_affected_version(sys_version):
25    sys_version = sys_version[:3]
26    if sys_version < (2, 5, 0):
27        # Not sure whether these old versions are affected so being
28        # conservative and not patching.
29        return False
30    elif sys_version < (2, 6, 5):
31        return True
32    return False
33
34def _fixed_readline(self):
35    """Read line from remote."""
36    line = []
37    while 1:
38        char = self.sslobj.read(1)
39        line.append(char)
40        if char in ("\n", ""): return ''.join(line)
41
42_fixed_readline.patched = True    # Marker to indicate patched version
43
44
45ssl_class = imaplib.IMAP4_SSL
46
47if _is_affected_version(sys.version_info) and not hasattr(ssl_class.readline, 'patched'):
48    ssl_class.readline = _fixed_readline
Note: See TracBrowser for help on using the browser.