From 7e7a3dba5fd4262269f713dfe21ba7e4746fc2dd Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 10 Apr 2015 13:24:41 +0300 Subject: Issue #23865: close() methods in multiple modules now are idempotent and more robust at shutdown. If needs to release multiple resources, they are released even if errors are occured. --- Lib/smtplib.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'Lib/smtplib.py') diff --git a/Lib/smtplib.py b/Lib/smtplib.py index 99ffdeeb35..db23ff0d20 100755 --- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -855,12 +855,16 @@ class SMTP: def close(self): """Close the connection to the SMTP server.""" - if self.file: - self.file.close() - self.file = None - if self.sock: - self.sock.close() - self.sock = None + try: + file = self.file + self.file = None + if file: + file.close() + finally: + sock = self.sock + self.sock = None + if sock: + sock.close() def quit(self): """Terminate the SMTP session.""" -- cgit v1.2.1