Skip to content

Commit feae9ea

Browse files
committed
consolidate4 casting addr to char*
1 parent ca86fb5 commit feae9ea

File tree

1 file changed

+20
-48
lines changed

1 file changed

+20
-48
lines changed

zmq/backend/cython/_zmq.py

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,18 @@ def get(self, option: C.int):
659659
return rc
660660

661661

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+
662674
@cclass
663675
class Socket:
664676
"""
@@ -908,16 +920,7 @@ def bind(self, addr: str | bytes):
908920
tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
909921
encoded to utf-8 first.
910922
"""
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)
921924
_check_closed(self)
922925
rc: C.int = zmq_bind(self.handle, c_addr)
923926
if rc != 0:
@@ -957,13 +960,7 @@ def connect(self, addr: str | bytes) -> None:
957960
encoded to utf-8 first.
958961
"""
959962
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)
967964
_check_closed(self)
968965

969966
while True:
@@ -991,14 +988,8 @@ def unbind(self, addr: str | bytes):
991988
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
992989
encoded to utf-8 first.
993990
"""
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)
1000992
_check_closed(self)
1001-
1002993
rc: C.int = zmq_unbind(self.handle, c_addr)
1003994
if rc != 0:
1004995
raise ZMQError()
@@ -1018,13 +1009,7 @@ def disconnect(self, addr: str | bytes):
10181009
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
10191010
encoded to utf-8 first.
10201011
"""
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)
10281013
_check_closed(self)
10291014

10301015
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):
10521037
default: zmq.EVENT_ALL
10531038
The zmq event bitmask for which events will be sent to the monitor.
10541039
"""
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)
10721044

10731045
_check_rc(zmq_socket_monitor(self.handle, c_addr, events))
10741046

0 commit comments

Comments
 (0)