[Pyrex] when pyrex gets tough

David McNab david at rebirthing.co.nz
Mon Sep 3 02:19:49 CEST 2007


On Mon, 2007-09-03 at 11:09 +1200, Greg Ewing wrote:
> cdef extern from "yuv4mpeg.h":
> 
>     ctypedef struct y4m_ratio_t:
>        int n
>        int d
> 
>     y4m_ratio_t y4m_fps_NTSC_FILM
>     y4m_ratio_t y4m_fps_FILM
>     y4m_ratio_t y4m_fps_PAL
8><

Well, that works just fine.

Putting the extern'ed ratio struct constants into the 'cdef extern from'
was just the voodoo I was looking for - it allows the generated C code
to reference them, but doesn't declare them as vars - it correctly
relies on the existing extern decs in yuv4mpeg.h.

(Did I miss something when I was so impatiently reading the pyrex
manual? Is this a special case worth adding to the manual?)

Anyway, thanks for your time and effort, Greg.

It's taught me one thing - Pyrex is a fine precision scalpel, while SWIG
is a huge diesel chainsaw. Each has its own strengths and weaknesses.

The latter, of course, is huge and heavy and can create one hell of a
mess :)

My yuv4mpeg wrapper, done in SWIG, results in a 450k .so file :|

The one good thing about SWIG though is its brilliant and seamless C++
support (hint hint!)

Cheers
David

> cdef void to_ratio(object tup, y4m_ratio_t *ratio):
>     ratio.n = tup[0]
>     ratio.d = tup[1]
> 
> cdef object from_ratio(y4m_ratio_t ratio):
>     return ratio.n, ratio.d
> 
> # Make the constants available to Python
> 
> fps_NTSC_FILM = from_ratio(y4m_fps_NTSC_FILM)
> fps_FILM = from_ratio(y4m_fps_FILM)
> fps_PAL = from_ratio(y4m_fps_PAL)
> 
> 
> The above code compiles, although I haven't tried
> to run it.
> 
> --
> Greg
> 




More information about the Pyrex mailing list