Skip to content

Commit dc35b1e

Browse files
authored
Merge pull request #624 from JohanMabille/inspect
Implemented inspectVariables request
2 parents 4754cdc + 2946dcb commit dc35b1e

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

ipykernel/debugger.py

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import os
3+
import re
34

45
import zmq
56
from zmq.utils import jsonapi
@@ -9,6 +10,7 @@
910

1011
from .compiler import (get_file_name, get_tmp_directory, get_tmp_hash_seed)
1112

13+
from IPython.core.getipython import get_ipython
1214
import debugpy
1315

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

329-
def accept_variable(self, variable):
330-
cond = variable['type'] != 'list' and variable['type'] != 'ZMQExitAutocall' and variable['type'] != 'dict'
331-
cond = cond and variable['name'] not in ['debugpy', 'get_ipython', '_']
332-
cond = cond and variable['name'][0:2] != '_i'
331+
def accept_variable(self, variable_name):
332+
forbid_list = [
333+
'__name__',
334+
'__doc__',
335+
'__package__',
336+
'__loader__',
337+
'__spec__',
338+
'__annotations__',
339+
'__builtins__',
340+
'__builtin__',
341+
'__display__',
342+
'get_ipython',
343+
'debugpy',
344+
'exit',
345+
'quit',
346+
'In',
347+
'Out',
348+
'_oh',
349+
'_dh',
350+
'_',
351+
'__',
352+
'___'
353+
]
354+
cond = variable_name not in forbid_list
355+
cond = cond and not bool(re.search(r'^_\d', variable_name))
356+
cond = cond and variable_name[0:2] != '_i'
333357
return cond
334358

335359
async def variables(self, message):
336360
reply = await self._forward_message(message)
337361
# TODO : check start and count arguments work as expected in debugpy
338362
reply['body']['variables'] = \
339-
[var for var in reply['body']['variables'] if self.accept_variable(var)]
363+
[var for var in reply['body']['variables'] if self.accept_variable(var['name'])]
340364
return reply
341365

342366
async def attach(self, message):
@@ -383,8 +407,24 @@ async def debugInfo(self, message):
383407
return reply
384408

385409
async def inspectVariables(self, message):
386-
# TODO
387-
return {}
410+
var_list = []
411+
for k, v in get_ipython().user_ns.items():
412+
if self.accept_variable(k):
413+
var_list.append({
414+
'name': k,
415+
'value': v,
416+
'variablesReference': 0
417+
})
418+
reply = {
419+
'type': 'response',
420+
'request_seq': message['seq'],
421+
'success': True,
422+
'command': message['command'],
423+
'body': {
424+
'variables': var_list
425+
}
426+
}
427+
return reply
388428

389429
async def process_request(self, message):
390430
reply = {}
@@ -398,11 +438,11 @@ async def process_request(self, message):
398438
self.log.info('The debugger has started')
399439
else:
400440
reply = {
401-
'command', 'initialize',
402-
'request_seq', message['seq'],
403-
'seq', 3,
404-
'success', False,
405-
'type', 'response'
441+
'command': 'initialize',
442+
'request_seq': message['seq'],
443+
'seq': 3,
444+
'success': False,
445+
'type': 'response'
406446
}
407447

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

0 commit comments

Comments
 (0)