Skip to content

Commit 8c8bafd

Browse files
author
kr-2003
committed
fixed redirection
1 parent 5ab3a13 commit 8c8bafd

File tree

2 files changed

+5
-82
lines changed

2 files changed

+5
-82
lines changed

share/jupyter/kernels/xcpp17-debugger/kernel.json.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"{connection_file}",
1111
"-g",
1212
"-O0",
13+
"--use-oop-jit",
1314
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
1415
"-I", "@XEUS_CPP_INCLUDE_DIR@",
1516
"-std=c++17"

src/xinterpreter.cpp

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,6 @@
2929

3030
using Args = std::vector<const char*>;
3131

32-
static int g_stdin_pipe[2] = {-1, -1};
33-
static int g_stdout_pipe[2] = {-1, -1};
34-
static int g_stderr_pipe[2] = {-1, -1};
35-
static bool g_pipes_initialized = false;
36-
37-
// Initialize global pipes once
38-
void init_global_pipes() {
39-
if (!g_pipes_initialized) {
40-
if (pipe(g_stdin_pipe) == 0 && pipe(g_stdout_pipe) == 0 && pipe(g_stderr_pipe) == 0) {
41-
g_pipes_initialized = true;
42-
} else {
43-
throw std::runtime_error("Failed to create global pipes");
44-
}
45-
}
46-
}
47-
4832
void* createInterpreter(const Args& ExtraArgs = {})
4933
{
5034
Args ClangArgs = {/*"-xc++"*/"-v"};
@@ -68,67 +52,13 @@ void* createInterpreter(const Args& ExtraArgs = {})
6852

6953
// FIXME: We should process the kernel input options and conditionally pass
7054
// the gpu args here.
71-
if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
72-
return s == "-g";}) == ExtraArgs.end()) {
73-
// If no debugger option, then use the non-OOP JIT execution.
74-
return Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
75-
}
76-
77-
init_global_pipes();
78-
79-
return Cpp::CreateInterpreter(ClangArgs, {}, true, g_stdin_pipe[0], g_stdout_pipe[1], g_stderr_pipe[1]);
55+
return Cpp::CreateInterpreter(ClangArgs /*, {"-cuda"}*/);
8056
}
8157

8258
using namespace std::placeholders;
8359

8460
namespace xcpp
8561
{
86-
class GlobalPipeRedirectRAII {
87-
private:
88-
std::string captured_stderr;
89-
90-
std::string read_all_from_fd(int fd) {
91-
std::string result;
92-
char buffer[4096];
93-
ssize_t bytes_read;
94-
95-
// Set non-blocking to avoid hanging
96-
int flags = fcntl(fd, F_GETFL, 0);
97-
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
98-
99-
while ((bytes_read = read(fd, buffer, sizeof(buffer))) > 0) {
100-
result.append(buffer, bytes_read);
101-
}
102-
103-
// Restore original flags
104-
fcntl(fd, F_SETFL, flags);
105-
return result;
106-
}
107-
108-
void write_to_fd(int fd, const std::string& data) {
109-
write(fd, data.c_str(), data.length());
110-
}
111-
112-
public:
113-
GlobalPipeRedirectRAII(const std::string& input_data = "") {
114-
if (!input_data.empty() && g_stdin_pipe[1] != -1) {
115-
write_to_fd(g_stdin_pipe[1], input_data);
116-
close(g_stdin_pipe[1]); // Close write end to signal EOF
117-
}
118-
}
119-
~GlobalPipeRedirectRAII() {
120-
// Read any pending output from pipes
121-
std::string stdout_content = read_all_from_fd(g_stdout_pipe[0]);
122-
captured_stderr = read_all_from_fd(g_stderr_pipe[0]);
123-
124-
// Output captured stdout content
125-
if (!stdout_content.empty()) {
126-
std::cout << stdout_content;
127-
}
128-
}
129-
130-
const std::string& get_captured_stderr() const { return captured_stderr; }
131-
};
13262
struct StreamRedirectRAII {
13363
std::string &err;
13464
StreamRedirectRAII(std::string &e) : err(e) {
@@ -246,20 +176,12 @@ __get_cxx_version ()
246176
m_code_to_execution_count_map[code].push_back(execution_count);
247177
m_execution_count_to_code_map[execution_count] = code;
248178

249-
bool use_out_of_process = g_pipes_initialized;
250-
251179
// Attempt normal evaluation
252180
try
253181
{
254-
if (use_out_of_process) {
255-
std::string input_for_process = "";
256-
GlobalPipeRedirectRAII redirect(input_for_process);
257-
compilation_result = Cpp::Process(code.c_str());
258-
err = redirect.get_captured_stderr();
259-
} else {
260-
StreamRedirectRAII R(err);
261-
compilation_result = Cpp::Process(code.c_str());
262-
}
182+
183+
StreamRedirectRAII R(err);
184+
compilation_result = Cpp::Process(code.c_str());
263185
}
264186
catch (std::exception& e)
265187
{

0 commit comments

Comments
 (0)