Skip to content

Commit 121b376

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

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

zmq/backend/cython/_zmq.py

Lines changed: 36 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,10 @@ 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+
try:
961+
c_addr: p_char = addr
962+
except TypeError:
963+
raise TypeError(f"Expected addr to be str, got {addr!r}") from None
955964

956965
_check_closed(self)
957966

@@ -965,7 +974,7 @@ def connect(self, addr: str) -> None:
965974
else:
966975
break
967976

968-
def unbind(self, addr):
977+
def unbind(self, addr: str | bytes):
969978
"""
970979
Unbind from an address (undoes a call to bind).
971980
@@ -980,21 +989,19 @@ def unbind(self, addr):
980989
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
981990
encoded to utf-8 first.
982991
"""
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
985997

986998
_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
992999

993-
rc = zmq_unbind(self.handle, c_addr)
1000+
rc: C.int = zmq_unbind(self.handle, c_addr)
9941001
if rc != 0:
9951002
raise ZMQError()
9961003

997-
def disconnect(self, addr):
1004+
def disconnect(self, addr: str | bytes):
9981005
"""
9991006
Disconnect from a remote 0MQ socket (undoes a call to connect).
10001007
@@ -1009,21 +1016,20 @@ def disconnect(self, addr):
10091016
tcp, udp, pgm, inproc and ipc. If the address is unicode, it is
10101017
encoded to utf-8 first.
10111018
"""
1012-
rc: C.int
1013-
c_addr: p_char
1014-
1015-
_check_closed(self)
10161019
if isinstance(addr, str):
10171020
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)
10211027

1022-
rc = zmq_disconnect(self.handle, c_addr)
1028+
rc: C.int = zmq_disconnect(self.handle, c_addr)
10231029
if rc != 0:
10241030
raise ZMQError()
10251031

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):
10271033
"""
10281034
Start publishing socket events on inproc.
10291035
See libzmq docs for zmq_monitor for details.
@@ -1036,7 +1042,7 @@ def monitor(self, addr, events: C.int = ZMQ_EVENT_ALL):
10361042
10371043
Parameters
10381044
----------
1039-
addr : str
1045+
addr : str | None
10401046
The inproc url used for monitoring. Passing None as
10411047
the addr will cause an existing socket monitor to be
10421048
deregistered.

0 commit comments

Comments
 (0)