Skip to content

Commit c3408ab

Browse files
committed
Merge branch 'main' into _GLOBAL_constants
# Conflicts: # adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py # adafruit_wiznet5k/adafruit_wiznet5k_socket.py
2 parents d35bff0 + f97e78c commit c3408ab

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def __init__(
187187

188188
# attempt to initialize the module
189189
self._ch_base_msb = 0
190-
assert self._w5xxx_init() == 1, "Failed to initialize WIZnet module."
190+
if self._w5xxx_init() != 1:
191+
raise RuntimeError("Failed to initialize WIZnet module.")
191192
# Set MAC address
192193
self.mac_address = mac
193194
self.src_port = 0
@@ -214,7 +215,8 @@ def __init__(
214215
ret = self.set_dhcp(hostname, dhcp_timeout)
215216
if ret != 0:
216217
self._dhcp_client = None
217-
assert ret == 0, "Failed to configure DHCP Server!"
218+
if ret != 0:
219+
raise RuntimeError("Failed to configure DHCP Server!")
218220

219221
def set_dhcp(
220222
self, hostname: Optional[str] = None, response_timeout: float = 30
@@ -272,7 +274,8 @@ def get_host_by_name(self, hostname: str) -> bytes:
272274
ret = _dns_client.gethostbyname(hostname)
273275
if self._debug:
274276
print("* Resolved IP: ", ret)
275-
assert ret != -1, "Failed to resolve hostname!"
277+
if ret == -1:
278+
raise RuntimeError("Failed to resolve hostname!")
276279
return ret
277280

278281
@property
@@ -626,7 +629,8 @@ def socket_available(self, socket_num: int, sock_type: int = _SNMR_TCP) -> int:
626629
socket_num, sock_type
627630
)
628631
)
629-
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
632+
if socket_num > self.max_sockets:
633+
raise ValueError("Provided socket exceeds max_sockets.")
630634

631635
res = self._get_rx_rcv_size(socket_num)
632636

@@ -679,7 +683,8 @@ def socket_connect(
679683
:param int conn_mode: The connection mode. Use SNMR_TCP for TCP or SNMR_UDP for UDP,
680684
defaults to SNMR_TCP.
681685
"""
682-
assert self.link_status, "Ethernet cable disconnected!"
686+
if not self.link_status:
687+
raise ConnectionError("Ethernet cable disconnected!")
683688
if self._debug:
684689
print(
685690
"* w5k socket connect, protocol={}, port={}, ip={}".format(
@@ -689,7 +694,7 @@ def socket_connect(
689694
# initialize a socket and set the mode
690695
res = self.socket_open(socket_num, conn_mode=conn_mode)
691696
if res == 1:
692-
raise RuntimeError("Failed to initialize a connection with the socket.")
697+
raise ConnectionError("Failed to initialize a connection with the socket.")
693698

694699
# set socket destination IP and port
695700
self._write_sndipr(socket_num, dest)
@@ -703,7 +708,7 @@ def socket_connect(
703708
if self._debug:
704709
print("SN_SR:", self.socket_status(socket_num)[0])
705710
if self.socket_status(socket_num)[0] == SNSR_SOCK_CLOSED:
706-
raise RuntimeError("Failed to establish connection.")
711+
raise ConnectionError("Failed to establish connection.")
707712
elif conn_mode == SNMR_UDP:
708713
self.udp_datasize[socket_num] = 0
709714
return 1
@@ -747,7 +752,8 @@ def socket_listen(
747752
:param int conn_mode: Connection mode SNMR_TCP for TCP or SNMR_UDP for
748753
UDP, defaults to SNMR_TCP.
749754
"""
750-
assert self.link_status, "Ethernet cable disconnected!"
755+
if not self.link_status:
756+
raise ConnectionError("Ethernet cable disconnected!")
751757
if self._debug:
752758
print(
753759
"* Listening on port={}, ip={}".format(
@@ -811,7 +817,8 @@ def socket_open(self, socket_num: int, conn_mode: int = _SNMR_TCP) -> int:
811817
UDP, defaults to SNMR_TCP.
812818
:return int: 1 if the socket was opened, 0 if not.
813819
"""
814-
assert self.link_status, "Ethernet cable disconnected!"
820+
if not self.link_status:
821+
raise ConnectionError("Ethernet cable disconnected!")
815822
if self._debug:
816823
print("*** Opening socket %d" % socket_num)
817824
status = self._read_snsr(socket_num)[0]
@@ -843,10 +850,8 @@ def socket_open(self, socket_num: int, conn_mode: int = _SNMR_TCP) -> int:
843850
# open socket
844851
self._write_sncr(socket_num, _CMD_SOCK_OPEN)
845852
self._read_sncr(socket_num)
846-
assert (
847-
self._read_snsr((socket_num))[0] == 0x13
848-
or self._read_snsr((socket_num))[0] == 0x22
849-
), "Could not open socket in TCP or UDP mode."
853+
if self._read_snsr((socket_num))[0] not in [0x13, 0x22]:
854+
raise RuntimeError("Could not open socket in TCP or UDP mode.")
850855
return 0
851856
return 1
852857

@@ -872,7 +877,7 @@ def socket_disconnect(self, socket_num: int) -> None:
872877
self._write_sncr(socket_num, _CMD_SOCK_DISCON)
873878
self._read_sncr(socket_num)
874879

875-
def socket_read(
880+
def socket_read( # pylint: disable=too-many-branches
876881
self, socket_num: int, length: int
877882
) -> Tuple[int, Union[int, bytearray]]:
878883
"""
@@ -886,8 +891,11 @@ def socket_read(
886891
was unsuccessful then both items equal an error code, 0 for no data waiting and -1
887892
for no connection to the socket.
888893
"""
889-
assert self.link_status, "Ethernet cable disconnected!"
890-
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
894+
895+
if not self.link_status:
896+
raise ConnectionError("Ethernet cable disconnected!")
897+
if socket_num > self.max_sockets:
898+
raise ValueError("Provided socket exceeds max_sockets.")
891899

892900
# Check if there is data available on the socket
893901
ret = self._get_rx_rcv_size(socket_num)
@@ -980,7 +988,8 @@ def socket_write(
980988
981989
:return int: The number of bytes written to the buffer.
982990
"""
983-
assert self.link_status, "Ethernet cable disconnected!"
991+
if not self.link_status:
992+
raise ConnectionError("Ethernet cable disconnected!")
984993
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
985994
status = 0
986995
ret = 0

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,11 @@ def parse_dhcp_response(
274274

275275
# -- Parse Packet, FIXED -- #
276276
# Validate OP
277-
assert (
278-
_BUFF[0] == _DHCP_BOOT_REPLY
279-
), "Malformed Packet - \
277+
if _BUFF[0] != _DHCP_BOOT_REPLY:
278+
raise RuntimeError(
279+
"Malformed Packet - \
280280
DHCP message OP is not expected BOOT Reply."
281+
)
281282

282283
xid = _BUFF[4:8]
283284
if bytes(xid) < self._initial_xid:

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def getaddrinfo(
101101
:return List[Tuple[int, int, int, str, Tuple[str, int]]]: Address info entries.
102102
"""
103103
if not isinstance(port, int):
104-
raise RuntimeError("Port must be an integer")
104+
raise ValueError("Port must be an integer")
105105
if is_ipv4(host):
106106
return [(_AF_INET, socktype, proto, "", (host, port))]
107107
return [(_AF_INET, socktype, proto, "", (gethostbyname(host), port))]
@@ -285,7 +285,8 @@ def listen(self, backlog: Optional[int] = None) -> None:
285285
286286
:param Optional[int] backlog: Included for compatibility but ignored.
287287
"""
288-
assert self._listen_port is not None, "Use bind to set the port before listen!"
288+
if self._listen_port is None:
289+
raise RuntimeError("Use bind to set the port before listen!")
289290
_the_interface.socket_listen(self.socknum, self._listen_port)
290291
self._buffer = b""
291292

@@ -342,9 +343,10 @@ def connect(
342343
:param Optional[int] conntype: Raises an exception if set to 3, unused otherwise, defaults
343344
to None.
344345
"""
345-
assert (
346-
conntype != 0x03
347-
), "Error: SSL/TLS is not currently supported by CircuitPython."
346+
if conntype == 0x03:
347+
raise NotImplementedError(
348+
"Error: SSL/TLS is not currently supported by CircuitPython."
349+
)
348350
host, port = address
349351

350352
if hasattr(host, "split"):
@@ -567,7 +569,8 @@ def readline(self) -> bytes:
567569

568570
def disconnect(self) -> None:
569571
"""Disconnect a TCP socket."""
570-
assert self._sock_type == _SOCK_STREAM, "Socket must be a TCP socket."
572+
if self._sock_type != _SOCK_STREAM:
573+
raise RuntimeError("Socket must be a TCP socket.")
571574
_the_interface.socket_disconnect(self.socknum)
572575

573576
def close(self) -> None:

0 commit comments

Comments
 (0)