forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIOHandler.cxx
More file actions
268 lines (229 loc) · 6.34 KB
/
Copy pathIOHandler.cxx
File metadata and controls
268 lines (229 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Author: Danilo Piparo, Omar Zapata, Enric Tejedor 16/12/2015
/*************************************************************************
* Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include <Python.h>
#include <fcntl.h>
#ifdef _MSC_VER // Visual Studio
#include <winsock2.h>
#include <io.h>
#pragma comment(lib, "Ws2_32.lib")
#define pipe(fds) _pipe(fds, 1048575, _O_BINARY)
#define read _read
#define dup _dup
#define dup2 _dup2
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#else
#include <unistd.h>
#endif
#include <string>
#include <iostream>
#include "TInterpreter.h"
//////////////////////////
// MODULE FUNCTIONALITY //
//////////////////////////
class JupyROOTExecutorHandler {
private:
bool fCapturing = false;
std::string fStdoutpipe;
std::string fStderrpipe;
int fStdout_pipe[2] = {0, 0};
int fStderr_pipe[2] = {0, 0};
int fSaved_stderr = 0;
int fSaved_stdout = 0;
public:
JupyROOTExecutorHandler();
void Poll();
void InitCapture();
void EndCapture();
void Clear();
std::string &GetStdout();
std::string &GetStderr();
};
#ifndef F_LINUX_SPECIFIC_BASE
#define F_LINUX_SPECIFIC_BASE 1024
#endif
#ifndef F_SETPIPE_SZ
#define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
#endif
constexpr long MAX_PIPE_SIZE = 1048575;
JupyROOTExecutorHandler::JupyROOTExecutorHandler() {}
static void PollImpl(FILE *stdStream, int *pipeHandle, std::string &pipeContent)
{
fflush(stdStream);
#ifdef _MSC_VER
char buffer[60000] = "";
struct _stat st;
_fstat(pipeHandle[0], &st);
if (st.st_size) {
_read(pipeHandle[0], buffer, 60000);
pipeContent += buffer;
}
#else
int buf_read;
char ch;
while (true) {
buf_read = read(pipeHandle[0], &ch, 1);
if (buf_read == 1) {
pipeContent += ch;
} else
break;
}
#endif
}
void JupyROOTExecutorHandler::Poll()
{
PollImpl(stdout, fStdout_pipe, fStdoutpipe);
PollImpl(stderr, fStderr_pipe, fStderrpipe);
}
static void InitCaptureImpl(int &savedStdStream, int *pipeHandle, int FILENO)
{
savedStdStream = dup(FILENO);
if (pipe(pipeHandle) != 0) {
return;
}
#ifndef _MSC_VER
long flags_stdout = fcntl(pipeHandle[0], F_GETFL);
if (flags_stdout == -1)
return;
flags_stdout |= O_NONBLOCK;
fcntl(pipeHandle[0], F_SETFL, flags_stdout);
fcntl(pipeHandle[0], F_SETPIPE_SZ, MAX_PIPE_SIZE);
#endif
dup2(pipeHandle[1], FILENO);
}
void JupyROOTExecutorHandler::InitCapture()
{
if (!fCapturing) {
InitCaptureImpl(fSaved_stdout, fStdout_pipe, STDOUT_FILENO);
InitCaptureImpl(fSaved_stderr, fStderr_pipe, STDERR_FILENO);
fCapturing = true;
}
}
void JupyROOTExecutorHandler::EndCapture()
{
if (fCapturing) {
Poll();
dup2(fSaved_stdout, STDOUT_FILENO);
dup2(fSaved_stderr, STDERR_FILENO);
close(fSaved_stdout);
close(fSaved_stderr);
close(fStdout_pipe[0]);
close(fStdout_pipe[1]);
close(fStderr_pipe[0]);
close(fStderr_pipe[1]);
fCapturing = false;
}
}
void JupyROOTExecutorHandler::Clear()
{
fStdoutpipe = "";
fStderrpipe = "";
}
std::string &JupyROOTExecutorHandler::GetStdout()
{
return fStdoutpipe;
}
std::string &JupyROOTExecutorHandler::GetStderr()
{
return fStderrpipe;
}
JupyROOTExecutorHandler *JupyROOTExecutorHandler_ptr = nullptr;
bool JupyROOTExecutorImpl(const char *code)
{
auto status = false;
try {
auto err = TInterpreter::kNoError;
if (gInterpreter->ProcessLine(code, &err)) {
status = true;
}
if (err == TInterpreter::kProcessing) {
gInterpreter->ProcessLine(".@");
gInterpreter->ProcessLine("cerr << \"Unbalanced braces. This cell was not processed.\" << endl;");
}
} catch (...) {
status = true;
}
return status;
}
bool JupyROOTDeclarerImpl(const char *code)
{
auto status = false;
try {
if (gInterpreter->Declare(code)) {
status = true;
}
} catch (...) {
status = true;
}
return status;
}
PyObject *JupyROOTExecutor(PyObject * /*self*/, PyObject *args)
{
const char *code;
if (!PyArg_ParseTuple(args, "s", &code))
return NULL;
auto res = JupyROOTExecutorImpl(code);
return PyLong_FromLong(res);
}
PyObject *JupyROOTDeclarer(PyObject * /*self*/, PyObject *args)
{
const char *code;
if (!PyArg_ParseTuple(args, "s", &code))
return NULL;
auto res = JupyROOTDeclarerImpl(code);
return PyLong_FromLong(res);
}
PyObject *JupyROOTExecutorHandler_Clear(PyObject * /*self*/, PyObject * /*args*/)
{
JupyROOTExecutorHandler_ptr->Clear();
Py_RETURN_NONE;
}
PyObject *JupyROOTExecutorHandler_Ctor(PyObject * /*self*/, PyObject * /*args*/)
{
if (!JupyROOTExecutorHandler_ptr) {
JupyROOTExecutorHandler_ptr = new JupyROOTExecutorHandler();
// Fixes for ROOT-7999
gInterpreter->ProcessLine("SetErrorHandler((ErrorHandlerFunc_t)&DefaultErrorHandler);");
}
Py_RETURN_NONE;
}
PyObject *JupyROOTExecutorHandler_Poll(PyObject * /*self*/, PyObject * /*args*/)
{
JupyROOTExecutorHandler_ptr->Poll();
Py_RETURN_NONE;
}
PyObject *JupyROOTExecutorHandler_EndCapture(PyObject * /*self*/, PyObject * /*args*/)
{
JupyROOTExecutorHandler_ptr->EndCapture();
Py_RETURN_NONE;
}
PyObject *JupyROOTExecutorHandler_InitCapture(PyObject * /*self*/, PyObject * /*args*/)
{
JupyROOTExecutorHandler_ptr->InitCapture();
Py_RETURN_NONE;
}
PyObject *JupyROOTExecutorHandler_GetStdout(PyObject * /*self*/, PyObject * /*args*/)
{
auto out = JupyROOTExecutorHandler_ptr->GetStdout().c_str();
return PyUnicode_FromString(out);
}
PyObject *JupyROOTExecutorHandler_GetStderr(PyObject * /*self*/, PyObject * /*args*/)
{
auto err = JupyROOTExecutorHandler_ptr->GetStderr().c_str();
return PyUnicode_FromString(err);
}
PyObject *JupyROOTExecutorHandler_Dtor(PyObject * /*self*/, PyObject * /*args*/)
{
if (!JupyROOTExecutorHandler_ptr)
Py_RETURN_NONE;
delete JupyROOTExecutorHandler_ptr;
JupyROOTExecutorHandler_ptr = nullptr;
Py_RETURN_NONE;
}