c++ - 如何在 python3 中使用 ctypes 导入 ostringstream?
问题描述
我正在为自然语言编写分析器,并且我在 python 3 中有一个用swig
. 我想使用一个函数,它是某种流写入器,它std::ostream & os
作为参数。所以我想如果我在我的python代码中以某种方式导入ostringstream
(读为lib.so
我应该在我的中使用的ctypes.CDLL
)然后将它传递给这个函数,以免调用它create_stream_writer(stream)
,然后使用stream.str()来获取字符串,它会起作用。有没有办法使用 ctypes 或任何其他库来做到这一点?我正在使用运行 Ubuntu 18.04、python3.6 的 docker 容器
我猜代码应该是这样的:
def analyse(text, config):
reader = PlainTextReader.create_string_reader(text, config)
stream = ctypes.ostringstream() # some magic hear
writer = TokenWriter.create_stream_writer('plain', stream, reader.tagset())
for sentence in sentences(reader):
writer.write_sentence(sentence)
return stream.str()
解决方案
你可以这样做(也让它对 Python 开发人员很好)。这个答案本质上是我关于包装 iostreams的旧答案的 Python 3 版本。
为了简化这里的事情,我使用了 boost 的 iostreams 库。如果您不能/不使用 boost,那么您可以从标准 C++ 库组件中编写这一切,这会更加冗长。
我的目标也比映射io.StringIO
到更高std::stringstream
,而是将任何“类似文件”的 Python 对象映射到任何iostream
. 也就是说,我们使用目标在 Python 对象上使用鸭子类型,以便在我们的 C++ 流对象需要时调用read()
和明智地调用。write()
%module test
%{
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/categories.hpp>
// This is just a helper that we can use with boost iostreams to proxy everything to a Python object
struct python_stream_device {
typedef char char_type;
typedef boost::iostreams::bidirectional_device_tag category;
std::streamsize read(char* s, std::streamsize n) {
PyObject *p = PyObject_CallMethod(o, "read", "l", static_cast<long int>(n));
if (PyErr_Occurred()) {
// TODO: throw a C++ exception to back out of wherever we are and then re-throw the Python one...
assert(false);
}
assert(p);
char *ptr = nullptr;
Py_ssize_t len = 0;
PyObject *str = PyUnicode_AsUTF8String(p);
PyBytes_AsStringAndSize(str, &ptr, &len);
if (PyErr_Occurred()) {
assert(false); // Let's just pretend this is error handlng...
}
memcpy(s, ptr, len);
Py_DECREF(str);
Py_DECREF(p);
return len;
}
std::streamsize write(const char* s, std::streamsize n) {
PyObject *ret = PyObject_CallMethod(o, "write", "s#", s, static_cast<Py_ssize_t>(n));
if (PyErr_Occurred()) {
// See above
assert(false);
}
std::streamsize r = PyLong_AsSsize_t(ret);
Py_DECREF(ret);
return r;
}
// Using this means we can rely on the default synthesised operator= + copy ctor etc. and saves us some code.
swig::SwigPtr_PyObject o;
python_stream_device(PyObject *o) : o(o) {}
};
typedef boost::iostreams::stream<python_stream_device> python_stream;
%}
// Here is the stuff that wraps it neatly
%typemap(in) std::iostream& (python_stream tmp) {
// Writing the typemap this way lets us get RAII semantics despite the goto in the SWIG macros in the simplest way
tmp.open(python_stream_device($input));
$1 = &tmp;
}
// We can just use the same typemaps for other cases too:
%apply std::iostream& { std::istream&, std::ostream& };
// Below is just for testing:
%{
#include <iostream>
%}
%inline %{
// This is the function you want to call
void fun1(std::ostream& out) {
assert(out.good());
out << "Hello world, from C++";
assert(out.good());
}
// This one is here for completeness because once you've got this far you may as well support this too.
void fun2(std::istream& in) {
std::string tmp;
//in >> tmp;
std::getline(in, tmp);
assert(in.good());
std::cout << "fun2 got: " << tmp << std::endl;
}
%}
这样就足够了,您可以像这样使用一些 Python:
import io
import test
i=io.StringIO()
test.fun1(i)
print('After fun1: %s' % i.getvalue())
i=io.StringIO('hello world, from Python!\n')
test.fun2(i)
推荐阅读
- html - 如何缩放我的 .svg 以填充整个视口?
- javascript - 在特定 div 中移动 div 并在某些断点处再次返回
- java - 为同时状态更改请求验证 CSRF 令牌
- ruby - 如何独立于规范助手运行 RSpec?
- ios - 快速的firebase聊天对话
- apache-spark - apache spark(V. 2.1) 流式传输 java
- dart - 在颤振中使用新关键字
- scala - zipWithIndex 和过滤器在输出中产生“Vector”字符串
- sql - 根据特定列连接/合并来自两个不相关表的记录
- javascript - 选中/取消选中输入(复选框)php变量