[Pyrex] when pyrex gets tough

David McNab david at rebirthing.co.nz
Sun Sep 2 02:04:31 CEST 2007


Hi,

I'm in the process of generating a python wrapper for the 'yuv4mpeg' API
in mjpegtools.

All was going smoothly until I hit on the framerate ratios:

        typedef struct _y4m_ratio {
          int n;  /* numerator   */
          int d;  /* denominator */
        } y4m_ratio_t;

That's no problem as it stands. What makes it tricky is:

        1. several functions require a pointer to a y4m_ratio_t, and
        
        2. the header file has numerous externs to canned structs, such
        as:
        
                /* 24000/1001 film (in NTSC)  */
                extern const y4m_ratio_t y4m_fps_NTSC_FILM;
                
                /* 24fps film                 */
                extern const y4m_ratio_t y4m_fps_FILM;
                
                /* 25fps PAL                  */
                extern const y4m_ratio_t y4m_fps_PAL;
        
For a python wrapper, it's reasonable to require:

        * a quick/easy way to pass in ratios as tuples, or create
          and use a ratio object
        * easy way to access the externed 'canned ratios' above
        * easy way to access the ratios as tuples

I gave up on the pyrex wrapper, sadly. The upside of this experience is
that it's forced me to grok SWIG, and now I've got a lovely SWIG wrapper
working, offering comfortable interfaces in both Python and C++.

If I had persisted with Pyrex, I would have needed to write several
method wrappers which take a tuple argument, then convert that tuple to
a ratio struct and pass it down to the real method. Also, I still didn't
figure out a way to expose the extern'ed canned ratios in a pythonic
way.

For my future wrap jobs, can anyone suggest a simple and pain-reduced
way of accomplishing the above?

Cheers
David






More information about the Pyrex mailing list