Skip to content

Commit f97e78c

Browse files
authored
Merge pull request #85 from bill88t/main
Remove `assert`'s
2 parents 17c9ed8 + d9a9759 commit f97e78c

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

+26-17
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(
@@ -807,7 +813,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
807813
UDP, defaults to SNMR_TCP.
808814
:return int: 1 if the socket was opened, 0 if not.
809815
"""
810-
assert self.link_status, "Ethernet cable disconnected!"
816+
if not self.link_status:
817+
raise ConnectionError("Ethernet cable disconnected!")
811818
if self._debug:
812819
print("*** Opening socket %d" % socket_num)
813820
status = self._read_snsr(socket_num)[0]
@@ -839,10 +846,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
839846
# open socket
840847
self._write_sncr(socket_num, CMD_SOCK_OPEN)
841848
self._read_sncr(socket_num)
842-
assert (
843-
self._read_snsr((socket_num))[0] == 0x13
844-
or self._read_snsr((socket_num))[0] == 0x22
845-
), "Could not open socket in TCP or UDP mode."
849+
if self._read_snsr((socket_num))[0] not in [0x13, 0x22]:
850+
raise RuntimeError("Could not open socket in TCP or UDP mode.")
846851
return 0
847852
return 1
848853

@@ -868,7 +873,7 @@ def socket_disconnect(self, socket_num: int) -> None:
868873
self._write_sncr(socket_num, CMD_SOCK_DISCON)
869874
self._read_sncr(socket_num)
870875

871-
def socket_read(
876+
def socket_read( # pylint: disable=too-many-branches
872877
self, socket_num: int, length: int
873878
) -> Tuple[int, Union[int, bytearray]]:
874879
"""
@@ -882,8 +887,11 @@ def socket_read(
882887
was unsuccessful then both items equal an error code, 0 for no data waiting and -1
883888
for no connection to the socket.
884889
"""
885-
assert self.link_status, "Ethernet cable disconnected!"
886-
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
890+
891+
if not self.link_status:
892+
raise ConnectionError("Ethernet cable disconnected!")
893+
if socket_num > self.max_sockets:
894+
raise ValueError("Provided socket exceeds max_sockets.")
887895

888896
# Check if there is data available on the socket
889897
ret = self._get_rx_rcv_size(socket_num)
@@ -976,7 +984,8 @@ def socket_write(
976984
977985
:return int: The number of bytes written to the buffer.
978986
"""
979-
assert self.link_status, "Ethernet cable disconnected!"
987+
if not self.link_status:
988+
raise ConnectionError("Ethernet cable disconnected!")
980989
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
981990
status = 0
982991
ret = 0

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

+4-3
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

+9-6
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))]
@@ -283,7 +283,8 @@ def listen(self, backlog: Optional[int] = None) -> None:
283283
284284
:param Optional[int] backlog: Included for compatibility but ignored.
285285
"""
286-
assert self._listen_port is not None, "Use bind to set the port before listen!"
286+
if self._listen_port is None:
287+
raise RuntimeError("Use bind to set the port before listen!")
287288
_the_interface.socket_listen(self.socknum, self._listen_port)
288289
self._buffer = b""
289290

@@ -340,9 +341,10 @@ def connect(
340341
:param Optional[int] conntype: Raises an exception if set to 3, unused otherwise, defaults
341342
to None.
342343
"""
343-
assert (
344-
conntype != 0x03
345-
), "Error: SSL/TLS is not currently supported by CircuitPython."
344+
if conntype == 0x03:
345+
raise NotImplementedError(
346+
"Error: SSL/TLS is not currently supported by CircuitPython."
347+
)
346348
host, port = address
347349

348350
if hasattr(host, "split"):
@@ -565,7 +567,8 @@ def readline(self) -> bytes:
565567

566568
def disconnect(self) -> None:
567569
"""Disconnect a TCP socket."""
568-
assert self._sock_type == SOCK_STREAM, "Socket must be a TCP socket."
570+
if self._sock_type != SOCK_STREAM:
571+
raise RuntimeError("Socket must be a TCP socket.")
569572
_the_interface.socket_disconnect(self.socknum)
570573

571574
def close(self) -> None:

0 commit comments

Comments
 (0)