@@ -187,7 +187,8 @@ def __init__(
187
187
188
188
# attempt to initialize the module
189
189
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." )
191
192
# Set MAC address
192
193
self .mac_address = mac
193
194
self .src_port = 0
@@ -214,7 +215,8 @@ def __init__(
214
215
ret = self .set_dhcp (hostname , dhcp_timeout )
215
216
if ret != 0 :
216
217
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!" )
218
220
219
221
def set_dhcp (
220
222
self , hostname : Optional [str ] = None , response_timeout : float = 30
@@ -272,7 +274,8 @@ def get_host_by_name(self, hostname: str) -> bytes:
272
274
ret = _dns_client .gethostbyname (hostname )
273
275
if self ._debug :
274
276
print ("* Resolved IP: " , ret )
275
- assert ret != - 1 , "Failed to resolve hostname!"
277
+ if ret == - 1 :
278
+ raise RuntimeError ("Failed to resolve hostname!" )
276
279
return ret
277
280
278
281
@property
@@ -626,7 +629,8 @@ def socket_available(self, socket_num: int, sock_type: int = SNMR_TCP) -> int:
626
629
socket_num , sock_type
627
630
)
628
631
)
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." )
630
634
631
635
res = self ._get_rx_rcv_size (socket_num )
632
636
@@ -679,7 +683,8 @@ def socket_connect(
679
683
:param int conn_mode: The connection mode. Use SNMR_TCP for TCP or SNMR_UDP for UDP,
680
684
defaults to SNMR_TCP.
681
685
"""
682
- assert self .link_status , "Ethernet cable disconnected!"
686
+ if not self .link_status :
687
+ raise ConnectionError ("Ethernet cable disconnected!" )
683
688
if self ._debug :
684
689
print (
685
690
"* w5k socket connect, protocol={}, port={}, ip={}" .format (
@@ -689,7 +694,7 @@ def socket_connect(
689
694
# initialize a socket and set the mode
690
695
res = self .socket_open (socket_num , conn_mode = conn_mode )
691
696
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." )
693
698
694
699
# set socket destination IP and port
695
700
self ._write_sndipr (socket_num , dest )
@@ -703,7 +708,7 @@ def socket_connect(
703
708
if self ._debug :
704
709
print ("SN_SR:" , self .socket_status (socket_num )[0 ])
705
710
if self .socket_status (socket_num )[0 ] == SNSR_SOCK_CLOSED :
706
- raise RuntimeError ("Failed to establish connection." )
711
+ raise ConnectionError ("Failed to establish connection." )
707
712
elif conn_mode == SNMR_UDP :
708
713
self .udp_datasize [socket_num ] = 0
709
714
return 1
@@ -747,7 +752,8 @@ def socket_listen(
747
752
:param int conn_mode: Connection mode SNMR_TCP for TCP or SNMR_UDP for
748
753
UDP, defaults to SNMR_TCP.
749
754
"""
750
- assert self .link_status , "Ethernet cable disconnected!"
755
+ if not self .link_status :
756
+ raise ConnectionError ("Ethernet cable disconnected!" )
751
757
if self ._debug :
752
758
print (
753
759
"* Listening on port={}, ip={}" .format (
@@ -807,7 +813,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
807
813
UDP, defaults to SNMR_TCP.
808
814
:return int: 1 if the socket was opened, 0 if not.
809
815
"""
810
- assert self .link_status , "Ethernet cable disconnected!"
816
+ if not self .link_status :
817
+ raise ConnectionError ("Ethernet cable disconnected!" )
811
818
if self ._debug :
812
819
print ("*** Opening socket %d" % socket_num )
813
820
status = self ._read_snsr (socket_num )[0 ]
@@ -839,10 +846,8 @@ def socket_open(self, socket_num: int, conn_mode: int = SNMR_TCP) -> int:
839
846
# open socket
840
847
self ._write_sncr (socket_num , CMD_SOCK_OPEN )
841
848
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." )
846
851
return 0
847
852
return 1
848
853
@@ -868,7 +873,7 @@ def socket_disconnect(self, socket_num: int) -> None:
868
873
self ._write_sncr (socket_num , CMD_SOCK_DISCON )
869
874
self ._read_sncr (socket_num )
870
875
871
- def socket_read (
876
+ def socket_read ( # pylint: disable=too-many-branches
872
877
self , socket_num : int , length : int
873
878
) -> Tuple [int , Union [int , bytearray ]]:
874
879
"""
@@ -882,8 +887,11 @@ def socket_read(
882
887
was unsuccessful then both items equal an error code, 0 for no data waiting and -1
883
888
for no connection to the socket.
884
889
"""
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." )
887
895
888
896
# Check if there is data available on the socket
889
897
ret = self ._get_rx_rcv_size (socket_num )
@@ -976,7 +984,8 @@ def socket_write(
976
984
977
985
:return int: The number of bytes written to the buffer.
978
986
"""
979
- assert self .link_status , "Ethernet cable disconnected!"
987
+ if not self .link_status :
988
+ raise ConnectionError ("Ethernet cable disconnected!" )
980
989
assert socket_num <= self .max_sockets , "Provided socket exceeds max_sockets."
981
990
status = 0
982
991
ret = 0
0 commit comments