22#include <pybind11/eval.h>
23#include <pybind11/iostream.h>
24#include <pybind11/pybind11.h>
35 using namespace pybind11;
36 py::print(s,
"end"_a =
"");
41 using namespace pybind11;
67void register_py_to_sham_print(py::module &m) {
71 wrapper_io(
int fileno) : _fileno(fileno) {}
72 void write(py::object &buffer) {
shambase::print(buffer.cast<std::string>()); }
75 int fileno() {
return _fileno; }
78 py::class_<wrapper_io>(m,
"wrapper_io")
80 .def(
"write", &wrapper_io::write)
81 .def(
"flush", &wrapper_io::flush)
82 .def(
"isatty", &wrapper_io::isatty)
83 .def(
"fileno", &wrapper_io::fileno);
85 m.def(
"hook_stdout", [&]() {
87 auto sys = py::module::import(
"sys");
91 def __init__(self, backend):
92 self.backend = backend
93 def write(self, text):
94 self.backend.write(text)
98 return self.backend.isatty()
100 return self.backend.fileno()
103 py::object py_wrapper_class = py::globals()["PyStdWrapper"];
105 py::object backend_out = py::cast(wrapper_io(1));
106 py::object backend_err = py::cast(wrapper_io(2));
107 py::object stdout_wrapper = py_wrapper_class(backend_out);
108 py::object stderr_wrapper = py_wrapper_class(backend_err);
110 sys.attr(
"stdout") = stdout_wrapper;
111 sys.attr(
"stderr") = stderr_wrapper;
113 }
catch (std::exception &e) {
119namespace shambindings {
121 enum { None = 0, Lib = 1, Embed = 2 } init_state = None;
123 template<
bool is_lib_mode>
124 void init(py::module &m) {
126 m.attr(
"__doc__") = R
"doc(Python bindings for Shamrock)doc";
146 void init_lib(py::module &m) { shambindings::init<true>(m); }
149 shambindings::init<false>(m);
151 register_py_to_sham_print(m);
152 m.attr(
"hook_stdout")();
157 if (init_state != Lib) {
160 "python bindings not initialized as lib mode, current mode = {}",
167 if (init_state != Embed) {
170 "python bindings not initialized as embed mode, current mode = {}",
std::int32_t i32
32 bit integer
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void print(std::string_view sv)
Prints a string to the console.
void flush()
Flushes the output buffer.
void change_printer(void(*func_printer_normal)(std::string_view), void(*func_printer_ln)(std::string_view), void(*func_flush_func)())
Changes the behavior of the print, println and flush functions.
bool is_a_tty()
Test if current terminal is a tty.
Pybind11 include and definitions.
std::function< void(py::module &)> fct_sig
alias to pybind11 namespace
std::unique_ptr< std::vector< fct_sig > > static_init_shamrock_pybind
Statically initialized python module init function list We use a unique pointer to ensure that the ve...
void py_func_printer_normal(std::string_view s)
With pybind we print using python out stream.
void py_func_flush_func()
Python print performs already a flush so we need nothing here.
void register_pybind_init_func(fct_sig fct)
Add a python module init function to the init list.
void py_func_printer_ln(std::string_view s)
With pybind we print using python out stream.
void expect_init_embed(SourceLocation loc=SourceLocation{})
Expect python bindings to be initialized as embed mode, throws if not.
void init_embed(py::module &m, bool hook_stdout=true)
Init python bindings and register them to Python API.
void expect_init_lib(SourceLocation loc=SourceLocation{})
Expect python bindings to be initialized as lib mode, throws if not.
void init_lib(py::module &m)
Init python bindings and register them to Python API.
provide information about the source location
This file contains tty info getters.