Skip to content

Commit

Permalink
Move Python-specific bits to ipkernel
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Mar 22, 2021
1 parent ae00576 commit d011b03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
24 changes: 23 additions & 1 deletion ipykernel/ipkernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
from IPython.utils.tokenutil import token_at_cursor, line_at_cursor
from tornado import gen
from traitlets import Instance, Type, Any, List, Bool, observe, observe_compat
from zmq.eventloop.zmqstream import ZMQStream

from .comm import CommManager
from .kernelbase import Kernel as KernelBase
from .zmqshell import ZMQInteractiveShell
from .eventloops import _use_appnope

from .debugger import Debugger
from .compiler import XCachingCompiler

try:
Expand All @@ -44,6 +45,8 @@ class IPythonKernel(KernelBase):
help="Set this flag to False to deactivate the use of experimental IPython completion APIs.",
).tag(config=True)

debugpy_stream = Instance(ZMQStream, allow_none=True)

user_module = Any()
@observe('user_module')
@observe_compat
Expand All @@ -67,6 +70,13 @@ def _user_ns_changed(self, change):
def __init__(self, **kwargs):
super(IPythonKernel, self).__init__(**kwargs)

# Initialize the Debugger
self.debugger = Debugger(self.log,
self.debugpy_stream,
self._publish_debug_event,
self.debug_shell_socket,
self.session)

# Initialize the InteractiveShell subclass
self.shell = self.shell_class.instance(parent=self,
profile_dir = self.profile_dir,
Expand Down Expand Up @@ -140,12 +150,20 @@ def __init__(self, **kwargs):
'file_extension': '.py'
}

@gen.coroutine
def dispatch_debugpy(self, msg):
# The first frame is the socket id, we can drop it
frame = msg[1].bytes.decode('utf-8')
self.log.debug("Debugpy received: %s", frame)
self.debugger.tcp_client.receive_dap_frame(frame)

@property
def banner(self):
return self.shell.banner

def start(self):
self.shell.exit_now = False
self.debugpy_stream.on_recv(self.dispatch_debugpy, copy=False)
super(IPythonKernel, self).start()

def set_parent(self, ident, parent, channel='shell'):
Expand Down Expand Up @@ -381,6 +399,10 @@ def do_complete(self, code, cursor_pos):
'metadata' : {},
'status' : 'ok'}

@gen.coroutine
def do_debug_request(self, msg):
return (yield self.debugger.process_request(msg))

def _experimental_do_complete(self, code, cursor_pos):
"""
Experimental completions from IPython, using Jedi.
Expand Down
19 changes: 1 addition & 18 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import time
import uuid
import warnings
import asyncio

try:
# jupyter_client >= 5, use tz-aware now
Expand Down Expand Up @@ -40,7 +39,6 @@

from ._version import kernel_protocol_version

from .debugger import Debugger

class Kernel(SingletonConfigurable):

Expand Down Expand Up @@ -71,7 +69,6 @@ def shell_streams(self):
return [shell_stream]

control_stream = Instance(ZMQStream, allow_none=True)
debugpy_stream = Instance(ZMQStream, allow_none=True)

debug_shell_socket = Any()

Expand Down Expand Up @@ -180,23 +177,10 @@ def __init__(self, **kwargs):
for msg_type in self.control_msg_types:
self.control_handlers[msg_type] = getattr(self, msg_type)

self.debugger = Debugger(self.log,
self.debugpy_stream,
self._publish_debug_event,
self.debug_shell_socket,
self.session)

self.control_queue = Queue()
if 'control_thread' in kwargs:
kwargs['control_thread'].io_loop.add_callback(self.poll_control_queue)

@gen.coroutine
def dispatch_debugpy(self, msg):
# The first frame is the socket id, we can drop it
frame = msg[1].bytes.decode('utf-8')
self.log.debug("Debugpy received: %s", frame)
self.debugger.tcp_client.receive_dap_frame(frame)

@gen.coroutine
def dispatch_control(self, msg):
self.control_queue.put_nowait(msg)
Expand Down Expand Up @@ -440,7 +424,6 @@ def start(self):
self.io_loop.add_callback(self.dispatch_queue)

self.control_stream.on_recv(self.dispatch_control, copy=False)
self.debugpy_stream.on_recv(self.dispatch_debugpy, copy=False)

self.shell_stream.on_recv(
partial(
Expand Down Expand Up @@ -752,7 +735,7 @@ def debug_request(self, stream, ident, parent):

@gen.coroutine
def do_debug_request(self, msg):
return (yield self.debugger.process_request(msg))
raise NotImplementedError

#---------------------------------------------------------------------------
# Engine methods (DEPRECATED)
Expand Down

0 comments on commit d011b03

Please sign in to comment.