Skip to content

Commit 2cd0baf

Browse files
committed
Add Modules Request
1 parent 1422755 commit 2cd0baf

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/xdebugger.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,43 @@ namespace xpyt
369369
return std::unique_ptr<xeus::xdebugger>(new debugger(context,
370370
config, user_name, session_id, debugger_config));
371371
}
372+
373+
nl::json debugger::modules(const nl::json& message)
374+
{
375+
py::module sys = py::module::import("sys");
376+
py::list modules = sys.attr("modules").attr("values")();
377+
378+
int start_module = message.value("startModule", 0);
379+
int module_count = message.value("moduleCount", static_cast<int>(py::len(modules)));
380+
381+
nl::json mods = nl::json::array();
382+
for (int i = start_module; i < module_count && i < static_cast<int>(py::len(modules)); ++i)
383+
{
384+
py::object module = modules[i];
385+
py::object spec = getattr(module, "__spec__", py::none());
386+
py::object origin = py::none();
387+
if (!spec.is_none())
388+
origin = getattr(spec, "origin", py::none());
389+
390+
if (!origin.is_none())
391+
{
392+
std::string filename = py::str(origin);
393+
if (filename.size() > 3 && filename.substr(filename.size() - 3) == ".py")
394+
{
395+
mods.push_back({
396+
{"id", i},
397+
{"name", py::str(module.attr("__name__"))},
398+
{"path", filename}
399+
});
400+
}
401+
}
402+
}
403+
404+
return {
405+
{"body", {
406+
{"modules", mods},
407+
{"totalModules", py::len(modules)}
408+
}}
409+
};
410+
}
372411
}

0 commit comments

Comments
 (0)