Skip to content

Commit ff6cd73

Browse files
committed
Remove all non-comment assert's
1 parent 2d7a38e commit ff6cd73

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

adafruit_wiznet5k/adafruit_wiznet5k.py

Lines changed: 26 additions & 16 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._w5100_init() == 1, "Failed to initialize WIZnet module."
190+
if self._w5100_init() != 1:
191+
raise AssertionError("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 AssertionError("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 AssertionError("Failed to resolve hostname!")
276279
return ret
277280

278281
@property
@@ -481,7 +484,8 @@ def detect_w5500(self) -> int:
481484
:return int: 1 if a W5500 chip is detected, -1 if not.
482485
"""
483486
self._chip_type = "w5500"
484-
assert self.sw_reset() == 0, "Chip not reset properly!"
487+
if self.sw_reset() != 0:
488+
raise AssertionError("Chip not reset properly!")
485489
self._write_mr(0x08)
486490
# assert self._read_mr()[0] == 0x08, "Expected 0x08."
487491
if self._read_mr()[0] != 0x08:
@@ -511,7 +515,8 @@ def detect_w5100s(self) -> int:
511515
"""
512516
self._chip_type = "w5100s"
513517
# sw reset
514-
assert self.sw_reset() == 0, "Chip not reset properly!"
518+
if self.sw_reset() != 0:
519+
raise AssertionError("Chip not reset properly!")
515520
if self.read(REG_VERSIONR_W5100S, 0x00)[0] != 0x51:
516521
return -1
517522

@@ -623,7 +628,8 @@ def socket_available(self, socket_num: int, sock_type: int = SNMR_TCP) -> int:
623628
socket_num, sock_type
624629
)
625630
)
626-
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
631+
if socket_num > self.max_sockets:
632+
raise AssertionError("Provided socket exceeds max_sockets.")
627633

628634
res = self._get_rx_rcv_size(socket_num)
629635

@@ -676,7 +682,8 @@ def socket_connect(
676682
:param int conn_mode: The connection mode. Use SNMR_TCP for TCP or SNMR_UDP for UDP,
677683
defaults to SNMR_TCP.
678684
"""
679-
assert self.link_status, "Ethernet cable disconnected!"
685+
if not self.link_status:
686+
raise AssertionError("Ethernet cable disconnected!")
680687
if self._debug:
681688
print(
682689
"* w5k socket connect, protocol={}, port={}, ip={}".format(
@@ -744,7 +751,8 @@ def socket_listen(
744751
:param int conn_mode: Connection mode SNMR_TCP for TCP or SNMR_UDP for
745752
UDP, defaults to SNMR_TCP.
746753
"""
747-
assert self.link_status, "Ethernet cable disconnected!"
754+
if not self.link_status:
755+
raise AssertionError("Ethernet cable disconnected!")
748756
if self._debug:
749757
print(
750758
"* Listening on port={}, ip={}".format(
@@ -804,7 +812,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
804812
UDP, defaults to SNMR_TCP.
805813
:return int: 1 if the socket was opened, 0 if not.
806814
"""
807-
assert self.link_status, "Ethernet cable disconnected!"
815+
if not self.link_status:
816+
raise AssertionError("Ethernet cable disconnected!")
808817
if self._debug:
809818
print("*** Opening socket %d" % socket_num)
810819
status = self._read_snsr(socket_num)[0]
@@ -836,10 +845,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
836845
# open socket
837846
self._write_sncr(socket_num, CMD_SOCK_OPEN)
838847
self._read_sncr(socket_num)
839-
assert (
840-
self._read_snsr((socket_num))[0] == 0x13
841-
or self._read_snsr((socket_num))[0] == 0x22
842-
), "Could not open socket in TCP or UDP mode."
848+
if self._read_snsr((socket_num))[0] not in [0x13, 0x22]:
849+
raise AssertionError("Could not open socket in TCP or UDP mode.")
843850
return 0
844851
return 1
845852

@@ -879,8 +886,10 @@ def socket_read(
879886
was unsuccessful then both items equal an error code, 0 for no data waiting and -1
880887
for no connection to the socket.
881888
"""
882-
assert self.link_status, "Ethernet cable disconnected!"
883-
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
889+
if not self.link_status:
890+
raise AssertionError("Ethernet cable disconnected!")
891+
if socket_num > self.max_sockets:
892+
raise AssertionError("Provided socket exceeds max_sockets.")
884893

885894
# Check if there is data available on the socket
886895
ret = self._get_rx_rcv_size(socket_num)
@@ -973,7 +982,8 @@ def socket_write(
973982
974983
:return int: The number of bytes written to the buffer.
975984
"""
976-
assert self.link_status, "Ethernet cable disconnected!"
985+
if not self.link_status:
986+
raise AssertionError("Ethernet cable disconnected!")
977987
assert socket_num <= self.max_sockets, "Provided socket exceeds max_sockets."
978988
status = 0
979989
ret = 0

adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py

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

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

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

adafruit_wiznet5k/adafruit_wiznet5k_socket.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 AssertionError("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,8 @@ 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 AssertionError("Error: SSL/TLS is not currently supported by CircuitPython.")
346346
host, port = address
347347

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

566566
def disconnect(self) -> None:
567567
"""Disconnect a TCP socket."""
568-
assert self._sock_type == SOCK_STREAM, "Socket must be a TCP socket."
568+
if (self._sock_type != SOCK_STREAM):
569+
raise AssertionError("Socket must be a TCP socket.")
569570
_the_interface.socket_disconnect(self.socknum)
570571

571572
def close(self) -> None:

0 commit comments

Comments
 (0)