[Pyrex] Initialisation of cdef global arrays with constants [resend]

sjmachin at lexicon.net sjmachin at lexicon.net
Fri Jan 7 21:01:21 CET 2005


Sorry if you are seeing this again, but I sent it on Dec 31 and didn't get my own copy 
back. As 2 other messages arrived from the Pyrex list this morning,  I'm trying again.

------- Forwarded message follows -------
Hello all,

Where in Python I would have at global level:

blah_table = (0, 1, 2, 42, 255)

or in C:

static const int blah_table[5] = {0, 1, 2, 42, 255};

Pyrex appears not to support (yet?) a statement like:

cdef int blah_table[5] = {0, 1, 2, 42, 255}

N.B. I know that I can use the Python style in Pyrex but I want fast
access to the tables.

So far my best try is something like this:

cdef copy_int_py2c(int target[], object source):
   cdef int x, count
   count = len(source)
   for x from 0 <= x < count:
      target[x] = source[x]

cdef int blah1[5]
copy_int_py2c(blah1, [0,1,2,42,255])
cdef int blah2[.....
copy_....

This works for speed, at the expense of code bloat. If the tables were
large, it would be a worry.

Any better suggestions?

Thanks in advance,
John

------- End of forwarded message -------



More information about the Pyrex mailing list