Skip to content

Commit 17a8b61

Browse files
committed
add Kernel.get_parent_header
- deprecate Kernel._parent_header - move new multi-parent header dict to new Kernel._parent_headers instead of changing what Kernel._parent_header means
1 parent 317159f commit 17a8b61

File tree

2 files changed

+70
-26
lines changed

2 files changed

+70
-26
lines changed

ipykernel/comm/comm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys)
6666
self.kernel.session.send(self.kernel.iopub_socket, msg_type,
6767
content,
6868
metadata=json_clean(metadata),
69-
parent=self.kernel._parent_header.get('shell', {}),
69+
parent=self.kernel.get_parent_header("shell"),
7070
ident=self.topic,
7171
buffers=buffers,
7272
)

ipykernel/kernelbase.py

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,18 @@ def _default_ident(self):
132132

133133
# track associations with current request
134134
_allow_stdin = Bool(False)
135-
_parent_header = Dict({'shell': {}, 'control': {}})
135+
_parent_headers = Dict({"shell": {}, "control": {}})
136136
_parent_ident = Dict({'shell': b'', 'control': b''})
137+
138+
@property
139+
def _parent_header(self):
140+
warnings.warn(
141+
"Kernel._parent_header is deprecated in ipykernel 6. Use .get_parent_header()",
142+
DeprecationWarning,
143+
stacklevel=2,
144+
)
145+
return self.get_parent_header(channel="shell")
146+
137147
# Time to sleep after flushing the stdout/err buffers in each execute
138148
# cycle. While this introduces a hard limit on the minimal latency of the
139149
# execute cycle, it helps prevent output synchronization problems for
@@ -207,6 +217,22 @@ def __init__(self, **kwargs):
207217

208218
self.control_queue = Queue()
209219

220+
def get_parent_header(self, channel="shell"):
221+
"""Get the parent header associated with a channel.
222+
223+
.. versionadded:: 6
224+
225+
Parameters
226+
----------
227+
channel (str): the name of the channel ('shell' or 'control')
228+
229+
Returns
230+
-------
231+
header (dict): the parent header for the most recent request
232+
on the channel.
233+
"""
234+
return self._parent_headers.get(channel, {})
235+
210236
def dispatch_control(self, msg):
211237
self.control_queue.put_nowait(msg)
212238

@@ -484,18 +510,21 @@ def _publish_execute_input(self, code, parent, execution_count):
484510

485511
def _publish_status(self, status, channel, parent=None):
486512
"""send status (busy/idle) on IOPub"""
487-
self.session.send(self.iopub_socket,
488-
'status',
489-
{'execution_state': status},
490-
parent=parent or self._parent_header[channel],
491-
ident=self._topic('status'),
492-
)
513+
self.session.send(
514+
self.iopub_socket,
515+
"status",
516+
{"execution_state": status},
517+
parent=parent or self.get_parent_header(channel),
518+
ident=self._topic("status"),
519+
)
520+
493521
def _publish_debug_event(self, event):
494-
self.session.send(self.iopub_socket,
495-
'debug_event',
496-
event,
497-
parent=self._parent_header['control'],
498-
ident=self._topic('debug_event')
522+
self.session.send(
523+
self.iopub_socket,
524+
"debug_event",
525+
event,
526+
parent=self.get_parent_header("control"),
527+
ident=self._topic("debug_event"),
499528
)
500529

501530
def set_parent(self, ident, parent, channel='shell'):
@@ -508,7 +537,7 @@ def set_parent(self, ident, parent, channel='shell'):
508537
on the stdin channel.
509538
"""
510539
self._parent_ident[channel] = ident
511-
self._parent_header[channel] = parent
540+
self._parent_headers[channel] = parent
512541

513542
def send_response(self, stream, msg_or_type, content=None, ident=None,
514543
buffers=None, track=False, header=None, metadata=None, channel='shell'):
@@ -520,8 +549,17 @@ def send_response(self, stream, msg_or_type, content=None, ident=None,
520549
This relies on :meth:`set_parent` having been called for the current
521550
message.
522551
"""
523-
return self.session.send(stream, msg_or_type, content, self._parent_header[channel],
524-
ident, buffers, track, header, metadata)
552+
return self.session.send(
553+
stream,
554+
msg_or_type,
555+
content,
556+
self.get_parent_header(channel),
557+
ident,
558+
buffers,
559+
track,
560+
header,
561+
metadata,
562+
)
525563

526564
def init_metadata(self, parent):
527565
"""Initialize metadata.
@@ -630,7 +668,7 @@ async def inspect_request(self, stream, ident, parent):
630668
content.get('detail_level', 0),
631669
)
632670
if inspect.isawaitable(reply_content):
633-
reply_content = await reply_content
671+
reply_content = await reply_content
634672

635673
# Before we send this object over, we scrub it for JSON usage
636674
reply_content = json_clean(reply_content)
@@ -878,11 +916,16 @@ def getpass(self, prompt='', stream=None):
878916
)
879917
if stream is not None:
880918
import warnings
881-
warnings.warn("The `stream` parameter of `getpass.getpass` will have no effect when using ipykernel",
882-
UserWarning, stacklevel=2)
883-
return self._input_request(prompt,
884-
self._parent_ident['shell'],
885-
self._parent_header['shell'],
919+
920+
warnings.warn(
921+
"The `stream` parameter of `getpass.getpass` will have no effect when using ipykernel",
922+
UserWarning,
923+
stacklevel=2,
924+
)
925+
return self._input_request(
926+
prompt,
927+
self._parent_ident["shell"],
928+
self.get_parent_header("shell"),
886929
password=True,
887930
)
888931

@@ -897,9 +940,10 @@ def raw_input(self, prompt=''):
897940
raise StdinNotImplementedError(
898941
"raw_input was called, but this frontend does not support input requests."
899942
)
900-
return self._input_request(str(prompt),
901-
self._parent_ident['shell'],
902-
self._parent_header['shell'],
943+
return self._input_request(
944+
str(prompt),
945+
self._parent_ident["shell"],
946+
self.get_parent_header("shell"),
903947
password=False,
904948
)
905949

@@ -944,7 +988,7 @@ def _input_request(self, prompt, ident, parent, password=False):
944988
raise KeyboardInterrupt("Interrupted by user") from None
945989
except Exception as e:
946990
self.log.warning("Invalid Message:", exc_info=True)
947-
991+
948992
try:
949993
value = reply["content"]["value"]
950994
except Exception:

0 commit comments

Comments
 (0)