@@ -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