如何在 Cython 中构建 iostream 对象(例如 cout)并将其传递给 C++ 函数?

How to build in Cython an iostream object (e. g. cout) and pass it to a c++ function?

我正在尝试包装以下 C++ class:
print.h

#include <string>
using namespace std;
class ControlParameters
{
public:
    ControlParameters();
    ~ControlParameters();
    void Initialize(string input, ostream& log);
};

print.cpp

#include "print.h"
#include <iostream>

ControlParameters::ControlParameters()
{
}
ControlParameters::~ControlParameters()
{
}
void ControlParameters::Initialize(string input, ostream& log)
{
 log<<"Output = "<< input <<endl;
}

我的String.pyx看起来如下:

from libcpp.string cimport string
cdef extern from "<iostream>" namespace "std":
    cdef cppclass ostream:
    ┆   ostream& write(const char*, int) except +

cdef extern from "print.h":
    cdef cppclass ControlParameters:
    ┆   ControlParameters() except +
    ┆   void Initialize(string, ostream&)

cdef class PyControlParameters:
    cdef ControlParameters *thisptr
    def __cinit__(self):
    ┆   self.thisptr = new ControlParameters()
    def __dealloc__(self):
    ┆   del self.thisptr
    def PyInitialize(self, a,b):
            self.thisptr.Initialize(a, b)

编译 String.pyx 后出现以下错误:

Compiling String.pyx because it changed.
[1/1] Cythonizing String.pyx

Error compiling Cython file:
------------------------------------------------------------
...
    def __cinit__(self):
        self.thisptr = new ControlParameters()
    def __dealloc__(self):
        del self.thisptr
    def PyInitialize(self, a,b):
            self.thisptr.Initialize(a, b)
                                      ^
------------------------------------------------------------

String.pyx:18:39: Cannot convert Python object to 'ostream'
Traceback (most recent call last):
  File "setup.py", line 7, in <module>
    language="c++"
  File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1039, in cythonize
    cythonize_one(*args)
  File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1161, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: String.pyx

我读到 我必须构建自己的 ostream class 才能获得 ostream 对象。但是我该如何定义这个对象呢? 我想我需要一个将我的 python 对象转换为 ostream 对象的函数。我如何实现这样的功能?

如果你总是想将它发送到 cout 那么最简单的方法是在 Cython 中执行此操作而不处理 Python 中的第二个参数。将 cout 添加到您的 cdef extern:

cdef extern from "<iostream>" namespace "std":
  # define ostream as before
  ostream cout

然后执行 PyInitialize 为:

def PyInitialize (self, a):
  self.thisptr.Initialize(a,cout)

这省去了为 Cython 编写完整的 ostream 包装器 class 的工作量。