1
我从一个Python函数解析Python的结构为的PyObject
class Bus(Structure):
_fields_ = [ ("a", c_int),
("b", c_char),
("c", c_float),
("d", c_double),
("e", c_char_p)
]
def GetBus( *args, **kwargs):
Signal = cast(c_char_p(kwargs['Bus']), POINTER(Bus)).contents
## Logic that updates Signal
return Signal
在我的C代码,我想更新我的C结构bus
返回以下结构的目的,通过解析pValue
,从获得的:
Bus bus ;
python_Bus = Py_BuildValue("s#", (char*)&bus, sizeof(Bus)) ;
PyDict_SetItemString(pDict,"Bus", python_Bus) ;
PyObject* pValue = PyObject_Call(pFunc, pArgs,pDict) ;
如何解析pValue
?
有没有Py???_???(pvalue, ???) ;
?或者我如何将它转换为char*
并在我的C代码中执行memcpy
(如果这是一种方式)?
我也尝试创建一个新的Python类型,不过貌似这一切归结于我的“getter
”功能Py_BuildValue
,我不希望有几个“setters
”为每一个元素。
使用Python2.7
谢谢!按预期工作。如果我将C'bus' _correctly_传递给Python,还可以请您评论一下吗?它的工作原理,但我想获得评论,如果我做任何愚蠢的或有任何其他好方法 – P0W 2015-04-14 22:14:52
我会创建一个[新类型](https://docs.python.org/2/extending/newtypes.html )并在两侧使用它。为什么要使用ctypes? – tynn 2015-04-14 22:32:52