cython将void *转换回python obj

时间:2014-06-08 05:41:40

标签: python c cython

想要将void *转换回python obj时收到错误消息。

基于这篇文章 Convert Python object to C void type 转换应该很容易。

struct _node {
    void * data;
} node;

cpdef match(self, path):
    m = cpyr3.r3_tree_match(self.root, path, NULL)

    if m:
        return <object>m.data

错误消息:

Error compiling Cython file:
------------------------------------------------------------
...

    cpdef match(self, path):
        m = cpyr3.r3_tree_match(self.root, path, NULL)

        if m:
            return <object>m.data
                           ^
------------------------------------------------------------

cpyr3.pyx:18:28: Cannot convert 'node *' to Python object

我测试了几种方法:

1

Error compiling Cython file:
------------------------------------------------------------
...
        cpyr3.r3_tree_compile(self.root, NULL)

    cpdef match(self, path):
        m = cpyr3.r3_tree_match(self.root, path, NULL)
        if m:
            return <object>(m.data[0])
                            ^
------------------------------------------------------------

cpyr3.pyx:17:29: Cannot convert 'node *' to Python object

2

这个将通过编译,但在运行时引发异常

Traceback (most recent call last):
  File "test.py", line 22, in <module>
    print run(r)
  File "test.py", line 13, in run
    return r.match('/foo/bar')
  File "cpyr3.pyx", line 14, in pyr3.R3Tree.match (cpyr3.c:1243)
    cpdef match(self, path):
  File "cpyr3.pyx", line 17, in pyr3.R3Tree.match (cpyr3.c:1186)
    return <object>(m[0].data[0])
AttributeError: 'dict' object has no attribute 'data'

字典来自哪里?

EDIT2:重现问题(详情):

来源回购:https://github.com/lucemia/pyr3/tree/cython

git clone https://github.com/lucemia/pyr3/ -b cython 
cd pyr3
rm cpyr3.c
rm pyr3.c
git submodule init; git submodule update; 
cd r3
./autogen.sh
./configure
cd ..
python setup_cython.py build_ext --inplace

EDIT3:

更改cpyr3.pxd 来自

cdef extern from "r3.h":
    ctypedef struct node:
        pass

cdef extern from "r3.h":
    ctypedef struct node:
        void *data

解决问题!

1 个答案:

答案 0 :(得分:2)

我测试了

cdef struct _node:
    void * data
ctypedef _node node

cpdef pack_unpack():
    cdef node my_node = node(<void *>"some object")

    # This is what should be returned
    cdef node *m = &my_node
    return <object>m.data

import pyximport
pyximport.install()

import c_unpacker
print(c_unpacker.pack_unpack())

它完全正常。

所以我的答案是其他可能正在发生的事情。如果我有一个可以测试的最小可运行样本,这将有所帮助。

要检查三个可能的错误:

  • m未输入或输入错误

  • self.rootpath类型错误

  • _node输入错误。例如,cdef struct _node: pass我得到了

    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    cpdef pack_unpack():
        cdef node my_node = node(<void *>"some object")
    
        # This is what should be returned
        cdef node *m = &my_node
        return <object>m.data
                       ^
    ------------------------------------------------------------
    
    c_unpacker.pyx:10:20: Cannot convert 'node *' to Python object
    

    这是因为根据Cython看到的错误定义,m没有属性data。在这些场景中,Cython默认转换为Python类型。


通过编译步骤,我设法找到了你的

cdef extern from "r3.h":
    ctypedef struct node:
        pass

这是我名单上的第三点。您应该将其更改为

cdef extern from "r3.h":
    ctypedef struct node:
        void *data

这会删除该错误。