gpt4 book ai didi

python - pybind11 将 python sys.stdout 从 print() 重定向到 C++

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:41 26 4
gpt4 key购买 nike

我有一个带有 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 重定向到 c++ 作为 std::string 而不仅使用 print() 语句修改 python 代码?

最佳答案

这个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

关于python - pybind11 将 python sys.stdout 从 print() 重定向到 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58758429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com