From 1247dd4b7635a251795d0ae8b52de31165dc1dc5 Mon Sep 17 00:00:00 2001 From: Justin Myers Date: Mon, 10 Jun 2024 16:31:04 -0700 Subject: [PATCH] Update WIZNet version check for SSL --- adafruit_connection_manager.py | 9 +++++++-- tests/ssl_context_test.py | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/adafruit_connection_manager.py b/adafruit_connection_manager.py index 58dcb0c..67152d7 100644 --- a/adafruit_connection_manager.py +++ b/adafruit_connection_manager.py @@ -149,8 +149,13 @@ def get_radio_socketpool(radio): # versions of the Wiznet5k library or on boards withouut the ssl module # see https://docs.circuitpython.org/en/latest/shared-bindings/support_matrix.html ssl_context = None - cp_version = sys.implementation[1] - if pool.SOCK_STREAM == 1 and cp_version >= WIZNET5K_SSL_SUPPORT_VERSION: + implementation_name = sys.implementation.name + implementation_version = sys.implementation.version + if ( + pool.SOCK_STREAM == 1 + and implementation_name == "circuitpython" + and implementation_version >= WIZNET5K_SSL_SUPPORT_VERSION + ): try: import ssl # pylint: disable=import-outside-toplevel diff --git a/tests/ssl_context_test.py b/tests/ssl_context_test.py index 02bf96e..25e389e 100644 --- a/tests/ssl_context_test.py +++ b/tests/ssl_context_test.py @@ -5,6 +5,7 @@ """ SLL Context Tests """ import ssl +from collections import namedtuple from unittest import mock import mocket @@ -13,6 +14,8 @@ import adafruit_connection_manager from adafruit_connection_manager import WIZNET5K_SSL_SUPPORT_VERSION +SimpleNamespace = namedtuple("SimpleNamespace", "name version") + def test_connect_esp32spi_https( # pylint: disable=unused-argument adafruit_esp32spi_socketpool_module, @@ -53,7 +56,9 @@ def test_connect_wiznet5k_https_not_supported( # pylint: disable=unused-argumen mock_pool = mocket.MocketPool() radio = mocket.MockRadio.WIZNET5K() old_version = (WIZNET5K_SSL_SUPPORT_VERSION[0] - 1, 0, 0) - with mock.patch("sys.implementation", (None, old_version)): + with mock.patch( + "sys.implementation", SimpleNamespace("circuitpython", old_version) + ): ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio) connection_manager = adafruit_connection_manager.ConnectionManager(mock_pool) @@ -69,6 +74,9 @@ def test_connect_wiznet5k_https_supported( # pylint: disable=unused-argument adafruit_wiznet5k_with_ssl_socketpool_module, ): radio = mocket.MockRadio.WIZNET5K() - with mock.patch("sys.implementation", (None, WIZNET5K_SSL_SUPPORT_VERSION)): + with mock.patch( + "sys.implementation", + SimpleNamespace("circuitpython", WIZNET5K_SSL_SUPPORT_VERSION), + ): ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio) assert isinstance(ssl_context, ssl.SSLContext)