@@ -659,6 +659,18 @@ def get(self, option: C.int):
659
659
return rc
660
660
661
661
662
+ @cfunc
663
+ @inline
664
+ def _c_addr (addr ) -> p_char :
665
+ if isinstance (addr , str ):
666
+ addr = addr .encode ('utf-8' )
667
+ try :
668
+ c_addr : p_char = addr
669
+ except TypeError :
670
+ raise TypeError (f"Expected addr to be str, got addr={ addr !r} " )
671
+ return c_addr
672
+
673
+
662
674
@cclass
663
675
class Socket :
664
676
"""
@@ -908,16 +920,7 @@ def bind(self, addr: str | bytes):
908
920
tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
909
921
encoded to utf-8 first.
910
922
"""
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
920
-
923
+ c_addr : p_char = _c_addr (addr )
921
924
_check_closed (self )
922
925
rc : C .int = zmq_bind (self .handle , c_addr )
923
926
if rc != 0 :
@@ -957,13 +960,7 @@ def connect(self, addr: str | bytes) -> None:
957
960
encoded to utf-8 first.
958
961
"""
959
962
rc : C .int
960
- if isinstance (addr , str ):
961
- addr = addr .encode ('utf-8' )
962
- try :
963
- c_addr : p_char = addr
964
- except TypeError :
965
- raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
966
-
963
+ c_addr : p_char = _c_addr (addr )
967
964
_check_closed (self )
968
965
969
966
while True :
@@ -991,14 +988,8 @@ def unbind(self, addr: str | bytes):
991
988
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
992
989
encoded to utf-8 first.
993
990
"""
994
-
995
- try :
996
- c_addr : p_char = addr
997
- except TypeError :
998
- raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
999
-
991
+ c_addr : p_char = _c_addr (addr )
1000
992
_check_closed (self )
1001
-
1002
993
rc : C .int = zmq_unbind (self .handle , c_addr )
1003
994
if rc != 0 :
1004
995
raise ZMQError ()
@@ -1018,13 +1009,7 @@ def disconnect(self, addr: str | bytes):
1018
1009
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
1019
1010
encoded to utf-8 first.
1020
1011
"""
1021
- if isinstance (addr , str ):
1022
- addr = addr .encode ('utf-8' )
1023
- try :
1024
- c_addr : p_char = addr
1025
- except TypeError :
1026
- raise TypeError (f"Expected addr to be str, got { addr !r} " ) from None
1027
-
1012
+ c_addr : p_char = _c_addr (addr )
1028
1013
_check_closed (self )
1029
1014
1030
1015
rc : C .int = zmq_disconnect (self .handle , c_addr )
@@ -1052,23 +1037,10 @@ def monitor(self, addr: str | bytes | None, events: C.int = ZMQ_EVENT_ALL):
1052
1037
default: zmq.EVENT_ALL
1053
1038
The zmq event bitmask for which events will be sent to the monitor.
1054
1039
"""
1055
-
1056
- if isinstance (addr , str ):
1057
- # cast str to utf8 bytes
1058
- addr = addr .encode ("utf-8" )
1059
-
1060
- # cast bytes to char*
1061
- c_addr : p_char
1062
-
1063
- if addr is None :
1064
- c_addr = NULL
1065
- else :
1066
- # let Cython do the casting,
1067
- # but return a nicer error message if it fails
1068
- try :
1069
- c_addr = addr
1070
- except TypeError :
1071
- raise TypeError (f"Monitor addr must be str, got { addr !r} " ) from None
1040
+ c_addr : p_char = NULL
1041
+ if addr is not None :
1042
+ c_addr = _c_addr (addr )
1043
+ _check_closed (self )
1072
1044
1073
1045
_check_rc (zmq_socket_monitor (self .handle , c_addr , events ))
1074
1046
0 commit comments