Skip to content

Remove assert's #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions adafruit_wiznet5k/adafruit_wiznet5k.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions adafruit_wiznet5k/adafruit_wiznet5k_dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,11 @@ def parse_dhcp_response(

# -- Parse Packet, FIXED -- #
# Validate OP
assert (
_BUFF[0] == DHCP_BOOT_REPLY
), "Malformed Packet - \
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:
Expand Down
13 changes: 8 additions & 5 deletions adafruit_wiznet5k/adafruit_wiznet5k_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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""

Expand Down Expand Up @@ -340,9 +341,10 @@ 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"):
Expand Down Expand Up @@ -565,7 +567,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:
Expand Down