]> granicus.if.org Git - python/commitdiff
imaplib: IMAP4 constructor closes the socket on error
authorVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 Jan 2011 23:01:37 +0000 (23:01 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Wed, 5 Jan 2011 23:01:37 +0000 (23:01 +0000)
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

index 9c38e1c823db14c1d22d69b1bd03ffde2998c31d..8c14728193da84f4b375a5f0c148d6212af9d375 100644 (file)
@@ -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.