Skip to content

Commit a18f007

Browse files
Let get_parent decide the channel to get parent header (ipython#1128)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 99b2ec4 commit a18f007

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

ipykernel/comm/comm.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6-
import threading
76
import uuid
87
from typing import Optional
98
from warnings import warn
@@ -12,7 +11,6 @@
1211
import traitlets.config
1312
from traitlets import Bool, Bytes, Instance, Unicode, default
1413

15-
from ipykernel.control import CONTROL_THREAD_NAME
1614
from ipykernel.jsonutil import json_clean
1715
from ipykernel.kernelbase import Kernel
1816

@@ -32,11 +30,6 @@ def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
3230
metadata = {} if metadata is None else metadata
3331
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
3432

35-
if threading.current_thread().name == CONTROL_THREAD_NAME:
36-
channel_from_which_to_get_parent_header = "control"
37-
else:
38-
channel_from_which_to_get_parent_header = "shell"
39-
4033
if self.kernel is None:
4134
self.kernel = Kernel.instance()
4235

@@ -45,7 +38,7 @@ def publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
4538
msg_type,
4639
content,
4740
metadata=json_clean(metadata),
48-
parent=self.kernel.get_parent(channel_from_which_to_get_parent_header),
41+
parent=self.kernel.get_parent(),
4942
ident=self.topic,
5043
buffers=buffers,
5144
)

ipykernel/ipkernel.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,6 @@ def do_apply(self, content, bufs, msg_id, reply_metadata):
675675
"error",
676676
reply_content,
677677
ident=self._topic("error"),
678-
channel="shell",
679678
)
680679
self.log.info("Exception in apply request:\n%s", "\n".join(reply_content["traceback"]))
681680
result_buf = []

ipykernel/kernelbase.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import os
1212
import socket
1313
import sys
14+
import threading
1415
import time
1516
import typing as t
1617
import uuid
@@ -19,6 +20,8 @@
1920
from functools import partial
2021
from signal import SIGINT, SIGTERM, Signals, default_int_handler, signal
2122

23+
from .control import CONTROL_THREAD_NAME
24+
2225
if sys.platform != "win32":
2326
from signal import SIGKILL
2427
else:
@@ -187,7 +190,7 @@ def _parent_header(self):
187190
DeprecationWarning,
188191
stacklevel=2,
189192
)
190-
return self.get_parent(channel="shell")
193+
return self.get_parent()
191194

192195
# Time to sleep after flushing the stdout/err buffers in each execute
193196
# cycle. While this introduces a hard limit on the minimal latency of the
@@ -598,7 +601,7 @@ def _publish_debug_event(self, event):
598601
self.iopub_socket,
599602
"debug_event",
600603
event,
601-
parent=self.get_parent("control"),
604+
parent=self.get_parent(),
602605
ident=self._topic("debug_event"),
603606
)
604607

@@ -614,7 +617,7 @@ def set_parent(self, ident, parent, channel="shell"):
614617
self._parent_ident[channel] = ident
615618
self._parents[channel] = parent
616619

617-
def get_parent(self, channel="shell"):
620+
def get_parent(self, channel=None):
618621
"""Get the parent request associated with a channel.
619622
620623
.. versionadded:: 6
@@ -629,6 +632,14 @@ def get_parent(self, channel="shell"):
629632
message : dict
630633
the parent message for the most recent request on the channel.
631634
"""
635+
636+
if channel is None:
637+
# If a channel is not specified, get information from current thread
638+
if threading.current_thread().name == CONTROL_THREAD_NAME:
639+
channel = "control"
640+
else:
641+
channel = "shell"
642+
632643
return self._parents.get(channel, {})
633644

634645
def send_response(
@@ -641,7 +652,7 @@ def send_response(
641652
track=False,
642653
header=None,
643654
metadata=None,
644-
channel="shell",
655+
channel=None,
645656
):
646657
"""Send a response to the message we're currently processing.
647658

0 commit comments

Comments
 (0)