pybind11将python sys.stdout从print()重定向到c++
pybind11 Redirect python sys.stdout to c++ from print()
我有一个带有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文件-如何在不使用print((语句修改python代码的情况下将python sys.stdout重定向为std::string的c++?
这个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 worldn")
}
// sys.stdout is back to its original state
在这种情况下,"捕获"是什么意思?最终,无论是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);
}
我的实现如下
原理是实现一个模块来取代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;
}
相关文章:
- 如何将stdout重定向到stderr
- 编写时C++中的输入重定向问题
- 获取通过重定向传入的输入文件的名称?
- Windows C++重定向库
- 写入文件 a.txt,而终端重定向到文件 b.txt
- 使用 dup2() 进行输入重定向
- 将 clangd stderr 重定向到文件而不是控制台
- 如何在C++中获取重定向网页的URL
- 将标准输出重定向到ostream
- 如何撤消此代码上的 cin 重定向?
- 使用重定向标准处理子进程中的 kbhit
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?
- 重定向输入和输出时如何处理主文件I/O?
- 如何将HTTPS流量重定向到本地主机:443上的代理?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- 尝试在 Windows 上重定向 stdout 和 stderr——_fileno(stdout) 返回 -2
- 可以将Boost消息队列文件重定向到用户指定的位置
- 在程序执行期间从标准输出重定向到自定义流
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- pybind11将python sys.stdout从print()重定向到c++