From 8034c47b89d880cbb22a281e3fd0686f55df64dc Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Mon, 7 Jan 2019 13:28:05 -0800 Subject: [PATCH] avoid 'referenced before assignment' exception (#150) --- pyHS100/protocol.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pyHS100/protocol.py b/pyHS100/protocol.py index 9b669e14..1a0892fb 100644 --- a/pyHS100/protocol.py +++ b/pyHS100/protocol.py @@ -42,6 +42,7 @@ class TPLinkSmartHomeProtocol: request = json.dumps(request) timeout = TPLinkSmartHomeProtocol.DEFAULT_TIMEOUT + sock = None try: sock = socket.create_connection((host, port), timeout) @@ -63,14 +64,17 @@ class TPLinkSmartHomeProtocol: finally: try: - sock.shutdown(socket.SHUT_RDWR) + if sock: + sock.shutdown(socket.SHUT_RDWR) except OSError: # OSX raises OSError when shutdown() gets called on a closed # socket. We ignore it here as the data has already been read # into the buffer at this point. pass + finally: - sock.close() + if sock: + sock.close() response = TPLinkSmartHomeProtocol.decrypt(buffer[4:]) _LOGGER.debug("< (%i) %s", len(response), response)