Skip to content

Commit f79c660

Browse files
committed
Implement chached input
Signed-off-by: martinRenou <martin.renou@gmail.com>
1 parent 739f4f2 commit f79c660

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/xinterpreter.cpp

+31-21
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,17 @@ namespace xpyt
9696

9797
// Add get_ipython to global namespace
9898
py::globals()["get_ipython"] = get_kernel_module().attr("get_ipython");
99-
99+
100100
// Initializes get_ipython result
101101
get_kernel_module().attr("get_ipython")();
102102

103103
m_has_ipython = get_kernel_module().attr("has_ipython").cast<bool>();
104104
//m_has_ipython = true;
105+
106+
// Initialize cached inputs
107+
py::globals()["_i"] = "";
108+
py::globals()["_ii"] = "";
109+
py::globals()["_iii"] = "";
105110
}
106111

107112
nl::json interpreter::execute_request_impl(int execution_count,
@@ -129,26 +134,26 @@ namespace xpyt
129134
// - either we reimplement the parsing logic in xeus-python
130135
// - or this logic is extracted from IPython into a dedicated package, that becomes
131136
// a dependency of both xeus-python and IPython.
132-
if (code.size() >= 2 && code[0] == '?')
133-
{
134-
std::string result = formatted_docstring(code);
135-
if (result.empty())
136-
{
137-
result = "Object " + code.substr(1) + " not found.";
138-
}
139-
140-
kernel_res["status"] = "ok";
141-
kernel_res["payload"] = nl::json::array();
142-
kernel_res["payload"][0] = nl::json::object({
143-
{"data", {
144-
{"text/plain", result}
145-
}},
146-
{"source", "page"},
147-
{"start", 0}
148-
});
149-
kernel_res["user_expressions"] = nl::json::object();
150-
151-
return kernel_res;
137+
if (code.size() >= 2 && code[0] == '?')
138+
{
139+
std::string result = formatted_docstring(code);
140+
if (result.empty())
141+
{
142+
result = "Object " + code.substr(1) + " not found.";
143+
}
144+
145+
kernel_res["status"] = "ok";
146+
kernel_res["payload"] = nl::json::array();
147+
kernel_res["payload"][0] = nl::json::object({
148+
{"data", {
149+
{"text/plain", result}
150+
}},
151+
{"source", "page"},
152+
{"start", 0}
153+
});
154+
kernel_res["user_expressions"] = nl::json::object();
155+
156+
return kernel_res;
152157
}
153158
code_copy = code;
154159
}
@@ -237,6 +242,11 @@ namespace xpyt
237242
kernel_res["traceback"] = error.m_traceback;
238243
}
239244

245+
// Cache inputs
246+
py::globals()["_iii"] = py::globals()["_ii"];
247+
py::globals()["_ii"] = py::globals()["_i"];
248+
py::globals()["_i"] = code;
249+
240250
return kernel_res;
241251
}
242252

0 commit comments

Comments
 (0)