From fdac555c4cfaaa0bf671c1a5e3fac0bd9c590925 Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 28 Jul 2015 15:11:47 +1200 Subject: [PATCH] Shut down the client if TLS handshake or login fails Avoids ResourceWarnings in Python 3. --- imapclient/config.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/imapclient/config.py b/imapclient/config.py index dac6c20..e07de9a 100644 --- a/imapclient/config.py +++ b/imapclient/config.py @@ -137,23 +137,26 @@ def create_client_from_config(conf): ssl=conf.ssl, ssl_context=ssl_context, stream=conf.stream) - - if conf.starttls: - client.starttls() - - if conf.oauth: - client.oauth_login(conf.oauth_url, - conf.oauth_token, - conf.oauth_token_secret) - elif conf.oauth2: - access_token = get_oauth2_token(conf.oauth2_client_id, - conf.oauth2_client_secret, - conf.oauth2_refresh_token) - client.oauth2_login(conf.username, access_token) - - elif not conf.stream: - client.login(conf.username, conf.password) - return client + try: + if conf.starttls: + client.starttls() + + if conf.oauth: + client.oauth_login(conf.oauth_url, + conf.oauth_token, + conf.oauth_token_secret) + elif conf.oauth2: + access_token = get_oauth2_token(conf.oauth2_client_id, + conf.oauth2_client_secret, + conf.oauth2_refresh_token) + client.oauth2_login(conf.username, access_token) + + elif not conf.stream: + client.login(conf.username, conf.password) + return client + except: + client.shutdown() + raise class Bunch(dict): -- 2.43.0