Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ namespace xcpp

void interpreter::execute_request_impl(
send_reply_callback cb,
int /*execution_count*/,
int execution_count,
const std::string& code,
xeus::execute_request_config config,
nl::json /*user_expressions*/
Expand Down Expand Up @@ -330,12 +330,13 @@ namespace xcpp
SilentStreamRedirectRAII silent_guard(config.silent);

std::string err;
Cpp::Box output;

// Attempt normal evaluation
try
{
StreamRedirectRAII R(err);
compilation_result = Cpp::Process(code.c_str());
compilation_result = Cpp::Evaluate(code.c_str(), output);
}
catch (std::exception& e)
{
Expand Down Expand Up @@ -384,15 +385,24 @@ namespace xcpp
}
else
{
/*
// Publish a mime bundle for the last return value if
// the semicolon was omitted.
if (!config.silent && output.hasValue() && trim(code).back() != ';')
{
nl::json pub_data = mime_repr(output);
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
}
*/
// Match an interactive C++ shell: publish the last value when the
// input did not suppress display with a trailing semicolon.
const std::size_t last = code.find_last_not_of(" \t\r\n");
if (!config.silent
&& output.getKind() != Cpp::Box::K_Unspecified
&& output.getKind() != Cpp::Box::K_Void
&& last != std::string::npos
&& code[last] != ';')
{
nl::json pub_data = {
{"text/plain", Cpp::GetValueAsString(output)}
};
publish_execution_result(
execution_count,
std::move(pub_data),
nl::json::object()
);
}
// Compose execute_reply message.
kernel_res = xeus::create_successful_reply();
}
Expand Down
46 changes: 45 additions & 1 deletion test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,50 @@ class StreamRedirectRAII {

TEST_SUITE("execute_request")
{
TEST_CASE("publishes the last expression value")
{
std::vector<const char*> args = {};
xcpp::interpreter interpreter(static_cast<int>(args.size()), args.data());

std::string published_type;
nl::json published_content;
interpreter.register_publisher(
[&published_type, &published_content](
xeus::xrequest_context,
const std::string& message_type,
nl::json,
nl::json content,
xeus::buffer_sequence)
{
if (message_type == "execute_result")
{
published_type = message_type;
published_content = std::move(content);
}
}
);

xeus::execute_request_config config;
config.silent = false;
config.store_history = false;
config.allow_stdin = false;
xeus::xrequest_context context(nl::json::object(), {});

std::promise<nl::json> promise;
std::future<nl::json> future = promise.get_future();
interpreter.execute_request(
std::move(context),
[&promise](nl::json result) { promise.set_value(std::move(result)); },
"5",
config,
nl::json::object()
);

REQUIRE(future.get()["status"] == "ok");
REQUIRE(published_type == "execute_result");
REQUIRE(published_content["data"]["text/plain"] == "(int) 5");
}

TEST_CASE("stl")
{
std::vector<const char*> Args = {
Expand Down Expand Up @@ -1266,4 +1310,4 @@ TEST_CASE("Silent mode restores std::cout and std::cerr buffers")
REQUIRE(std::cout.rdbuf() == cout_before);
REQUIRE(std::cerr.rdbuf() == cerr_before);
}
#endif
#endif
Loading