From ff6cd73f3314c94e8d59c681d7cd9e22d3ee898b Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Tue, 20 Dec 2022 16:23:03 +0200 Subject: [PATCH 1/5] Remove all non-comment assert's --- adafruit_wiznet5k/adafruit_wiznet5k.py | 42 ++++++++++++------- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 7 ++-- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 11 ++--- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index bef3a38..e4e6384 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -187,7 +187,8 @@ def __init__( # attempt to initialize the module self._ch_base_msb = 0 - assert self._w5100_init() == 1, "Failed to initialize WIZnet module." + if self._w5100_init() != 1: + raise AssertionError("Failed to initialize WIZnet module.") # Set MAC address self.mac_address = mac self.src_port = 0 @@ -214,7 +215,8 @@ def __init__( ret = self.set_dhcp(hostname, dhcp_timeout) if ret != 0: self._dhcp_client = None - assert ret == 0, "Failed to configure DHCP Server!" + if ret != 0: + raise AssertionError("Failed to configure DHCP Server!") def set_dhcp( self, hostname: Optional[str] = None, response_timeout: float = 30 @@ -272,7 +274,8 @@ def get_host_by_name(self, hostname: str) -> bytes: ret = _dns_client.gethostbyname(hostname) if self._debug: print("* Resolved IP: ", ret) - assert ret != -1, "Failed to resolve hostname!" + if ret == -1: + raise AssertionError("Failed to resolve hostname!") return ret @property @@ -481,7 +484,8 @@ def detect_w5500(self) -> int: :return int: 1 if a W5500 chip is detected, -1 if not. """ self._chip_type = "w5500" - assert self.sw_reset() == 0, "Chip not reset properly!" + if self.sw_reset() != 0: + raise AssertionError("Chip not reset properly!") self._write_mr(0x08) # assert self._read_mr()[0] == 0x08, "Expected 0x08." if self._read_mr()[0] != 0x08: @@ -511,7 +515,8 @@ def detect_w5100s(self) -> int: """ self._chip_type = "w5100s" # sw reset - assert self.sw_reset() == 0, "Chip not reset properly!" + if self.sw_reset() != 0: + raise AssertionError("Chip not reset properly!") if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51: return -1 @@ -623,7 +628,8 @@ def socket_available(self, socket_num: int, sock_type: int = SNMR_TCP) -> int: socket_num, sock_type ) ) - assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets." + if socket_num > self.max_sockets: + raise AssertionError("Provided socket exceeds max_sockets.") res = self._get_rx_rcv_size(socket_num) @@ -676,7 +682,8 @@ def socket_connect( :param int conn_mode: The connection mode. Use SNMR_TCP for TCP or SNMR_UDP for UDP, defaults to SNMR_TCP. """ - assert self.link_status, "Ethernet cable disconnected!" + if not self.link_status: + raise AssertionError("Ethernet cable disconnected!") if self._debug: print( "* w5k socket connect, protocol={}, port={}, ip={}".format( @@ -744,7 +751,8 @@ def socket_listen( :param int conn_mode: Connection mode SNMR_TCP for TCP or SNMR_UDP for UDP, defaults to SNMR_TCP. """ - assert self.link_status, "Ethernet cable disconnected!" + if not self.link_status: + raise AssertionError("Ethernet cable disconnected!") if self._debug: print( "* Listening on port={}, ip={}".format( @@ -804,7 +812,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int: UDP, defaults to SNMR_TCP. :return int: 1 if the socket was opened, 0 if not. """ - assert self.link_status, "Ethernet cable disconnected!" + if not self.link_status: + raise AssertionError("Ethernet cable disconnected!") if self._debug: print("*** Opening socket %d" % socket_num) status = self._read_snsr(socket_num)[0] @@ -836,10 +845,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int: # open socket self._write_sncr(socket_num, CMD_SOCK_OPEN) self._read_sncr(socket_num) - assert ( - self._read_snsr((socket_num))[0] == 0x13 - or self._read_snsr((socket_num))[0] == 0x22 - ), "Could not open socket in TCP or UDP mode." + if self._read_snsr((socket_num))[0] not in [0x13, 0x22]: + raise AssertionError("Could not open socket in TCP or UDP mode.") return 0 return 1 @@ -879,8 +886,10 @@ def socket_read( was unsuccessful then both items equal an error code, 0 for no data waiting and -1 for no connection to the socket. """ - assert self.link_status, "Ethernet cable disconnected!" - assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets." + if not self.link_status: + raise AssertionError("Ethernet cable disconnected!") + if socket_num > self.max_sockets: + raise AssertionError("Provided socket exceeds max_sockets.") # Check if there is data available on the socket ret = self._get_rx_rcv_size(socket_num) @@ -973,7 +982,8 @@ def socket_write( :return int: The number of bytes written to the buffer. """ - assert self.link_status, "Ethernet cable disconnected!" + if not self.link_status: + raise AssertionError("Ethernet cable disconnected!") assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets." status = 0 ret = 0 diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 3243f77..dcb10b5 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -274,10 +274,9 @@ def parse_dhcp_response( # -- Parse Packet, FIXED -- # # Validate OP - assert ( - _BUFF[0] == DHCP_BOOT_REPLY - ), "Malformed Packet - \ - DHCP message OP is not expected BOOT Reply." + if (_BUFF[0] != DHCP_BOOT_REPLY): + raise AssertionError("Malformed Packet - \ + DHCP message OP is not expected BOOT Reply.") xid = _BUFF[4:8] if bytes(xid) < self._initial_xid: diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index 01a372a..e5c9fb2 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -283,7 +283,8 @@ def listen(self, backlog: Optional[int] = None) -> None: :param Optional[int] backlog: Included for compatibility but ignored. """ - assert self._listen_port is not None, "Use bind to set the port before listen!" + if (self._listen_port is None): + raise AssertionError("Use bind to set the port before listen!") _the_interface.socket_listen(self.socknum, self._listen_port) self._buffer = b"" @@ -340,9 +341,8 @@ def connect( :param Optional[int] conntype: Raises an exception if set to 3, unused otherwise, defaults to None. """ - assert ( - conntype != 0x03 - ), "Error: SSL/TLS is not currently supported by CircuitPython." + if (conntype == 0x03): + raise AssertionError("Error: SSL/TLS is not currently supported by CircuitPython.") host, port = address if hasattr(host, "split"): @@ -565,7 +565,8 @@ def readline(self) -> bytes: def disconnect(self) -> None: """Disconnect a TCP socket.""" - assert self._sock_type == SOCK_STREAM, "Socket must be a TCP socket." + if (self._sock_type != SOCK_STREAM): + raise AssertionError("Socket must be a TCP socket.") _the_interface.socket_disconnect(self.socknum) def close(self) -> None: From a90d1f1c86af7b3e7ab06c91a34ce4495e9186b2 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Tue, 20 Dec 2022 16:31:02 +0200 Subject: [PATCH 2/5] forgor black --- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 8 +++++--- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index dcb10b5..4ccf289 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -274,9 +274,11 @@ def parse_dhcp_response( # -- Parse Packet, FIXED -- # # Validate OP - if (_BUFF[0] != DHCP_BOOT_REPLY): - raise AssertionError("Malformed Packet - \ - DHCP message OP is not expected BOOT Reply.") + if _BUFF[0] != DHCP_BOOT_REPLY: + raise AssertionError( + "Malformed Packet - \ + DHCP message OP is not expected BOOT Reply." + ) xid = _BUFF[4:8] if bytes(xid) < self._initial_xid: diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index e5c9fb2..aa9a531 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -283,7 +283,7 @@ def listen(self, backlog: Optional[int] = None) -> None: :param Optional[int] backlog: Included for compatibility but ignored. """ - if (self._listen_port is None): + if self._listen_port is None: raise AssertionError("Use bind to set the port before listen!") _the_interface.socket_listen(self.socknum, self._listen_port) self._buffer = b"" @@ -341,8 +341,10 @@ def connect( :param Optional[int] conntype: Raises an exception if set to 3, unused otherwise, defaults to None. """ - if (conntype == 0x03): - raise AssertionError("Error: SSL/TLS is not currently supported by CircuitPython.") + if conntype == 0x03: + raise AssertionError( + "Error: SSL/TLS is not currently supported by CircuitPython." + ) host, port = address if hasattr(host, "split"): @@ -565,7 +567,7 @@ def readline(self) -> bytes: def disconnect(self) -> None: """Disconnect a TCP socket.""" - if (self._sock_type != SOCK_STREAM): + if self._sock_type != SOCK_STREAM: raise AssertionError("Socket must be a TCP socket.") _the_interface.socket_disconnect(self.socknum) From a3bdc8d1871697a4cf1c18bf3947b259dc5752a4 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Tue, 20 Dec 2022 16:50:07 +0200 Subject: [PATCH 3/5] fix ci checks --- adafruit_wiznet5k/adafruit_wiznet5k.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index e4e6384..4cff1ed 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -872,7 +872,7 @@ def socket_disconnect(self, socket_num: int) -> None: self._write_sncr(socket_num, CMD_SOCK_DISCON) self._read_sncr(socket_num) - def socket_read( + def socket_read( # pylint: disable=too-many-branches self, socket_num: int, length: int ) -> Tuple[int, Union[int, bytearray]]: """ @@ -886,6 +886,7 @@ def socket_read( was unsuccessful then both items equal an error code, 0 for no data waiting and -1 for no connection to the socket. """ + if not self.link_status: raise AssertionError("Ethernet cable disconnected!") if socket_num > self.max_sockets: From 00cab19bedd177e74a049e517955b3efbbb47ec9 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Tue, 20 Dec 2022 16:58:18 +0200 Subject: [PATCH 4/5] Update all exceptions --- adafruit_wiznet5k/adafruit_wiznet5k.py | 30 +++++++++---------- adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py | 2 +- adafruit_wiznet5k/adafruit_wiznet5k_socket.py | 8 ++--- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index 4cff1ed..d1a112d 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -188,7 +188,7 @@ def __init__( # attempt to initialize the module self._ch_base_msb = 0 if self._w5100_init() != 1: - raise AssertionError("Failed to initialize WIZnet module.") + raise RuntimeError("Failed to initialize WIZnet module.") # Set MAC address self.mac_address = mac self.src_port = 0 @@ -216,7 +216,7 @@ def __init__( if ret != 0: self._dhcp_client = None if ret != 0: - raise AssertionError("Failed to configure DHCP Server!") + raise RuntimeError("Failed to configure DHCP Server!") def set_dhcp( self, hostname: Optional[str] = None, response_timeout: float = 30 @@ -275,7 +275,7 @@ def get_host_by_name(self, hostname: str) -> bytes: if self._debug: print("* Resolved IP: ", ret) if ret == -1: - raise AssertionError("Failed to resolve hostname!") + raise RuntimeError("Failed to resolve hostname!") return ret @property @@ -485,7 +485,7 @@ def detect_w5500(self) -> int: """ self._chip_type = "w5500" if self.sw_reset() != 0: - raise AssertionError("Chip not reset properly!") + raise RuntimeError("Chip not reset properly!") self._write_mr(0x08) # assert self._read_mr()[0] == 0x08, "Expected 0x08." if self._read_mr()[0] != 0x08: @@ -516,7 +516,7 @@ def detect_w5100s(self) -> int: self._chip_type = "w5100s" # sw reset if self.sw_reset() != 0: - raise AssertionError("Chip not reset properly!") + raise RuntimeError("Chip not reset properly!") if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51: return -1 @@ -629,7 +629,7 @@ def socket_available(self, socket_num: int, sock_type: int = SNMR_TCP) -> int: ) ) if socket_num > self.max_sockets: - raise AssertionError("Provided socket exceeds max_sockets.") + raise ValueError("Provided socket exceeds max_sockets.") res = self._get_rx_rcv_size(socket_num) @@ -683,7 +683,7 @@ def socket_connect( defaults to SNMR_TCP. """ if not self.link_status: - raise AssertionError("Ethernet cable disconnected!") + raise ConnectionError("Ethernet cable disconnected!") if self._debug: print( "* w5k socket connect, protocol={}, port={}, ip={}".format( @@ -693,7 +693,7 @@ def socket_connect( # initialize a socket and set the mode res = self.socket_open(socket_num, conn_mode=conn_mode) if res == 1: - raise RuntimeError("Failed to initialize a connection with the socket.") + raise ConnectionError("Failed to initialize a connection with the socket.") # set socket destination IP and port self._write_sndipr(socket_num, dest) @@ -707,7 +707,7 @@ def socket_connect( if self._debug: print("SN_SR:", self.socket_status(socket_num)[0]) if self.socket_status(socket_num)[0] == SNSR_SOCK_CLOSED: - raise RuntimeError("Failed to establish connection.") + raise ConnectionError("Failed to establish connection.") elif conn_mode == SNMR_UDP: self.udp_datasize[socket_num] = 0 return 1 @@ -752,7 +752,7 @@ def socket_listen( UDP, defaults to SNMR_TCP. """ if not self.link_status: - raise AssertionError("Ethernet cable disconnected!") + raise ConnectionError("Ethernet cable disconnected!") if self._debug: print( "* Listening on port={}, ip={}".format( @@ -813,7 +813,7 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int: :return int: 1 if the socket was opened, 0 if not. """ if not self.link_status: - raise AssertionError("Ethernet cable disconnected!") + raise ConnectionError("Ethernet cable disconnected!") if self._debug: print("*** Opening socket %d" % socket_num) status = self._read_snsr(socket_num)[0] @@ -846,7 +846,7 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int: self._write_sncr(socket_num, CMD_SOCK_OPEN) self._read_sncr(socket_num) if self._read_snsr((socket_num))[0] not in [0x13, 0x22]: - raise AssertionError("Could not open socket in TCP or UDP mode.") + raise RuntimeError("Could not open socket in TCP or UDP mode.") return 0 return 1 @@ -888,9 +888,9 @@ def socket_read( # pylint: disable=too-many-branches """ if not self.link_status: - raise AssertionError("Ethernet cable disconnected!") + raise ConnectionError("Ethernet cable disconnected!") if socket_num > self.max_sockets: - raise AssertionError("Provided socket exceeds max_sockets.") + raise ValueError("Provided socket exceeds max_sockets.") # Check if there is data available on the socket ret = self._get_rx_rcv_size(socket_num) @@ -984,7 +984,7 @@ def socket_write( :return int: The number of bytes written to the buffer. """ if not self.link_status: - raise AssertionError("Ethernet cable disconnected!") + raise ConnectionError("Ethernet cable disconnected!") assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets." status = 0 ret = 0 diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py index 4ccf289..b07ff9d 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py @@ -275,7 +275,7 @@ def parse_dhcp_response( # -- Parse Packet, FIXED -- # # Validate OP if _BUFF[0] != DHCP_BOOT_REPLY: - raise AssertionError( + raise RuntimeError( "Malformed Packet - \ DHCP message OP is not expected BOOT Reply." ) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py index aa9a531..b34cb08 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k_socket.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k_socket.py @@ -101,7 +101,7 @@ def getaddrinfo( :return List[Tuple[int, int, int, str, Tuple[str, int]]]: Address info entries. """ if not isinstance(port, int): - raise RuntimeError("Port must be an integer") + raise ValueError("Port must be an integer") if is_ipv4(host): return [(AF_INET, socktype, proto, "", (host, port))] return [(AF_INET, socktype, proto, "", (gethostbyname(host), port))] @@ -284,7 +284,7 @@ def listen(self, backlog: Optional[int] = None) -> None: :param Optional[int] backlog: Included for compatibility but ignored. """ if self._listen_port is None: - raise AssertionError("Use bind to set the port before listen!") + raise RuntimeError("Use bind to set the port before listen!") _the_interface.socket_listen(self.socknum, self._listen_port) self._buffer = b"" @@ -342,7 +342,7 @@ def connect( to None. """ if conntype == 0x03: - raise AssertionError( + raise NotImplementedError( "Error: SSL/TLS is not currently supported by CircuitPython." ) host, port = address @@ -568,7 +568,7 @@ def readline(self) -> bytes: def disconnect(self) -> None: """Disconnect a TCP socket.""" if self._sock_type != SOCK_STREAM: - raise AssertionError("Socket must be a TCP socket.") + raise RuntimeError("Socket must be a TCP socket.") _the_interface.socket_disconnect(self.socknum) def close(self) -> None: From d9a975944b29afac92c1d23ff35d540a9745597a Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Tue, 20 Dec 2022 22:02:38 +0200 Subject: [PATCH 5/5] fix changes --- adafruit_wiznet5k/adafruit_wiznet5k.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_wiznet5k/adafruit_wiznet5k.py b/adafruit_wiznet5k/adafruit_wiznet5k.py index f6b70b3..9bb1f57 100644 --- a/adafruit_wiznet5k/adafruit_wiznet5k.py +++ b/adafruit_wiznet5k/adafruit_wiznet5k.py @@ -187,7 +187,7 @@ def __init__( # attempt to initialize the module self._ch_base_msb = 0 - if self._w5100_init() != 1: + if self._w5xxx_init() != 1: raise RuntimeError("Failed to initialize WIZnet module.") # Set MAC address self.mac_address = mac