"存储临时Python参考的不安全C衍生物"当试图访问结构指针时

时间:2018-05-10 09:13:57

标签: cython

我想使用一个给我一个动态数组的库。动态数组结构有一个属性with tf.Session() as sess: with tf.variable_scope('experiment'): a = tf.get_variable('a', [1000,24,128], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1) ) b = tf.get_variable('b', [1000,15,128], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1) ) c = tf.get_variable('c', [1000,24,15], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1)) for i in range(1000): ai = tf.slice(a,[i,0,0],[1,-1,-1]) # (1,24,128) bi = tf.slice(b,[i,0,0],[1,-1,-1]) # (1,15,128) aii = tf.reshape(ai, [ int(ai.get_shape()[1]), int(ai.get_shape()[2])] ) # (24, 128) bii = tf.reshape(bi, [ int(bi.get_shape()[1]), int(bi.get_shape()[2])] ) # (15, 128) ci = contract_func(aii,bii) # (24,15) c[i] = ci ,它给出了数组的开头。

在构建列表之后,我想在cython中访问此指针(以制作数组的副本)。但我似乎无法从结构中获取指针元素。

这是我的pyx:

void* _heap_ptr

这是错误消息:

cimport src.clist as l

def main():

    cdef l.ptr_list basic_list
    cdef int i = 42
    basic_list = l.create_list_size(sizeof(i), 100)

    l.list_add_ptr(basic_list, &i)

    cdef int* arr;

    arr = basic_list._heap_ptr

    for i in range(1):
        print(arr[i])

我的pxd:

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

    l.list_add_ptr(basic_list, &i)

    cdef int* arr;

    arr = basic_list._heap_ptr
                   ^
------------------------------------------------------------

src/test.pyx:14:20: Cannot convert Python object to 'int *'

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

    l.list_add_ptr(basic_list, &i)

    cdef int* arr;

    arr = basic_list._heap_ptr
                   ^
------------------------------------------------------------

src/test.pyx:14:20: Storing unsafe C derivative of temporary Python reference

如何修复我的代码?为什么会这样?根据我的调查,如果您忘记将某些内容声明为C(例如,使用malloc而不是libc.stdlib.malloc,那么会弹出错误消息,但我无法看到此处发生类似的事情。)

0 个答案:

没有答案