Skip to content

Commit ca86fb5

Browse files
committed
keep accepting addr as bytes
otherwise, LAST_ENDPOINT is annoying
1 parent 890959a commit ca86fb5

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

zmq/backend/cython/_zmq.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def get(self, option: C.int):
892892

893893
return result
894894

895-
def bind(self, addr: str):
895+
def bind(self, addr: str | bytes):
896896
"""
897897
Bind the socket to an address.
898898
@@ -908,14 +908,21 @@ def bind(self, addr: str):
908908
tcp, udp, pgm, epgm, inproc and ipc. If the address is unicode, it is
909909
encoded to utf-8 first.
910910
"""
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
914920

915921
_check_closed(self)
916-
rc = zmq_bind(self.handle, c_addr)
922+
rc: C.int = zmq_bind(self.handle, c_addr)
917923
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:
919926
path = addr.split('://', 1)[-1]
920927
msg = (
921928
f'ipc path "{path}" is longer than {IPC_PATH_MAX_LEN} '
@@ -924,7 +931,7 @@ def bind(self, addr: str):
924931
'to check addr length (if it is defined).'
925932
)
926933
raise ZMQError(msg=msg)
927-
elif _zmq_errno() == ENOENT:
934+
elif _errno == ENOENT:
928935
path = addr.split('://', 1)[-1]
929936
msg = f'No such file or directory for ipc path "{path}".'
930937
raise ZMQError(msg=msg)
@@ -937,7 +944,7 @@ def bind(self, addr: str):
937944
else:
938945
break
939946

940-
def connect(self, addr: str) -> None:
947+
def connect(self, addr: str | bytes) -> None:
941948
"""
942949
Connect to a remote 0MQ socket.
943950
@@ -950,8 +957,12 @@ def connect(self, addr: str) -> None:
950957
encoded to utf-8 first.
951958
"""
952959
rc: C.int
953-
b_addr: bytes = addr.encode('utf-8')
954-
c_addr: p_char = b_addr
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
955966

956967
_check_closed(self)
957968

@@ -965,7 +976,7 @@ def connect(self, addr: str) -> None:
965976
else:
966977
break
967978

968-
def unbind(self, addr):
979+
def unbind(self, addr: str | bytes):
969980
"""
970981
Unbind from an address (undoes a call to bind).
971982
@@ -980,21 +991,19 @@ def unbind(self, addr):
980991
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
981992
encoded to utf-8 first.
982993
"""
983-
rc: C.int
984-
c_addr: p_char
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
985999

9861000
_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
9921001

993-
rc = zmq_unbind(self.handle, c_addr)
1002+
rc: C.int = zmq_unbind(self.handle, c_addr)
9941003
if rc != 0:
9951004
raise ZMQError()
9961005

997-
def disconnect(self, addr):
1006+
def disconnect(self, addr: str | bytes):
9981007
"""
9991008
Disconnect from a remote 0MQ socket (undoes a call to connect).
10001009
@@ -1009,21 +1018,20 @@ def disconnect(self, addr):
10091018
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
10101019
encoded to utf-8 first.
10111020
"""
1012-
rc: C.int
1013-
c_addr: p_char
1014-
1015-
_check_closed(self)
10161021
if isinstance(addr, str):
10171022
addr = addr.encode('utf-8')
1018-
if not isinstance(addr, bytes):
1019-
raise TypeError(f'expected str, got: {addr!r}')
1020-
c_addr = addr
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
10211027

1022-
rc = zmq_disconnect(self.handle, c_addr)
1028+
_check_closed(self)
1029+
1030+
rc: C.int = zmq_disconnect(self.handle, c_addr)
10231031
if rc != 0:
10241032
raise ZMQError()
10251033

1026-
def monitor(self, addr, events: C.int = ZMQ_EVENT_ALL):
1034+
def monitor(self, addr: str | bytes | None, events: C.int = ZMQ_EVENT_ALL):
10271035
"""
10281036
Start publishing socket events on inproc.
10291037
See libzmq docs for zmq_monitor for details.
@@ -1036,7 +1044,7 @@ def monitor(self, addr, events: C.int = ZMQ_EVENT_ALL):
10361044
10371045
Parameters
10381046
----------
1039-
addr : str
1047+
addr : str | None
10401048
The inproc url used for monitoring. Passing None as
10411049
the addr will cause an existing socket monitor to be
10421050
deregistered.

0 commit comments

Comments
 (0)