Skip to content

Commit 1d89505

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 1d89505

File tree

2 files changed

+71
-26
lines changed

2 files changed

+71
-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: 70 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,23 @@ 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
228+
the name of the channel ('shell' or 'control')
229+
230+
Returns
231+
-------
232+
header : dict
233+
the parent header for the most recent request on the channel.
234+
"""
235+
return self._parent_headers.get(channel, {})
236+
210237
def dispatch_control(self, msg):
211238
self.control_queue.put_nowait(msg)
212239

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

485512
def _publish_status(self, status, channel, parent=None):
486513
"""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-
)
514+
self.session.send(
515+
self.iopub_socket,
516+
"status",
517+
{"execution_state": status},
518+
parent=parent or self.get_parent_header(channel),
519+
ident=self._topic("status"),
520+
)
521+
493522
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')
523+
self.session.send(
524+
self.iopub_socket,
525+
"debug_event",
526+
event,
527+
parent=self.get_parent_header("control"),
528+
ident=self._topic("debug_event"),
499529
)
500530

501531
def set_parent(self, ident, parent, channel='shell'):
@@ -508,7 +538,7 @@ def set_parent(self, ident, parent, channel='shell'):
508538
on the stdin channel.
509539
"""
510540
self._parent_ident[channel] = ident
511-
self._parent_header[channel] = parent
541+
self._parent_headers[channel] = parent
512542

513543
def send_response(self, stream, msg_or_type, content=None, ident=None,
514544
buffers=None, track=False, header=None, metadata=None, channel='shell'):
@@ -520,8 +550,17 @@ def send_response(self, stream, msg_or_type, content=None, ident=None,
520550
This relies on :meth:`set_parent` having been called for the current
521551
message.
522552
"""
523-
return self.session.send(stream, msg_or_type, content, self._parent_header[channel],
524-
ident, buffers, track, header, metadata)
553+
return self.session.send(
554+
stream,
555+
msg_or_type,
556+
content,
557+
self.get_parent_header(channel),
558+
ident,
559+
buffers,
560+
track,
561+
header,
562+
metadata,
563+
)
525564

526565
def init_metadata(self, parent):
527566
"""Initialize metadata.
@@ -630,7 +669,7 @@ async def inspect_request(self, stream, ident, parent):
630669
content.get('detail_level', 0),
631670
)
632671
if inspect.isawaitable(reply_content):
633-
reply_content = await reply_content
672+
reply_content = await reply_content
634673

635674
# Before we send this object over, we scrub it for JSON usage
636675
reply_content = json_clean(reply_content)
@@ -878,11 +917,16 @@ def getpass(self, prompt='', stream=None):
878917
)
879918
if stream is not None:
880919
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'],
920+
921+
warnings.warn(
922+
"The `stream` parameter of `getpass.getpass` will have no effect when using ipykernel",
923+
UserWarning,
924+
stacklevel=2,
925+
)
926+
return self._input_request(
927+
prompt,
928+
self._parent_ident["shell"],
929+
self.get_parent_header("shell"),
886930
password=True,
887931
)
888932

@@ -897,9 +941,10 @@ def raw_input(self, prompt=''):
897941
raise StdinNotImplementedError(
898942
"raw_input was called, but this frontend does not support input requests."
899943
)
900-
return self._input_request(str(prompt),
901-
self._parent_ident['shell'],
902-
self._parent_header['shell'],
944+
return self._input_request(
945+
str(prompt),
946+
self._parent_ident["shell"],
947+
self.get_parent_header("shell"),
903948
password=False,
904949
)
905950

@@ -944,7 +989,7 @@ def _input_request(self, prompt, ident, parent, password=False):
944989
raise KeyboardInterrupt("Interrupted by user") from None
945990
except Exception as e:
946991
self.log.warning("Invalid Message:", exc_info=True)
947-
992+
948993
try:
949994
value = reply["content"]["value"]
950995
except Exception:

0 commit comments

Comments
 (0)