Skip to content

Commit 19b788f

Browse files
authored
Fixed silent mode in raw interpreter (#659)
1 parent e52c6b1 commit 19b788f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/xinterpreter_raw.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ namespace xpyt
107107
py::module context_module = get_request_context_module();
108108
}
109109

110+
namespace
111+
{
112+
class splinter_cell
113+
{
114+
public:
115+
116+
splinter_cell()
117+
{
118+
auto null_out = py::cpp_function([](const std::string&) {});
119+
120+
py::module sys = py::module::import("sys");
121+
m_stdout_func = sys.attr("stdout").attr("write");
122+
m_stderr_func = sys.attr("stderr").attr("write");
123+
sys.attr("stdout").attr("write") = null_out;
124+
sys.attr("stderr").attr("write") = null_out;
125+
}
126+
127+
~splinter_cell()
128+
{
129+
py::module sys = py::module::import("sys");
130+
sys.attr("stdout").attr("write") = m_stdout_func;
131+
sys.attr("stderr").attr("write") = m_stderr_func;
132+
}
133+
134+
private:
135+
136+
py::object m_stdout_func;
137+
py::object m_stderr_func;
138+
};
139+
}
140+
110141
void raw_interpreter::execute_request_impl(
111142
send_reply_callback cb,
112143
int execution_count,
@@ -138,7 +169,7 @@ namespace xpyt
138169
// If the last statement is an expression, we compile it separately
139170
// in an interactive mode (This will trigger the display hook)
140171
py::object last_stmt = expressions[py::len(expressions) - 1];
141-
if (py::isinstance(last_stmt, ast.attr("Expr")))
172+
if (py::isinstance(last_stmt, ast.attr("Expr")) && !config.silent)
142173
{
143174
code_ast.attr("body").attr("pop")();
144175

@@ -161,6 +192,7 @@ namespace xpyt
161192
}
162193
else
163194
{
195+
splinter_cell guard;
164196
py::object compiled_code = builtins.attr("compile")(code_ast, filename, "exec");
165197
exec(compiled_code);
166198
}

0 commit comments

Comments
 (0)