From ce94c93aca704b549e6aaeff9d8a32910a52215d Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Mon, 10 Nov 2025 18:04:42 +0530 Subject: [PATCH 1/2] Fix execution in silent mode --- src/xinterpreter.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/xinterpreter.cpp b/src/xinterpreter.cpp index 15d6861b..284a28dd 100644 --- a/src/xinterpreter.cpp +++ b/src/xinterpreter.cpp @@ -161,11 +161,12 @@ __get_cxx_version () auto cout_strbuf = std::cout.rdbuf(); auto cerr_strbuf = std::cerr.rdbuf(); + std::unique_ptr nullbuf; if (config.silent) { - auto null = xnull(); - std::cout.rdbuf(&null); - std::cerr.rdbuf(&null); + nullbuf = std::make_unique(); + std::cout.rdbuf(nullbuf.get()); + std::cerr.rdbuf(nullbuf.get()); } std::string err; From a4ecd707d933ba63a62294f90b647db9a57edb27 Mon Sep 17 00:00:00 2001 From: anutosh491 Date: Mon, 10 Nov 2025 18:23:24 +0530 Subject: [PATCH 2/2] Add tests --- test/test_interpreter.cpp | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test_interpreter.cpp b/test/test_interpreter.cpp index 5ce9d540..292d4bee 100644 --- a/test/test_interpreter.cpp +++ b/test/test_interpreter.cpp @@ -1136,3 +1136,44 @@ TEST_CASE("C and C++ stdout/stderr capture") REQUIRE(captured_err.find("C stderr") != std::string::npos); REQUIRE(captured_err.find("C++ stderr") != std::string::npos); } + +TEST_CASE("Silent mode suppresses C and C++ stdout/stderr") +{ + std::vector Args = {}; + xcpp::interpreter interpreter((int)Args.size(), Args.data()); + + xeus::execute_request_config config; + // forcing silent as true + config.silent = true; + config.store_history = false; + config.allow_stdin = false; + + nl::json header = nl::json::object(); + xeus::xrequest_context::guid_list id = {}; + xeus::xrequest_context context(header, id); + + std::promise promise; + auto callback = [&promise](nl::json result) { promise.set_value(result); }; + + StreamRedirectRAII cout_redirect(std::cout); + StreamRedirectRAII cerr_redirect(std::cerr); + + std::string code = R"( + #include + #include + printf("C stdout\n"); + fprintf(stderr, "C stderr\n"); + std::cout << "C++ stdout\n"; + std::cerr << "C++ stderr\n"; + )"; + + interpreter.execute_request(context, callback, code, config, nl::json::object()); + (void)promise.get_future().get(); + + std::string captured_out = cout_redirect.getCaptured(); + std::string captured_err = cerr_redirect.getCaptured(); + + // Nothing should reach the redirected streams in silent mode + REQUIRE(captured_out.empty()); + REQUIRE(captured_err.empty()); +}