Index: Lib/imaplib.py =================================================================== --- Lib/imaplib.py (révision 66888) +++ Lib/imaplib.py (copie de travail) @@ -147,6 +147,7 @@ class abort(error): pass # Service errors - close and retry class readonly(abort): pass # Mailbox status changed to READ-ONLY + encoding = "ISO-8859-1" mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]", re.ASCII) def __init__(self, host = '', port = IMAP4_PORT): @@ -219,7 +220,12 @@ # Overridable methods - def open(self, host = '', port = IMAP4_PORT): + def _create_socket(self): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((self.host, self.port)) + return sock + + def open(self, host='', port=IMAP4_PORT): """Setup connection to remote server on "host:port" (default: localhost:standard IMAP4 port). This connection will be used by the routines: @@ -227,9 +233,8 @@ """ self.host = host self.port = port - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.connect((host, port)) - self.file = self.sock.makefile('rb') + self.sock = self._create_socket() + self.file = self.sock.makefile('r', encoding=self.encoding) def read(self, size): @@ -244,6 +249,7 @@ def send(self, data): """Send data to remote.""" + data = bytes(data, self.encoding) self.sock.sendall(data) @@ -1137,67 +1143,18 @@ self.certfile = certfile IMAP4.__init__(self, host, port) + def _create_socket(self): + sock = IMAP4._create_socket() + return ssl.wrap_socket(sock, self.keyfile, self.certfile) - def open(self, host = '', port = IMAP4_SSL_PORT): + def open(self, host='', port=IMAP4_SSL_PORT): """Setup connection to remote server on "host:port". (default: localhost:standard IMAP4 SSL port). This connection will be used by the routines: read, readline, send, shutdown. """ - self.host = host - self.port = port - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((host, port)) - self.sock = ssl.wrap_socket(sock, self.keyfile, self.certfile) - self.file = self.sock.makefile('rb') + IMAP4.open(self, host, port) - - 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 b''.join(chunks) - - - def readline(self): - """Read line from remote.""" - line = [] - while 1: - char = self.sslobj.read(1) - line.append(char) - if char == b"\n": return b''.join(line) - - - def send(self, data): - """Send data to remote.""" - bytes = len(data) - while bytes > 0: - sent = self.sslobj.write(data) - if sent == bytes: - break # avoid copy - data = data[sent:] - bytes = bytes - sent - - - def shutdown(self): - """Close I/O established in "open".""" - self.sock.close() - - - def socket(self): - """Return socket instance used to connect to IMAP4 server. - - socket = .socket() - """ - return self.sock - - def ssl(self): """Return SSLObject instance used to communicate with the IMAP4 server.