Types 将cstruct转换为python字符串
以下是我要完成的工作的总结:Types 将cstruct转换为python字符串,types,cython,Types,Cython,以下是我要完成的工作的总结: 将不透明的cstruct转换为python数组 对阵列进行酸洗 我在说服cython让我将C对象转换成python对象方面遇到了问题 这是一个让我头疼的简化版代码。 这在myothermodule.pyx文件中 cdef char* struct_to_string(structtype ctarg): cdef: bspmat *bsptarg list arrays = [] np.ndarray[np.f
cdef char* struct_to_string(structtype ctarg):
cdef:
bspmat *bsptarg
list arrays = []
np.ndarray[np.float64_t, ndim=2] arr
char * string_data
for i in range(ctarg.numlevel):
bsptarg = ctarg.level[i]
arr = bspmat_to_array(bsptarg) #this works fine in other contexts
arrays.append(arr)
cdef bytes pystr = cPickle.dumps(arrays, protocol=2) #fastest protocol
string_data = <char *> pystr
return string_data
这是尝试编译时产生的错误类型:
middle.pyx:232:45: Cannot convert 'structtype *' to Python object
Error compiling Cython file:
------------------------------------------------------------
[...]
ctarg = pv.another_cython_struct
strdata = myothermodule.struct_to_string( ctarg )
^
------------------------------------------------------------
middle.pyx:232:37: Obtaining 'char *' from temporary Python value
Traceback (most recent call last):
File "setup.py", line 21, in <module>
ext_modules = cythonize(extensions)
File "/usr/local/lib/python2.7/dist-packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py", line 785, in cythonize
cythonize_one(*args[1:])
File "/usr/local/lib/python2.7/dist-packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py", line 902, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: middle.pyx
middle.pyx:232:45:无法将“structtype*”转换为Python对象
编译Cython文件时出错:
------------------------------------------------------------
[...]
ctarg=pv.另一个cython结构
strdata=myothermodule.struct_to_string(ctarg)
^
------------------------------------------------------------
middle.pyx:232:37:从临时Python值中获取“char*”
回溯(最近一次呼叫最后一次):
文件“setup.py”,第21行,在
ext_modules=cythonize(扩展)
cythonize中的文件“/usr/local/lib/python2.7/dist packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py”,第785行
cythonize_one(*args[1:]
文件“/usr/local/lib/python2.7/dist packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py”,第902行,在cythonize_one中
raise编译器错误(无,pyx_文件)
Cython.Compiler.Errors.CompileError:middle.pyx
任何关于如何说服这项工作的线索都将不胜感激 第一个错误与
structtype
和structtype*
之间的差异有关。您可以通过将cdef char*struct\u的定义更改为_string(structtype*ctarg)
来解决这个问题(但很难确定,因为您没有提供structtype
的定义)(实际上,我认为这可能不是导致第一条错误消息的原因,但这肯定是个问题)是否有定义为structtype
和struct\u to\u string
的myothermodule.pxd,然后在middle.pyx中定义cimport myothermodule
?并且应该myothermodule.struct\u到字符串(ctarg)
是myothermodule.struct\u到字符串(ctarg[0])
即不是指针?
middle.pyx:232:45: Cannot convert 'structtype *' to Python object
Error compiling Cython file:
------------------------------------------------------------
[...]
ctarg = pv.another_cython_struct
strdata = myothermodule.struct_to_string( ctarg )
^
------------------------------------------------------------
middle.pyx:232:37: Obtaining 'char *' from temporary Python value
Traceback (most recent call last):
File "setup.py", line 21, in <module>
ext_modules = cythonize(extensions)
File "/usr/local/lib/python2.7/dist-packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py", line 785, in cythonize
cythonize_one(*args[1:])
File "/usr/local/lib/python2.7/dist-packages/Cython-0.20.1-py2.7-linux-x86_64.egg/Cython/Build/Dependencies.py", line 902, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: middle.pyx