Skip to content

Commit c6ce7ec

Browse files
pyzmq 25 compatibility
Handle a new keyword argument to `Context.socket` in our subclass.
1 parent 29a40a0 commit c6ce7ec

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

labscript_utils/ls_zprocess.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import zprocess
2020
import zprocess.process_tree
21-
from zprocess.security import SecureContext
21+
from zprocess.security import SecureContext, SecureSocket
2222
from labscript_utils.labconfig import LabConfig
2323
from labscript_utils import dedent
2424
import zprocess.zlog
@@ -248,10 +248,19 @@ def instance(cls):
248248
# Super required to call unbound class method of parent class:
249249
return super(Context, cls).instance(shared_secret=config['shared_secret'])
250250

251-
def socket(self, *args, **kwargs):
252-
config = get_config()
253-
kwargs['allow_insecure'] = config['allow_insecure']
254-
return SecureContext.socket(self, *args, **kwargs)
251+
def socket(self, socket_type, socket_class=None, **kwargs):
252+
# socket_class kwarg introduced in pyzmq 25. Pass it through if it was given,
253+
# otherwise don't.
254+
if socket_class is not None:
255+
kwargs['socket_class'] = socket_class
256+
# Only insert our security-related args to the socket if we know it's going to
257+
# be a SecureSocket. If caller has explicitly requested a different socket type
258+
# (e.g since pyzmq 25, ThreadAuthenticator sets up an internal socket by calling
259+
# `Context.socket(..., socket_class=zmq.Socket)), then don't.`
260+
if socket_class is None or issubclass(socket_class, SecureContext):
261+
config = get_config()
262+
kwargs['allow_insecure'] = config['allow_insecure']
263+
return SecureContext.socket(self, socket_type=socket_type, **kwargs)
255264

256265

257266
def Lock(*args, **kwargs):

0 commit comments

Comments
 (0)