小编And*_*rge的帖子

如何在Cython C++容器中存储python对象?

我想用将现有的到Python,使用的C++库.在这种情况下,它是adevs库.

问题是如何使用Cython将Python对象存储在C++容器中?对于引用计数问题,我知道这有点 气馁,但是可以这样做,如果是的话,怎么做?

我知道Gauthier Boaglios类似问题的回答.但是,这并没有解决引用计数的问题,显然,我尝试了以下方法:

让我们说'cadevs.pxd'我有以下代码:

cdef extern from "adevs/adevs_digraph.h" namespace "adevs":
cdef cppclass PortValue[VALUE, PORT]:
    PortValue() except +
    PortValue(PORT port, const VALUE& value) except +
    PORT port
    VALUE value
Run Code Online (Sandbox Code Playgroud)

在'adevs.pyx'中:

from cpython.ref cimport PyObject
cimport cadevs

ctypedef PyObject* PythonObject

cdef class PortValue:
    cdef cadevs.PortValue[PythonObject, PythonObject]* _c_portvalue

    def __cinit__(self, object port, object value):
        self._c_portvalue = new cadevs.PortValue[PythonObject, PythonObject](
            <PyObject *>port, <PyObject *>value
        )

    def …
Run Code Online (Sandbox Code Playgroud)

c++ python templates cython

8
推荐指数
1
解决办法
722
查看次数

标签 统计

c++ ×1

cython ×1

python ×1

templates ×1