Sturla Molden | 6 Jan 14:22
Picon

Re: [Cython] Arrays of Python Objects


Matthew Bromberg wrote:

> I was wrong.  A buffer of the type
> cdef object[int] myhash
> can only be declared locally inside some function or method.

Ignore my last post!!!

You are confusing PEP 3118 buffers with C arrays.

You probably want this:

    DEF BUF_SIZE = 32
    cdef object myhash[BUF_SIZE]

which should work at module scope. This says "myhash is a C array of
object, of length BUF_SIZE."

Your statement

   cdef object[int] myhash

is not a Cython syntax error, but means something else:

"object is a type that exposes a PEP 3118 Py_buffer storing C integers,
and myhash is an instance of PEP 3118 Py_buffer."

A Py_buffer can only be used inside a method because of the auxillary
variables to which it unboxes for fast array lookup. Hence the error
(Continue reading)

Stefan Behnel | 6 Jan 14:28
Picon
Favicon

Re: [Cython] Arrays of Python Objects


Sturla Molden, 06.01.2010 14:22:
> You probably want this:
> 
>     DEF BUF_SIZE = 32
>     cdef object myhash[BUF_SIZE]

And, in case the array size needs to be determined at runtime, why not just 
use a regular Python tuple?

Stefan

Sturla Molden | 6 Jan 14:46
Picon

Re: [Cython] Arrays of Python Objects


> And, in case the array size needs to be determined at runtime, why not
> just
> use a regular Python tuple?

I agree. It seems array lookup is not the bottleneck here, but object
creation. He should just store a container on module scope. I would have
used a Python list as it is mutable.

cdef list myhash = [extclass() for n in range(bufsize)]

S.M.


Gmane