From: Victor Stinner Date: Wed, 5 Jan 2011 23:01:37 +0000 (+0000) Subject: imaplib: IMAP4 constructor closes the socket on error X-Git-Tag: v3.2rc1~164 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=33e649cf6dc9b1764f309ed0a3206b474f8d99e1;p=thirdparty%2FPython%2Fcpython.git imaplib: IMAP4 constructor closes the socket on error Fix a ResourceWarning(unclosed socket) if an exception is raised in the constructor after the creation of the socket. Patch written by Nadeem Vawda. --- diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 9c38e1c823db..8c14728193da 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -169,6 +169,17 @@ class IMAP4: self.open(host, port) + try: + self._connect() + except Exception: + try: + self.shutdown() + except socket.error: + pass + raise + + + def _connect(self): # Create unique tag for this session, # and compile tagged response matcher.