Pyrex without Python (was Re: calling Pyrex results from C)

Paul Prescod paul at prescod.net
Tue Jan 20 20:29:02 EST 2004


Jeff Epler wrote:

> As I mentioned in another thread a few weeks ago, a current hobby is
> writing code for small embedded processors, where "small" means 1k-4k
> instructions and 128-1k bytes data.  C is a fine language for this as long
> as you don't use FP arithmetic or much of the standard library.

You're definiately pushing the edge. Your domain name is appropriate. ;)

> I wish I could write my code in Pyrex, of course keeping to 'int8_t'
> and 'int16_t' as only basic data types, but it inevitably makes Python
> API calls.
> 
> How close to impossible would it be to make Pyrex emit Python-free code
> if there are only 'cdefs'? 

It will generate a bunch of Python module crap. You could extract the 
pure-C code from the middle of it.

The C code generated by the cdefs is pretty pure.

static int __pyx_f_11pyrexmodule_fast_fib(int __pyx_v_n,int 
__pyx_v_a,int __pyx_
v_b) {
   int __pyx_r;
   int __pyx_1;

   /* "/Users/pprescod/code/fib/pyrexmodule.pyx":5 */
   __pyx_1 = (__pyx_v_n == 0);
   if (__pyx_1) {

     /* "/Users/pprescod/code/fib/pyrexmodule.pyx":6 */
     __pyx_r = __pyx_v_b;
     goto __pyx_L0;
     goto __pyx_L2;
   }
   /*else*/ {

     /* "/Users/pprescod/code/fib/pyrexmodule.pyx":8 */
     __pyx_r = __pyx_f_11pyrexmodule_fast_fib((__pyx_v_n - 
1),__pyx_v_b,(__pyx_v_
a + __pyx_v_b));
     goto __pyx_L0;
   }
   __pyx_L2:;

   __pyx_r = 0;
   goto __pyx_L0;
   __pyx_L1:;
   __Pyx_WriteUnraisable("pyrexmodule.fast_fib");
   __pyx_L0:;
   return __pyx_r;
}

> ... Are there any obvious gotchas in
> Pyrex-generated code if 'int' is a 16-bit datatype?  Is there some
> reason that large stack frames and/or calls to malloc() would be
> unavoidable in Pyrex?

Well...I think it is great to use Pyrex to avoid C (even ignoring Python 
extending). But onje of the main benefits of Pyrex is memory management 
(refcount + GC just like Python). I really don't see much benefit to 
using Pyrex if even _C_'s standard memory management is too extravagent 
for your needs. What exactly would Pyrex buy you if you can't afford to 
use its high-level features like memory management, constructors and OO?

  Paul Prescod






More information about the Python-list mailing list