Skip to content

Commit

Permalink
pattern.web.imap fix for Python 2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom De Smedt committed May 27, 2015
1 parent f135e37 commit 9ec2396
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions pattern/web/imap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

####################################################################################################

import sys
import os
import imaplib
import re
import imaplib
import email
import time

Expand Down Expand Up @@ -57,20 +58,30 @@ def encode_utf8(string):
# Fixes an issue in Python 2.5- with memory allocation.
# See: http://bugs.python.org/issue1389051

class IMAP4(imaplib.IMAP4):
pass
if sys.version_info[:2] > (2, 5):

class IMAP4(imaplib.IMAP4):
pass

class IMAP4_SSL(imaplib.IMAP4_SSL):
pass

else:

class IMAP4(imaplib.IMAP4):
pass

class IMAP4_SSL(imaplib.IMAP4_SSL):
def read(self, size):
"""Read 'size' bytes from remote."""
# sslobj.read() sometimes returns < size bytes
chunks = []
read = 0
while read < size:
data = self.sslobj.read(min(size-read, 16384)) # use min() instead of max().
read += len(data)
chunks.append(data)
return ''.join(chunks)
class IMAP4_SSL(imaplib.IMAP4_SSL):
def read(self, size):
"""Read 'size' bytes from remote."""
# sslobj.read() sometimes returns < size bytes
chunks = []
read = 0
while read < size:
data = self.sslobj.read(min(size-read, 16384))
read += len(data)
chunks.append(data)
return ''.join(chunks)

#### MAIL ##########################################################################################

Expand Down Expand Up @@ -176,6 +187,11 @@ def __getattr__(self, k):
#--- MAIL FOLDER -----------------------------------------------------------------------------------

def _decode(s, message):
try:
# Decode MIME header (e.g., "=?utf-8?q?").
s = email.Header.decode_header(s)[0][0]
except:
pass
try:
# Decode message Content-Type charset to Unicode.
# If all fails, try Latin-1 (common case).
Expand Down

0 comments on commit 9ec2396

Please sign in to comment.