From 3767ce0e131a6a313f5d4fb8ee9e5837862430d8 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Mon, 1 Aug 2022 15:29:33 +0200 Subject: [PATCH 1/3] Move comm package --- include/xeus-python/xinterpreter.hpp | 1 + src/xinterpreter.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/xeus-python/xinterpreter.hpp b/include/xeus-python/xinterpreter.hpp index 1382a024..77440d51 100644 --- a/include/xeus-python/xinterpreter.hpp +++ b/include/xeus-python/xinterpreter.hpp @@ -79,6 +79,7 @@ namespace xpyt py::object m_displayhook; py::object m_logger; py::object m_terminal_stream; + py::object m_comm_manager; // The interpreter has the same scope as a `gil_scoped_release` instance // so that the GIL is not held by default, it will only be held when the diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index a5243078..b2cc371d 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -65,6 +65,7 @@ namespace xpyt py::gil_scoped_acquire acquire; py::module sys = py::module::import("sys"); + py::module comm = py::module::import("comm"); py::module logging = py::module::import("logging"); py::module display_module = get_display_module(); @@ -73,8 +74,12 @@ namespace xpyt py::module comm_module = get_comm_module(); py::module kernel_module = get_kernel_module(); - // Monkey patching "from ipykernel.comm import Comm" - sys.attr("modules")["ipykernel.comm"] = comm_module; + // TODO Handle old ipywidgets by monkey patching ipykernel too... + comm.attr("register_comm_classes")( + comm_module.attr("Comm"), + comm_module.attr("CommManager") + ); + m_comm_manager = comm.attr("create_comm_manager")(); instanciate_ipython_shell(); From 57b76c8dd31b1eb54f71142b3027742df6c196ca Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 20 Oct 2022 11:24:06 +0200 Subject: [PATCH 2/3] Update the comm module --- src/xcomm.cpp | 10 ++++++++++ src/xinterpreter.cpp | 11 ++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/xcomm.cpp b/src/xcomm.cpp index 6f74b2bd..5ec5e392 100644 --- a/src/xcomm.cpp +++ b/src/xcomm.cpp @@ -144,6 +144,16 @@ namespace xpyt .def(py::init<>()) .def("register_target", &xcomm_manager::register_target); + comm_module.def("create_comm", [&comm_module](py::args objs, py::kwargs kw) { + return comm_module.attr("Comm")(*objs, **kw); + }); + + comm_module.def("get_comm_manager", [&comm_module]() { + static py::object comm_manager = comm_module.attr("CommManager")(); + + return comm_manager; + }); + return comm_module; } diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index b2cc371d..45a750a5 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -65,7 +65,6 @@ namespace xpyt py::gil_scoped_acquire acquire; py::module sys = py::module::import("sys"); - py::module comm = py::module::import("comm"); py::module logging = py::module::import("logging"); py::module display_module = get_display_module(); @@ -74,12 +73,10 @@ namespace xpyt py::module comm_module = get_comm_module(); py::module kernel_module = get_kernel_module(); - // TODO Handle old ipywidgets by monkey patching ipykernel too... - comm.attr("register_comm_classes")( - comm_module.attr("Comm"), - comm_module.attr("CommManager") - ); - m_comm_manager = comm.attr("create_comm_manager")(); + // Old approach: ipykernel provides the comm + sys.attr("modules")["ipykernel.comm"] = comm_module; + // New approach: we provide our comm module + sys.attr("modules")["comm"] = comm_module; instanciate_ipython_shell(); From 2732a920cf0039f51a3f3073e043452f0fa63a7d Mon Sep 17 00:00:00 2001 From: martinRenou Date: Thu, 20 Oct 2022 11:26:50 +0200 Subject: [PATCH 3/3] Remove leftover --- include/xeus-python/xinterpreter.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/xeus-python/xinterpreter.hpp b/include/xeus-python/xinterpreter.hpp index 77440d51..1382a024 100644 --- a/include/xeus-python/xinterpreter.hpp +++ b/include/xeus-python/xinterpreter.hpp @@ -79,7 +79,6 @@ namespace xpyt py::object m_displayhook; py::object m_logger; py::object m_terminal_stream; - py::object m_comm_manager; // The interpreter has the same scope as a `gil_scoped_release` instance // so that the GIL is not held by default, it will only be held when the