forked from jupyter-xeus/xeus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxinterpreter_raw.hpp
More file actions
105 lines (77 loc) · 3.69 KB
/
Copy pathxinterpreter_raw.hpp
File metadata and controls
105 lines (77 loc) · 3.69 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
/***************************************************************************
* Copyright (c) 2021, QuantStack
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XPYT_INTERPRETER_RAW_HPP
#define XPYT_INTERPRETER_RAW_HPP
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif
#include <string>
#include <memory>
#include "nlohmann/json.hpp"
#include "xeus/xinterpreter.hpp"
#include "pybind11/pybind11.h"
#include "xeus_python_config.hpp"
namespace py = pybind11;
namespace nl = nlohmann;
namespace xpyt
{
class XEUS_PYTHON_API XPYT_FORCE_PYBIND11_EXPORT raw_interpreter : public xeus::xinterpreter
{
public:
using gil_scoped_release_ptr = std::unique_ptr<py::gil_scoped_release>;
// If redirect_output_enabled is true (default) then this interpreter will
// capture outputs and send them using publish_stream.
// Disable this if your interpreter uses custom output redirection.
// If redirect_display_enabled is true (default) then this interpreter will
// overwrite sys.displayhook and send execution results using publish_execution_result.
// Disable this if your interpreter uses custom display hook.
raw_interpreter(
py::dict globals,
bool redirect_output_enabled=true, bool redirect_display_enabled = true);
virtual ~raw_interpreter();
protected:
void configure_impl() override;
void execute_request_impl(send_reply_callback cb,
int execution_counter,
const std::string& code,
xeus::execute_request_config config,
nl::json user_expressions) override;
nl::json complete_request_impl(const std::string& code, int cursor_pos) override;
nl::json inspect_request_impl(const std::string& code,
int cursor_pos,
int detail_level) override;
nl::json is_complete_request_impl(const std::string& code) override;
nl::json kernel_info_request_impl() override;
void shutdown_request_impl() override;
void set_request_context(xeus::xrequest_context context) override;
const xeus::xrequest_context& get_request_context() const noexcept override;
void redirect_output();
py::object m_displayhook;
// The interpreter has the same scope as a `gil_scoped_release` instance
// so that the GIL is not held by default, it will only be held when the
// interpreter wants to execute Python code. This means that whenever
// the interpreter will execute Python code it will need to create an
// `gil_scoped_acquire` instance first.
//
// By default GIL is locked, therefore configure_impl() releases it.
// If an application has already released the GIL by the time the interpreter
// is started, m_release_gil_at_startup has to be set to false to prevent
// releasing it again in configure_impl().
//
bool m_release_gil_at_startup = true;
gil_scoped_release_ptr m_release_gil = nullptr;
bool m_redirect_display_enabled;
std::string _i,_ii,_iii;
py::dict m_global_dict;
};
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#endif