_IO_文件的Pybind11文件指针包装器 我正在编写一个C++类的Python绑定,它接受一个文件指针-< /p> PYBIND11_MODULE(pywrapper, m) { ... py::class_<Dog, Animal>(m, "Dog") .def(py::init<FILE * const>()); }
我得到了一个预期的错误-_IO_文件的Pybind11文件指针包装器 我正在编写一个C++类的Python绑定,它接受一个文件指针-< /p> PYBIND11_MODULE(pywrapper, m) { ... py::class_<Dog, Animal>(m, "Dog") .def(py::init<FILE * const>()); },python,c++,pybind11,Python,C++,Pybind11,我得到了一个预期的错误- File "main.py", line 6, in test_init client = Dog(f) TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. pywrapper.Dog(arg0: _IO_FILE) Invoked with: <_io.TextIOWrapper nam
File "main.py", line 6, in test_init
client = Dog(f)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. pywrapper.Dog(arg0: _IO_FILE)
Invoked with: <_io.TextIOWrapper name='test.log' mode='w' encoding='UTF-8'>
文件“main.py”,第6行,在test_init中
客户=狗(f)
TypeError:\uuuu init\uuuuu():构造函数参数不兼容。支持以下参数类型:
1.pywrapper.Dog(arg0:_IO_文件)
使用以下命令调用:
我如何在这里编写构造函数的包装器?我认为pybind11中没有实现输入缓冲区。下面是输出缓冲区的实现
以下是将缓冲区用作输出流的示例:
File "main.py", line 6, in test_init
client = Dog(f)
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. pywrapper.Dog(arg0: _IO_FILE)
Invoked with: <_io.TextIOWrapper name='test.log' mode='w' encoding='UTF-8'>
.def("read_from_file_like_object",
[](MyClass&, py::object fileHandle) {
if (!(py::hasattr(fileHandle,"write") &&
py::hasattr(fileHandle,"flush") )){
throw py::type_error("MyClass::read_from_file_like_object(file): incompatible function argument: `file` must be a file-like object, but `"
+(std::string)(py::repr(fileHandle))+"` provided"
);
}
py::detail::pythonbuf buf(fileHandle);
std::ostream stream(&buf);
//... use the stream
},
py::arg("buf")
)