将cstruct转换为python字符串

时间:2016-02-08 10:38:13

标签: types cython

以下是我要完成的内容摘要:

  1. 将不透明的cstruct转换为python数组
  2. Pickle the Array
  3. 我在说服cython让我将C对象转换为python对象方面遇到了问题。

    这是代码的简化版本,令我头疼。 这是文件myothermodule.pyx

    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:

    cdef push_data(mycythonstruct pv):
        cdef:
            cdef char * strdata
            cdef myothermodule.structtype *ctarg
        ctarg = pv.another_cython_struct
        strdata = myothermodule.struct_to_string( ctarg )
    

    这是尝试编译时产生的错误类型:

    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
    

    如何说服这个工作的任何线索将不胜感激!

0 个答案:

没有答案