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

Implemented inspectVariables request #624

Merged
merged 1 commit into from
Mar 30, 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
64 changes: 52 additions & 12 deletions ipykernel/debugger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import re

import zmq
from zmq.utils import jsonapi
Expand All @@ -9,6 +10,7 @@

from .compiler import (get_file_name, get_tmp_directory, get_tmp_hash_seed)

from IPython.core.getipython import get_ipython
import debugpy

class DebugpyMessageQueue:
Expand Down Expand Up @@ -326,17 +328,39 @@ async def stackTrace(self, message):
reply['body']['stackFrames'] = reply['body']['stackFrames'][:module_idx+1]
return reply

def accept_variable(self, variable):
cond = variable['type'] != 'list' and variable['type'] != 'ZMQExitAutocall' and variable['type'] != 'dict'
cond = cond and variable['name'] not in ['debugpy', 'get_ipython', '_']
cond = cond and variable['name'][0:2] != '_i'
def accept_variable(self, variable_name):
forbid_list = [
'__name__',
'__doc__',
'__package__',
'__loader__',
'__spec__',
'__annotations__',
'__builtins__',
'__builtin__',
'__display__',
'get_ipython',
'debugpy',
'exit',
'quit',
'In',
'Out',
'_oh',
'_dh',
'_',
'__',
'___'
]
cond = variable_name not in forbid_list
cond = cond and not bool(re.search(r'^_\d', variable_name))
cond = cond and variable_name[0:2] != '_i'
return cond

async def variables(self, message):
reply = await self._forward_message(message)
# TODO : check start and count arguments work as expected in debugpy
reply['body']['variables'] = \
[var for var in reply['body']['variables'] if self.accept_variable(var)]
[var for var in reply['body']['variables'] if self.accept_variable(var['name'])]
return reply

async def attach(self, message):
Expand Down Expand Up @@ -383,8 +407,24 @@ async def debugInfo(self, message):
return reply

async def inspectVariables(self, message):
# TODO
return {}
var_list = []
for k, v in get_ipython().user_ns.items():
if self.accept_variable(k):
var_list.append({
'name': k,
'value': v,
'variablesReference': 0
})
reply = {
'type': 'response',
'request_seq': message['seq'],
'success': True,
'command': message['command'],
'body': {
'variables': var_list
}
}
return reply

async def process_request(self, message):
reply = {}
Expand All @@ -398,11 +438,11 @@ async def process_request(self, message):
self.log.info('The debugger has started')
else:
reply = {
'command', 'initialize',
'request_seq', message['seq'],
'seq', 3,
'success', False,
'type', 'response'
'command': 'initialize',
'request_seq': message['seq'],
'seq': 3,
'success': False,
'type': 'response'
}

handler = self.static_debug_handlers.get(message['command'], None)
Expand Down