转换Python对象到C void类型 [英] Convert Python object to C void type
本文介绍了转换Python对象到C void类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用用Cython转换Python对象到C void类型?
How can I convert Python object to C void type using Cython?
目前,当我尝试投我得到这个消息
Currently I am getting this message when I try to cast
铸造临时Python对象以非数字非Python类型
Casting temporary Python object to non-numeric non-Python type
推荐答案
这可以这样做:
1。从Python的铸造到C
如果你真正的意思无效*
这将是:
If you really meant void *
this would be :
some_pyobj = "abc"
cdef void *ptr
ptr = <void *>some_pyobj
如果你的意思是的PyObject *
这将是:
If you meant PyObject *
this would be :
cdef PyObject *ptr
ptr = <PyObject *>some_pyobj # Cast from Python object to C pointer
然后,从C端,的PyObject结构
可通过包括 Python.h
。
下面是引用(从 object.h
Python的包含文件):
Here is the reference (from object.h
Python include file) :
/* Nothing is actually declared to be a PyObject, but every pointer to
* a Python object can be cast to a PyObject*. This is inheritance built
* by hand. Similarly every pointer to a variable-size Python object can,
* in addition, be cast to PyVarObject*.
*/
typedef struct _object {
PyObject_HEAD
} PyObject;
2。从C铸造到Python
它工作在两种方式,这意味着以下也是可能的:
It works in both ways, meaning that the following is also possible :
cdef PyObject *ptr
ptr = <PyObject *>some_pyobj
cdef object some_other_pyobj
some_other_pyobj = <object>ptr # Cast from C pointer to Python object
这篇关于转换Python对象到C void类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文