Skip to content

Commit bd51e03

Browse files
Vipul-Cariappamcbarton
authored andcommitted
implement sub-interpreters
1 parent 81fed93 commit bd51e03

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ set(XEUS_CPP_SRC
189189
src/xparser.cpp
190190
src/xutils.cpp
191191
src/xmagics/os.cpp
192+
src/xmagics/multi_interpreter.cpp
192193
)
193194

194195
if(NOT EMSCRIPTEN)

src/xinterpreter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "xparser.hpp"
2626
#include "xsystem.hpp"
2727

28+
#include "xmagics/multi_interpreter.hpp"
29+
2830
using Args = std::vector<const char*>;
2931

3032
void* createInterpreter(const Args &ExtraArgs = {}) {
@@ -371,6 +373,7 @@ __get_cxx_version ()
371373
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit",
372374
// timeit(&m_interpreter));
373375
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
376+
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("subinterp", multi_interpreter());
374377
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
375378
#ifndef EMSCRIPTEN
376379
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());

src/xmagics/multi_interpreter.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/************************************************************************************
2+
* Copyright (c) 2025, xeus-cpp contributors *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
************************************************************************************/
8+
9+
#include "multi_interpreter.hpp"
10+
11+
#include <iostream>
12+
#include <iterator>
13+
#include <sstream>
14+
#include <string>
15+
#include <vector>
16+
17+
#include "CppInterOp/CppInterOp.h"
18+
19+
static std::vector<std::string> split(const std::string& input)
20+
{
21+
std::istringstream buffer(input);
22+
std::vector<std::string> ret(
23+
(std::istream_iterator<std::string>(buffer)),
24+
std::istream_iterator<std::string>()
25+
);
26+
return ret;
27+
}
28+
29+
static constexpr size_t len(std::string_view n)
30+
{
31+
return n.size();
32+
}
33+
34+
namespace xcpp
35+
{
36+
void multi_interpreter::operator()(const std::string& line, const std::string& cell)
37+
{
38+
auto Args0 = split(line.substr(len("subinterp")));
39+
40+
Cpp::TInterp_t OldI = Cpp::GetInterpreter(); // we need to restore the old interpreter
41+
Cpp::TInterp_t I = nullptr;
42+
std::string name;
43+
if (Args0[0] == "--use")
44+
{
45+
I = interpreters[Args0[1]];
46+
}
47+
else
48+
{
49+
auto named = std::find(Args0.begin(), Args0.end(), "--name");
50+
if (named != Args0.end())
51+
{
52+
name = *(named + 1);
53+
}
54+
}
55+
56+
std::vector<const char*> Args(I ? 0 : Args0.size());
57+
if (!I)
58+
{
59+
for (auto start = Args0.begin(), end = Args0.end(); start != end; start++)
60+
{
61+
if (*start == "--name")
62+
{
63+
start++;
64+
continue;
65+
}
66+
Args.push_back((*start).c_str());
67+
}
68+
}
69+
70+
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
71+
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
72+
if (!I)
73+
{
74+
I = Cpp::CreateInterpreter(Args); // TODO: error handling
75+
}
76+
if (I)
77+
{
78+
Cpp::Declare(cell.c_str(), false, I);
79+
}
80+
std::cout << Cpp::EndStdStreamCapture();
81+
std::cerr << Cpp::EndStdStreamCapture();
82+
83+
if (!name.empty())
84+
{
85+
interpreters[name] = I; // TODO: check if this is redefinition
86+
Cpp::ActivateInterpreter(OldI); // restoring old interpreter
87+
}
88+
else
89+
{
90+
Cpp::DeleteInterpreter(I);
91+
}
92+
}
93+
} // namespace xcpp

src/xmagics/multi_interpreter.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/************************************************************************************
2+
* Copyright (c) 2025, xeus-cpp contributors *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
************************************************************************************/
8+
9+
#ifndef XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP
10+
#define XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP
11+
12+
#include <string>
13+
#include <unordered_map>
14+
15+
#include "CppInterOp/CppInterOp.h"
16+
#include "xeus-cpp/xmagics.hpp"
17+
18+
namespace xcpp
19+
{
20+
class multi_interpreter : public xmagic_cell
21+
{
22+
public:
23+
24+
XEUS_CPP_API
25+
void operator()(const std::string& line, const std::string& cell) override;
26+
27+
private:
28+
29+
std::unordered_map<std::string, Cpp::TInterp_t> interpreters;
30+
};
31+
} // namespace xcpp
32+
33+
#endif // XEUS_CPP_MULTI_INTERPRETER_MAGIC_HPP

0 commit comments

Comments
 (0)