Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kernel.get_parent to match set_parent #661

Merged
merged 1 commit into from
May 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ def _default_ident(self):

# track associations with current request
_allow_stdin = Bool(False)
_parent_headers = Dict({"shell": {}, "control": {}})
_parents = Dict({"shell": {}, "control": {}})
_parent_ident = Dict({'shell': b'', 'control': b''})

@property
def _parent_header(self):
warnings.warn(
"Kernel._parent_header is deprecated in ipykernel 6. Use .get_parent_header()",
"Kernel._parent_header is deprecated in ipykernel 6. Use .get_parent()",
DeprecationWarning,
stacklevel=2,
)
return self.get_parent_header(channel="shell")
return self.get_parent(channel="shell")

# Time to sleep after flushing the stdout/err buffers in each execute
# cycle. While this introduces a hard limit on the minimal latency of the
Expand Down Expand Up @@ -218,23 +218,6 @@ def __init__(self, **kwargs):

self.control_queue = Queue()

def get_parent_header(self, channel="shell"):
"""Get the parent header associated with a channel.

.. versionadded:: 6

Parameters
----------
channel : str
the name of the channel ('shell' or 'control')

Returns
-------
header : dict
the parent header for the most recent request on the channel.
"""
return self._parent_headers.get(channel, {})

def dispatch_control(self, msg):
self.control_queue.put_nowait(msg)

Expand Down Expand Up @@ -546,7 +529,7 @@ def _publish_status(self, status, channel, parent=None):
self.iopub_socket,
"status",
{"execution_state": status},
parent=parent or self.get_parent_header(channel),
parent=parent or self.get_parent(channel),
ident=self._topic("status"),
)

Expand All @@ -555,12 +538,12 @@ def _publish_debug_event(self, event):
self.iopub_socket,
"debug_event",
event,
parent=self.get_parent_header("control"),
parent=self.get_parent("control"),
ident=self._topic("debug_event"),
)

def set_parent(self, ident, parent, channel='shell'):
"""Set the current parent_header
"""Set the current parent request

Side effects (IOPub messages) and replies are associated with
the request that caused them via the parent_header.
Expand All @@ -569,7 +552,24 @@ def set_parent(self, ident, parent, channel='shell'):
on the stdin channel.
"""
self._parent_ident[channel] = ident
self._parent_headers[channel] = parent
self._parents[channel] = parent

def get_parent(self, channel="shell"):
"""Get the parent request associated with a channel.

.. versionadded:: 6

Parameters
----------
channel : str
the name of the channel ('shell' or 'control')

Returns
-------
message : dict
the parent message for the most recent request on the channel.
"""
return self._parents.get(channel, {})

def send_response(self, stream, msg_or_type, content=None, ident=None,
buffers=None, track=False, header=None, metadata=None, channel='shell'):
Expand All @@ -585,7 +585,7 @@ def send_response(self, stream, msg_or_type, content=None, ident=None,
stream,
msg_or_type,
content,
self.get_parent_header(channel),
self.get_parent(channel),
ident,
buffers,
track,
Expand Down Expand Up @@ -957,7 +957,7 @@ def getpass(self, prompt='', stream=None):
return self._input_request(
prompt,
self._parent_ident["shell"],
self.get_parent_header("shell"),
self.get_parent("shell"),
password=True,
)

Expand All @@ -975,7 +975,7 @@ def raw_input(self, prompt=''):
return self._input_request(
str(prompt),
self._parent_ident["shell"],
self.get_parent_header("shell"),
self.get_parent("shell"),
SylvainCorlay marked this conversation as resolved.
Show resolved Hide resolved
password=False,
)

Expand Down