【问题标题】:Wrapping/Casting C struct in Cython to Python class将 Cython 中的 C 结构包装/转换为 Python 类
【发布时间】:2014-06-26 02:06:10
【问题描述】:

我刚刚开始熟悉 Cython,尝试将一些结构从 C 库包装到 Python 方法和类。我不太明白从(初始化的)C 结构到相应的 Python 类的转换应该如何工作。我在这里错过了什么:

来自 C 头文件的 sn-p:

struct test_struct {
    int _something;
    complex_struct* _another;
};
typedef struct test_struct test;

test *test_new(void);
int some_method(test **n, int irrelevant);

我的 .pxd 中对应的 sn-p:

cdef struct test_struct:
    pass
ctypedef test_struct test

test* test_new()
int some_method(test **n, int irrelevant)

我的 .pyx:

def do_something(int irrelevant):
    cdef test* t = test_new()
    ret = some_method(&t, irrelevant)

    # Here comes the problem...
    return <Test?>t

cdef class Test:  
    cdef test* _t

    # cinit here and some methods here. No members except _t

return 语句之前的所有内容都可以正常工作。我在ret 等中得到了正确的值。但返回语句中的强制转换似乎不正确或缺少更多信息。发出t = do_something(42) Python 段错误时。

段错误本身没有任何帮助:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a9e74b in internal_print () from /usr/lib/libpython2.7.so.1.0
(gdb) bt
#0  0x00007ffff7a9e74b in internal_print () from /usr/lib/libpython2.7.so.1.0
#1  0x00007ffff7a81adb in PyFile_WriteObject () from /usr/lib/libpython2.7.so.1.0
#2  0x00007ffff7b19acb in sys_displayhook () from /usr/lib/libpython2.7.so.1.0
#3  0x00007ffff7a64c23 in PyObject_Call () from /usr/lib/libpython2.7.so.1.0
#4  0x00007ffff7af2ff7 in PyEval_CallObjectWithKeywords () from /usr/lib/libpython2.7.so.1.0
#5  0x00007ffff7af6f3b in PyEval_EvalFrameEx () from /usr/lib/libpython2.7.so.1.0
#6  0x00007ffff7af91b0 in PyEval_EvalCodeEx () from /usr/lib/libpython2.7.so.1.0
#7  0x00007ffff7af92b2 in PyEval_EvalCode () from /usr/lib/libpython2.7.so.1.0
#8  0x00007ffff7b11f9f in run_mod () from /usr/lib/libpython2.7.so.1.0
#9  0x00007ffff7b13ec0 in PyRun_InteractiveOneFlags () from /usr/lib/libpython2.7.so.1.0
#10 0x00007ffff7b140ae in PyRun_InteractiveLoopFlags () from /usr/lib/libpython2.7.so.1.0
#11 0x00007ffff7b1470e in PyRun_AnyFileExFlags () from /usr/lib/libpython2.7.so.1.0
#12 0x00007ffff7b24bcf in Py_Main () from /usr/lib/libpython2.7.so.1.0
#13 0x00007ffff746f000 in __libc_start_main () from /usr/lib/libc.so.6
#14 0x000000000040073e in _start ()

如您所见,do_something 方法应该返回一个 Test 类型的 Python 对象。我需要添加什么才能使演员成功?我需要手动将结构映射到 Python 类吗?我可以使用一些 Cython 魔法吗?

【问题讨论】:

  • 你不想强制转换结构。您想创建一个Test 实例并使用该struct 对其进行初始化。我假设您已经尝试过:return Test(t) [或您的初始化程序具有的任何签名]。

标签: python c struct casting cython


【解决方案1】:

您需要将Test 变成C 类型的实际包装器。但是您也不能将 C 参数传递给 Python 函数(例如构造函数)。所以你还需要一个工厂函数。这是一个例子:

cdef class Test:
    cdef test* _t

    def __cinit__(self):
        self._t = NULL

    def _setup(self, test* t):
        self._t = t
        return self

    property something:
        def __get__(self):
            return self._t._something
        def __set__(self, int val):
            self._t._something = val

cdef Test_create(test* t):
    return Test()._setup(t)

然后在do_something():

return Test_create(t)

【讨论】:

  • 我认为def _setup中的def应该是cdef _setup
猜你喜欢
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2016-09-10
  • 1970-01-01
  • 2019-04-20
相关资源
最近更新 更多