@@ -892,7 +892,7 @@ def get(self, option: C.int):
892
892
893
893
return result
894
894
895
- def bind (self , addr : str ):
895
+ def bind (self , addr : str | bytes ):
896
896
"""
897
897
Bind the socket to an address.
898
898
@@ -908,14 +908,21 @@ def bind(self, addr: str):
908
908
tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
909
909
encoded to utf-8 first.
910
910
"""
911
- rc : C .int
912
- b_addr : bytes = addr .encode ('utf-8' )
913
- c_addr : p_char = b_addr
911
+ b_addr : bytes
912
+ if isinstance (addr , str ):
913
+ b_addr = addr .encode ('utf-8' )
914
+ else :
915
+ b_addr = addr
916
+ try :
917
+ c_addr : p_char = b_addr
918
+ except TypeError :
919
+ raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
914
920
915
921
_check_closed (self )
916
- rc = zmq_bind (self .handle , c_addr )
922
+ rc : C . int = zmq_bind (self .handle , c_addr )
917
923
if rc != 0 :
918
- if IPC_PATH_MAX_LEN and _zmq_errno () == ENAMETOOLONG :
924
+ _errno : C .int = _zmq_errno ()
925
+ if IPC_PATH_MAX_LEN and _errno == ENAMETOOLONG :
919
926
path = addr .split ('://' , 1 )[- 1 ]
920
927
msg = (
921
928
f'ipc path "{ path } " is longer than { IPC_PATH_MAX_LEN } '
@@ -924,7 +931,7 @@ def bind(self, addr: str):
924
931
'to check addr length (if it is defined).'
925
932
)
926
933
raise ZMQError (msg = msg )
927
- elif _zmq_errno () == ENOENT :
934
+ elif _errno == ENOENT :
928
935
path = addr .split ('://' , 1 )[- 1 ]
929
936
msg = f'No such file or directory for ipc path "{ path } ".'
930
937
raise ZMQError (msg = msg )
@@ -937,7 +944,7 @@ def bind(self, addr: str):
937
944
else :
938
945
break
939
946
940
- def connect (self , addr : str ) -> None :
947
+ def connect (self , addr : str | bytes ) -> None :
941
948
"""
942
949
Connect to a remote 0MQ socket.
943
950
@@ -950,8 +957,10 @@ def connect(self, addr: str) -> None:
950
957
encoded to utf-8 first.
951
958
"""
952
959
rc : C .int
953
- b_addr : bytes = addr .encode ('utf-8' )
954
- c_addr : p_char = b_addr
960
+ try :
961
+ c_addr : p_char = addr
962
+ except TypeError :
963
+ raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
955
964
956
965
_check_closed (self )
957
966
@@ -965,7 +974,7 @@ def connect(self, addr: str) -> None:
965
974
else :
966
975
break
967
976
968
- def unbind (self , addr ):
977
+ def unbind (self , addr : str | bytes ):
969
978
"""
970
979
Unbind from an address (undoes a call to bind).
971
980
@@ -980,21 +989,19 @@ def unbind(self, addr):
980
989
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
981
990
encoded to utf-8 first.
982
991
"""
983
- rc : C .int
984
- c_addr : p_char
992
+
993
+ try :
994
+ c_addr : p_char = addr
995
+ except TypeError :
996
+ raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
985
997
986
998
_check_closed (self )
987
- if isinstance (addr , str ):
988
- addr = addr .encode ('utf-8' )
989
- if not isinstance (addr , bytes ):
990
- raise TypeError (f'expected str, got: { addr !r} ' )
991
- c_addr = addr
992
999
993
- rc = zmq_unbind (self .handle , c_addr )
1000
+ rc : C . int = zmq_unbind (self .handle , c_addr )
994
1001
if rc != 0 :
995
1002
raise ZMQError ()
996
1003
997
- def disconnect (self , addr ):
1004
+ def disconnect (self , addr : str | bytes ):
998
1005
"""
999
1006
Disconnect from a remote 0MQ socket (undoes a call to connect).
1000
1007
@@ -1009,21 +1016,20 @@ def disconnect(self, addr):
1009
1016
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
1010
1017
encoded to utf-8 first.
1011
1018
"""
1012
- rc : C .int
1013
- c_addr : p_char
1014
-
1015
- _check_closed (self )
1016
1019
if isinstance (addr , str ):
1017
1020
addr = addr .encode ('utf-8' )
1018
- if not isinstance (addr , bytes ):
1019
- raise TypeError (f'expected str, got: { addr !r} ' )
1020
- c_addr = addr
1021
+ try :
1022
+ c_addr : p_char = addr
1023
+ except TypeError :
1024
+ raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
1025
+
1026
+ _check_closed (self )
1021
1027
1022
- rc = zmq_disconnect (self .handle , c_addr )
1028
+ rc : C . int = zmq_disconnect (self .handle , c_addr )
1023
1029
if rc != 0 :
1024
1030
raise ZMQError ()
1025
1031
1026
- def monitor (self , addr , events : C .int = ZMQ_EVENT_ALL ):
1032
+ def monitor (self , addr : str | bytes | None , events : C .int = ZMQ_EVENT_ALL ):
1027
1033
"""
1028
1034
Start publishing socket events on inproc.
1029
1035
See libzmq docs for zmq_monitor for details.
@@ -1036,7 +1042,7 @@ def monitor(self, addr, events: C.int = ZMQ_EVENT_ALL):
1036
1042
1037
1043
Parameters
1038
1044
----------
1039
- addr : str
1045
+ addr : str | None
1040
1046
The inproc url used for monitoring. Passing None as
1041
1047
the addr will cause an existing socket monitor to be
1042
1048
deregistered.
0 commit comments