Skip to content

Commit c070362

Browse files
committed
record by name which ports are chosen at random
1 parent ee9bd1e commit c070362

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

jupyter_client/connect.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ def _ip_changed(self, name, old, new):
333333
control_port = Integer(0, config=True,
334334
help="set the control (ROUTER) port [default: random]")
335335

336+
# names of the ports with random assignment
337+
_random_port_names = None
338+
336339
@property
337340
def ports(self):
338341
return [ getattr(self, name) for name in port_names ]
@@ -417,6 +420,22 @@ def cleanup_ipc_files(self):
417420
except (IOError, OSError):
418421
pass
419422

423+
def _record_random_port_names(self):
424+
"""Records which of the ports are randomly assigned.
425+
426+
Records on first invocation. Does nothing on later invocations."""
427+
428+
if self.transport != 'tcp':
429+
return
430+
if self._random_port_names is not None:
431+
return
432+
433+
self._random_port_names = []
434+
for name in port_names:
435+
if getattr(self, name) <= 0:
436+
self._random_port_names.append(name)
437+
438+
420439
def write_connection_file(self):
421440
"""Write connection info to JSON dict in self.connection_file."""
422441
if self._connection_file_written and os.path.exists(self.connection_file):
@@ -431,6 +450,7 @@ def write_connection_file(self):
431450
kernel_name=self.kernel_name
432451
)
433452
# write_connection_file also sets default ports:
453+
self._record_random_port_names()
434454
for name in port_names:
435455
setattr(self, name, cfg[name])
436456

@@ -467,6 +487,7 @@ def load_connection_info(self, info):
467487
self.transport = info.get('transport', self.transport)
468488
self.ip = info.get('ip', self._ip_default())
469489

490+
self._record_random_port_names()
470491
for name in port_names:
471492
if getattr(self, name) == 0 and name in info:
472493
# not overridden by config or cl_args

jupyter_client/tests/test_connect.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def initialize(self, argv=[]):
2121
JupyterApp.initialize(self, argv=argv)
2222
self.init_connection_file()
2323

24+
class DummyConfigurable(connect.ConnectionFileMixin):
25+
def initialize(self):
26+
pass
27+
2428
sample_info = dict(ip='1.2.3.4', transport='ipc',
2529
shell_port=1, hb_port=2, iopub_port=3, stdin_port=4, control_port=5,
2630
key=b'abc123', signature_scheme='hmac-md5', kernel_name='python'
@@ -31,6 +35,7 @@ def initialize(self, argv=[]):
3135
key=b'abc123', signature_scheme='hmac-md5', kernel_name='test'
3236
)
3337

38+
3439
def test_write_connection_file():
3540
with TemporaryDirectory() as d:
3641
cf = os.path.join(d, 'kernel.json')
@@ -172,3 +177,11 @@ def test_find_connection_file_abspath():
172177
f.write('{}')
173178
assert connect.find_connection_file(abs_cf, path=jupyter_runtime_dir()) == abs_cf
174179

180+
181+
def test_mixin_record_random_ports():
182+
with TemporaryDirectory() as d:
183+
dc = DummyConfigurable(data_dir=d, kernel_name='via-tcp', transport='tcp')
184+
dc.write_connection_file()
185+
assert dc._connection_file_written
186+
assert os.path.exists(dc.connection_file)
187+
assert dc._random_port_names == connect.port_names

0 commit comments

Comments
 (0)