如何在python3中使用ctypes导入ostringstream?

时间:2019-07-07 13:09:47

标签: c++ python-3.x wrapper ctypes swig

我正在编写用于自然语言的分析器,并且用python 3 swig创建了c ++代码包装。我想使用一个函数,它是某种流编写器,它以std::ostream & os作为参数。因此,我想如果我以某种方式在我的python代码中导入ostringstream(在lib.so中应使用的ctypes.CDLL形式)然后将其传递给此函数,以免称为{ {1}},然后使用stream.str()获取字符串。是否可以使用ctypes或其他任何库来执行此操作? 我正在使用运行Ubuntu 18.04,python3.6

的Docker容器 我猜

代码应该像这样:

create_stream_writer(stream)

2 个答案:

答案 0 :(得分:2)

您可以执行此操作(也使它对Python开发人员也很好)。该答案本质上是我的较早答案on wrapping 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)

答案 1 :(得分:0)

正如Mark Tolonen在注释中指出的那样,使用ctypes不可能做到这一点。因此,我只编写了满足我所有需求的c ++函数,然后使用SWIG创建了包装器。因为在SWIG中使用typemap将StringIO(Python)映射到ostreingstream(C ++)看起来像是魔术,我找不到实现此目的的方法。