Skip to content

Commit 06218f5

Browse files
alexander-penevgithub-actions[bot]vgvassilev
authored
Initial merge with xeus-clang-repl - Capture outputs (#59)
* Initial merge with xeus-clang-repl - Capture outputs --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Vassil Vassilev <[email protected]>
1 parent ce3f8e9 commit 06218f5

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ endif ()
129129
function(configure_kernel kernel)
130130
set(XEUS_CPP_PATH "$ENV{PATH}")
131131
set(XEUS_CPP_LD_LIBRARY_PATH "$ENV{LD_LIBRARY_PATH}")
132-
set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CppInterOp_CLANG_VERSION})
132+
set(XEUS_CPP_RESOURCE_DIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/clang/${CPPINTEROP_LLVM_VERSION_MAJOR})
133133

134134
configure_file (
135135
"${CMAKE_CURRENT_SOURCE_DIR}/${kernel}/kernel.json.in"

environment.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ name: xeus-cpp
22
channels:
33
- conda-forge
44
dependencies:
5-
- xeus-cpp=0.0.1
5+
- xeus-cpp

src/xinterpreter.cpp

+21-4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ using namespace std::placeholders;
5959

6060
namespace xcpp
6161
{
62+
struct StreamRedirectRAII {
63+
std::string &err;
64+
StreamRedirectRAII(std::string &e) : err(e) {
65+
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
66+
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
67+
}
68+
~StreamRedirectRAII() {
69+
std::string out = Cpp::EndStdStreamCapture();
70+
err = Cpp::EndStdStreamCapture();
71+
std::cout << out;
72+
}
73+
};
74+
6275
void interpreter::configure_impl()
6376
{
6477
xeus::register_interpreter(this);
@@ -153,28 +166,32 @@ __get_cxx_version ()
153166
std::cerr.rdbuf(&null);
154167
}
155168

169+
std::string err;
170+
156171
// Attempt normal evaluation
157172
try
158173
{
174+
StreamRedirectRAII R(err);
159175
compilation_result = Cpp::Process(code.c_str());
160176
}
161177
catch (std::exception& e)
162178
{
163179
errorlevel = 1;
164-
ename = "Standard Exception :";
180+
ename = "Standard Exception: ";
165181
evalue = e.what();
166182
}
167183
catch (...)
168184
{
169185
errorlevel = 1;
170-
ename = "Error :";
186+
ename = "Error: ";
171187
}
172188

173189
if (compilation_result)
174190
{
175191
errorlevel = 1;
176-
ename = "Error :";
177-
evalue = "Compilation error!";
192+
ename = "Error: ";
193+
evalue = "Compilation error! " + err;
194+
std::cerr << err;
178195
}
179196

180197
// Flush streams

test/test_xcpp_kernel.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import unittest
1010
import jupyter_kernel_test
11+
import platform
1112

1213

1314
class XCppTests(jupyter_kernel_test.KernelTests):
@@ -24,7 +25,12 @@ class XCppTests(jupyter_kernel_test.KernelTests):
2425
code_stderr = '#include <iostream>\nstd::cerr << "oops" << std::endl;'
2526

2627
# Pager: code that should display something (anything) in the pager
27-
#code_page_something = "?std::vector"
28+
code_page_something = "?std::vector"
29+
30+
# Exception throwing
31+
# TODO: Remove 'if' when test work on MacOS/arm64. Throw Exceptions make
32+
# kernel/test non-workable.
33+
###code_generate_error = 'throw std::runtime_error("Unknown exception");' if platform.system() != "Darwin" or platform.processor() != 'arm' else ''
2834

2935
# Samples of code which generate a result value (ie, some text
3036
# displayed as Out[n])
@@ -41,8 +47,45 @@ class XCppTests(jupyter_kernel_test.KernelTests):
4147
{
4248
'code': '#include <string>\n#include "xcpp/xdisplay.hpp"\nstd::string test("foobar");\nxcpp::display(test);',
4349
'mime': 'text/plain'
50+
},
51+
{
52+
'code': """
53+
#include <string>
54+
#include <fstream>
55+
#include "nlohmann/json.hpp"
56+
#include "xtl/xbase64.hpp"
57+
namespace im {
58+
struct image {
59+
inline image(const std::string& filename) {
60+
std::ifstream fin(filename, std::ios::binary);
61+
m_buffer << fin.rdbuf();
62+
}
63+
std::stringstream m_buffer;
64+
};
65+
nlohmann::json mime_bundle_repr(const image& i) {
66+
auto bundle = nlohmann::json::object();
67+
bundle["image/png"] = xtl::base64encode(i.m_buffer.str());
68+
return bundle;
69+
}
70+
}
71+
#include "xcpp/xdisplay.hpp"
72+
im::image marie("../notebooks/images/marie.png");
73+
xcpp::display(marie);""",
74+
'mime': 'image/png'
4475
}
4576
]
4677

78+
79+
class XCppTests2(jupyter_kernel_test.KernelTests):
80+
81+
kernel_name = 'xcpp'
82+
83+
# language_info.name in a kernel_info_reply should match this
84+
language_name = 'C++'
85+
86+
# Code that should write the exact string `hello, world` to STDOUT
87+
code_hello_world = '#include <stdio.h>\nprintf("hello, world");'
88+
89+
4790
if __name__ == '__main__':
4891
unittest.main()

0 commit comments

Comments
 (0)