From 33e649cf6dc9b1764f309ed0a3206b474f8d99e1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 5 Jan 2011 23:01:37 +0000 Subject: [PATCH] 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. --- Lib/imaplib.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 9c38e1c823d..8c14728193d 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.