我有一个带有pybind11嵌入式python解释器的c ++程序,执行以下python文件,它将直接打印到std::cout
# test.py
print("text")
执行文件的c ++程序:
#include <pybind11/embed.h>
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{};
py::eval_file("test.py");
}
我发现其他解决方案需要修改python文件-如何将python sys.stdout作为std :: string重定向到c ++, 而无需仅使用print()语句修改python代码?< / p>
答案 0 :(得分:1)
此github问题描述了一种方法: https://github.com/pybind/pybind11/issues/1622
逐字复制该问题的代码。建议使用以下位使其起作用:
#include <pybind11/pybind11.h>
namespace py = pybind11;
从问题开始:
class PyStdErrOutStreamRedirect {
py::object _stdout;
py::object _stderr;
py::object _stdout_buffer;
py::object _stderr_buffer;
public:
PyStdErrOutStreamRedirect() {
auto sysm = py::module::import("sys");
_stdout = sysm.attr("stdout");
_stderr = sysm.attr("stderr");
auto stringio = py::module::import("io").attr("StringIO");
_stdout_buffer = stringio(); // Other filelike object can be used here as well, such as objects created by pybind11
_stderr_buffer = stringio();
sysm.attr("stdout") = _stdout_buffer;
sysm.attr("stderr") = _stderr_buffer;
}
std::string stdoutString() {
_stdout_buffer.attr("seek")(0);
return py::str(_stdout_buffer.attr("read")());
}
std::string stderrString() {
_stderr_buffer.attr("seek")(0);
return py::str(_stderr_buffer.attr("read")());
}
~PyStdErrOutStreamRedirect() {
auto sysm = py::module::import("sys");
sysm.attr("stdout") = _stdout;
sysm.attr("stderr") = _stderr;
}
};
用法:
{
PyStdErrOutStreamRedirect pyOutputRedirect{};
py::print("hello world");
// Other noisy python functions can be put here
assert(pyOutputRedirect.stdoutString() == "hello world\n")
}
// sys.stdout is back to its original state
答案 1 :(得分:0)
在这种情况下,“捕获”是什么意思?最终,无论是python还是C ++,编写都是通过操作系统进行的。如果只是要使输出静音,将其写入文件,将其发送到另一个进程等,等等,则可以拦截该级别的所有输出。
下面是一个示例,该示例压缩python脚本期间的所有stdout输出,然后还原,此后stdout的行为将像以前一样(对于python和其他情况):
#include <pybind11/embed.h>
#include <unistd.h>
namespace py = pybind11;
int main() {
auto fdo = fileno(stdout);
auto savefd = dup(fdo);
auto f = fopen("/dev/null", "w");
dup2(fileno(f), fdo);
py::scoped_interpreter guard{};
py::eval_file("test.py");
fflush(stdout);
dup2(savefd, fdo);
}
答案 2 :(得分:0)
我的实现如下
原理是实现一个模块来代替sys.stdout
#include <iostream>
#include <pybind11/embed.h>
PYBIND11_EMBEDDED_MODULE(my_sys, m) {
struct my_stdout {
my_stdout() = default;
my_stdout(const my_stdout &) = default;
my_stdout(my_stdout &&) = default;
};
py::class_<my_stdout> my_stdout(m, "my_stdout");
my_stdout.def_static("write", [](py::object buffer) {
std::cout << buffer.cast<std::string>();
});
my_stdout.def_static("flush", []() {
std::cout << std::flush;
});
m.def("hook_stdout", []() {
auto py_sys = py::module::import("sys");
auto my_sys = py::module::import("my_sys");
py_sys.attr("stdout") = my_sys.attr("my_stdout");
});
}
auto main(int argc, char **argv) -> int {
py::scoped_interpreter python;
#if 0
py_stdout.attr("write") = py::cpp_function([](py::object info) {
std::cout << "info" << std::endl;
});
#else
py::module::import("my_sys").attr("hook_stdout")();
#endif
py::print("Hello, World!\n")
return 0;
}