forked from jupyter-xeus/xeus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkernel.cpp
272 lines (226 loc) · 9.32 KB
/
xkernel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <string>
#include <utility>
#include "nlohmann/json.hpp"
#include "xeus/xinterpreter.hpp"
#include "pybind11_json/pybind11_json.hpp"
#include "pybind11/pybind11.h"
#include "pybind11/functional.h"
#include "pybind11/eval.h"
#include "xeus-python/xutils.hpp"
#include "xcomm.hpp"
#include "xkernel.hpp"
#include "xinternal_utils.hpp"
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif
namespace py = pybind11;
namespace nl = nlohmann;
using namespace pybind11::literals;
namespace xpyt_ipython
{
/***********************
* xkernel declaration *
**********************/
struct xkernel
{
xkernel() = default;
py::dict get_parent();
py::object m_comm_manager;
};
/**************************
* xkernel implementation *
**************************/
py::dict xkernel::get_parent()
{
return py::dict(py::arg("header") = xeus::get_interpreter().parent_header().get<py::object>());
}
/*****************
* kernel module *
*****************/
py::module get_kernel_module_impl()
{
py::module kernel_module = xpyt::create_module("kernel");
py::class_<xkernel>(kernel_module, "XKernel")
.def(py::init<>())
.def("get_parent", &xkernel::get_parent)
.def_property_readonly("_parent_header", &xkernel::get_parent)
.def_readwrite("comm_manager", &xkernel::m_comm_manager);
return kernel_module;
}
}
namespace xpyt_raw
{
struct xmock_ipython
{
py::object kernel;
void register_post_execute(py::args, py::kwargs) {};
void enable_gui(py::args, py::kwargs) {};
void observe(py::args, py::kwargs) {};
void showtraceback(py::args, py::kwargs) {};
};
struct xmock_kernel
{
xmock_kernel() = default;
inline py::object parent_header() const
{
return py::dict(py::arg("header") = xeus::get_interpreter().parent_header().get<py::object>());
}
xpyt::xcomm_manager m_comm_manager;
};
/*****************
* kernel module *
*****************/
void bind_history_manager(py::module& kernel_module)
{
py::class_<xeus::xhistory_manager>(kernel_module, "HistoryManager")
.def_property_readonly("session_number", [](xeus::xhistory_manager&) {return 0;})
.def("get_range", [](xeus::xhistory_manager& me,
int session,
int start,
int stop,
bool raw,
bool output)
{
return me.get_range(session, start, stop, raw, output)["history"];
},
py::arg("session") = 0,
py::arg("start") = 0,
py::arg("stop") = 1000,
py::arg("raw") = true,
py::arg("output") = false)
.def("get_range_by_str",
[](const xeus::xhistory_manager& me, py::str range_str, bool raw, bool output)
{
py::list range_split = range_str.attr("split")("-");
int start = std::stoi(py::cast<std::string>(range_split[0]));
int stop = (range_split.size() > 1) ? std::stoi(py::cast<std::string>(range_split[1])) : start + 1;
int session = 0;
return me.get_range(session, start - 1, stop - 1, raw, output)["history"];
},
py::arg("range_str"),
py::arg("raw") = true,
py::arg("output") = false)
.def("get_tail",
[](const xeus::xhistory_manager& me, int last_n, bool raw, bool output)
{
return me.get_tail(last_n, raw, output)["history"];
},
py::arg("last_n"),
py::arg("raw") = true,
py::arg("output") = false
)
.def("search",
[](const xeus::xhistory_manager& me, std::string pattern, bool raw, bool output, py::object py_n, bool unique)
{
int n = py_n.is_none() ? 1000 : py::cast<int>(py_n);
return me.search(pattern, raw, output, n, unique)["history"];
},
py::arg("pattern") = "*",
py::arg("raw") = true,
py::arg("output") = false,
py::arg("n") = py::none(),
py::arg("unique") = false);
}
void bind_comm(py::module& kernel_module)
{
py::class_<xpyt::xcomm>(kernel_module, "Comm")
.def(
py::init<const py::object&, const py::object&, const py::object&, const py::object&, py::kwargs>(),
"target_name"_a="", "data"_a=py::dict(), "metadata"_a=py::dict(), "buffers"_a=py::list()
)
.def("close", &xpyt::xcomm::close, "data"_a=py::dict(), "metadata"_a=py::dict(), "buffers"_a=py::list())
.def("send", &xpyt::xcomm::send, "data"_a=py::dict(), "metadata"_a=py::dict(), "buffers"_a=py::list())
.def("on_msg", &xpyt::xcomm::on_msg)
.def("on_close", &xpyt::xcomm::on_close)
.def_property_readonly("comm_id", &xpyt::xcomm::comm_id)
.def_property_readonly("kernel", &xpyt::xcomm::kernel);
py::class_<xpyt::xcomm_manager>(kernel_module, "CommManager")
.def(py::init<>())
.def("register_target", &xpyt::xcomm_manager::register_target);
}
void bind_mock_objects(py::module& kernel_module)
{
py::class_<xmock_kernel>(kernel_module, "MockKernel", py::dynamic_attr())
.def(py::init<>())
.def_property_readonly("_parent_header", &xmock_kernel::parent_header)
.def_readwrite("comm_manager", &xmock_kernel::m_comm_manager);
py::class_<xmock_ipython>(kernel_module, "MockIPython")
.def(py::init<>())
.def_readwrite("kernel", &xmock_ipython::kernel)
.def("register_post_execute", &xmock_ipython::register_post_execute)
.def("enable_gui", &xmock_ipython::enable_gui)
.def("observe", &xmock_ipython::observe)
.def("showtraceback", &xmock_ipython::showtraceback);
}
struct ipython_instance
{
ipython_instance() : m_instance(py::none())
{
}
py::object get_instance(const py::module& kernel_module) const
{
if (m_instance.is(py::none()))
{
m_instance = kernel_module.attr("MockIPython")();
m_instance.attr("kernel") = kernel_module.attr("MockKernel")();
}
return m_instance;
}
private:
mutable py::object m_instance;
};
py::module get_kernel_module_impl()
{
py::module kernel_module = xpyt::create_module("kernel");
bind_history_manager(kernel_module);
bind_comm(kernel_module);
bind_mock_objects(kernel_module);
// To keep ipywidgets working, we must not import any module from IPython
// before the kernel module has been defined and IPython.core has been
// monkey patched. Otherwise, any call to register_target_comm will execute
// that of IPython instead of that of xeus. Thereafter any call to register_comm
// will execute that of xeus, where the target has not been registered, resulting
// in a segmentation fault.
// Initializing the xeus_python object as a memoized variable ensures the initialization
// of the interactive shell (which imports a lot of module from IPython) will
// occur AFTER IPython.core has been monkey_patched.
// Notice that using a static variable in the lambda to achieve the memoization
// results in a random crash at kernel shutdown.
// Also notice that using an attribute of kernel_module to memoize results
// in random segfault in the interpreter.
ipython_instance ipyinstance;
kernel_module.def("get_ipython", [ipyinstance, kernel_module]() {
return ipyinstance.get_instance(kernel_module);
});
return kernel_module;
}
}
namespace xpyt
{
py::module get_kernel_module(bool raw_mode /*false*/)
{
static py::module kernel_module;
if (raw_mode)
{
kernel_module = xpyt_raw::get_kernel_module_impl();
}
else
{
kernel_module = xpyt_ipython::get_kernel_module_impl();
}
return kernel_module;
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif