[Pyrex] Question

Duncan Booth duncan at rcp.co.uk
Wed Apr 16 13:26:41 CEST 2003


On 16 Apr 2003 at 12:47, Riccardo Trocca wrote:

> Hello, I'm learning PyRex and I'm trying to port some work already done with Swig to pyrex as an exercise.
> The first problem I've found is:
> in the .h I need to wrap I've a lot of #defines of values that must be passed to the C calls. I'd like to export thos 
> values to the python module.
> For example I've got #define RIMAGE_BGR24 0x8001.
> I declared it in the import part of the pyx file because I need it inside the pyrex code.
> Now what can I do in order to mymodule.RIMAGE_BGR24?

How about something like this?

---- myhdr.h ----
#define RIMAGE_BGR24 0x8001
#define ANOTHER 1234
---- test.pyx ----
cdef extern from "myhdr.h":
    int c_RIMAGE_BGR24  "RIMAGE_BGR24"
    int c_ANOTHER       "ANOTHER"

RIMAGE_BGR24 = c_RIMAGE_BGR24
ANOTHER = c_ANOTHER
-------------------

>>> import test
>>> test.ANOTHER
1234
>>> test.RIMAGE_BGR24
32769

If you have a lot of such symbols you could probably knock 
something up in Python to scan the header file and generate 
the .pyx (or a .pxi?) automatically.
-- 
Duncan Booth                                     
duncan at dales.rmplc.co.uk
int month(char *p){return(124864/((p[0]+p[1]-
p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://dales.rmplc.co.uk/Duncan





More information about the Pyrex mailing list