From 57ade90242b621057a01f68e3c68dc001b0ee767 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Fri, 15 Mar 2024 21:35:42 -0500 Subject: [PATCH] add __enter__ & __exit__ --- adafruit_esp32spi/adafruit_esp32spi_socket.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/adafruit_esp32spi/adafruit_esp32spi_socket.py b/adafruit_esp32spi/adafruit_esp32spi_socket.py index 70acbed..347b330 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_socket.py +++ b/adafruit_esp32spi/adafruit_esp32spi_socket.py @@ -66,6 +66,17 @@ def __init__( # pylint: enable=too-many-arguments + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb) -> None: + self.close() + while ( + _the_interface.socket_status(self._socknum) + != adafruit_esp32spi.SOCKET_CLOSED + ): + pass + def connect(self, address, conntype=None): """Connect the socket to the 'address' (which can be 32bit packed IP or a hostname string). 'conntype' is an extra that may indicate SSL or not,