On Jul 21, 2009, at 10:47 PM, Peter Alexander wrote:

> Hi all,
>
> I'm getting a cython build[1] error[2] from the following code  
> snippet[3].
> A test file[4] using a similar implementation (stack memory only)  
> built fine.
> Could you help me see what I'm doing wrong here.

Cython variable semantics are just like they are in Python,  
specifically, note that you are assigning to mp_head in that function.

mp_head = mp_cur

This makes mp_head into a local variable (and, since you haven't  
specified a type, of type object). Just like Python, if you want to  
assign to a global variable, you need to put "global mp_head" in your  
function body.

To make this error more clear, it would be a good idea to emit a  
warning that mp_head is being used before it is defined.

- Robert

> I'm using repo: cython-devel changeset: 2094:bcc850a1253a date: Mon
> Jul 20 15:40:26 2009 -0700
> Thanks ~Pete
>
> [1] #######  build.sh script ##########
>
> cython -a -I/home/travlr/src/numpy.gitsvn/doc/cython/ "$1"
>
> gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing - 
> std=c99\
>     -I/usr/include/python2.6 -o ${1%\.*}.so ${1%\.*}.c
>
> ######## end build.sh script ########
>
> [2] ######## build error message ########
>
> $ ./build utils.pyx
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
>     void* mmalloc(size_t size, size_t n_items):
>         cdef:
>             MemPoolItem* mp_cur = <MemPoolItem*>malloc(sizeof 
> (MemPoolItem))
>         mp_cur.memory = malloc(size * n_items)
>         mp_cur.next = mp_head
>                             ^
> ------------------------------------------------------------
> /home/travlr/py/cy/utils.pyx:14:29: Cannot convert Python object to
> 'MemPoolItem *'
>
> [...]
>
> ######## end build error message ###########
>
> [3] ######## code snippet #########
>
> from defs cimport *
>
> cdef:
>     struct MemPoolItem:
>         void*           memory
>         MemPoolItem*    next
>
>     MemPoolItem* mp_head=NULL
>
>     void* mmalloc(size_t size, size_t n_items):
>         cdef:
>             MemPoolItem* mp_cur = <MemPoolItem*>malloc(sizeof 
> (MemPoolItem))
>         mp_cur.memory = malloc(size * n_items)
>         mp_cur.next = mp_head
>         mp_head = mp_cur
>         return mp_cur.memory
>
>     mfree():
>         cdef:
>             MemPoolItem* mp_cur = mpi_head
>             MemPoolItem* mp_tmp = NULL
>         while mp_cur:
>             free(mp_cur.memory)
>             mp_cur.memory = NULL
>             mp_tmp = mpi_cur.next
>             free(mpi_cur)
>             mp_cur = mpi_tmp
>         mp_head = NULL
>
> ######## end code snippet #########
>
> [4] ######## test file #########
>
> cdef:
>     struct S_t:
>         int val
>         S_t* next
>
>     S_t s
>
>     _run():
>         s.val = 345
>         print s.val
>
> def run():
>     _run()
>
> ########### end test file ############
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to