On Wed, Jul 22, 2009 at 7:28 AM, Peter Alexander<[email protected]> wrote:
> On Wed, Jul 22, 2009 at 6:38 AM, Robert
> Bradshaw<[email protected]> wrote:
>> 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
>>
>
> Hi Robert,
>
> I'm certainly confused by the semantics. Also, I can't find the use of
> 'global' documented anywhere yet.
> 1) I'm not sure why the "test" file built ok while referencing the
> varible w/ out declaring it to be "global".
> 2) Placing "global mp_head" as the first line of the function body in
> both functions gives a different error(s):
>
> Thank you,
>
> ################################
> $ ./build utils.pyx
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
>        return mp_cur.memory
>
>    mfree():
>        global mp_head
>        cdef:
>            MemPoolItem* mp_cur = mpi_head
>                                         ^
> ------------------------------------------------------------
> /home/travlr/py/cy/utils.pyx:22:42: undeclared name not builtin: mpi_head
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
>            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
>                           ^
> ------------------------------------------------------------
> /home/travlr/py/cy/utils.pyx:27:28: undeclared name not builtin: mpi_cur
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
>            MemPoolItem* mp_tmp = NULL
>        while mp_cur:
>            free(mp_cur.memory)
>            mp_cur.memory = NULL
>            mp_tmp = mpi_cur.next
>            free(mpi_cur)
>                       ^
> ------------------------------------------------------------
> /home/travlr/py/cy/utils.pyx:28:24: undeclared name not builtin: mpi_cur
>
> Error converting Pyrex file to C:
> ------------------------------------------------------------
>        while mp_cur:
>            free(mp_cur.memory)
>            mp_cur.memory = NULL
>            mp_tmp = mpi_cur.next
>            free(mpi_cur)
>            mp_cur = mpi_tmp
>                           ^
> ------------------------------------------------------------
>

Damn, I hate when I find a solution just after I posted the problem.
Please excuse me for I had a mis-spelling.
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to