[Python-Dev] ANN: Pyrex - a language for writing Python extension modules

Kevin Butler kbutler@campuspipeline.com
Thu, 04 Apr 2002 10:08:22 -0700


Greg Ewing wrote:
 > Pyrex is more or less Python with C data types
 > added. You can write functions which freely
 > intermix operations on Python and C data, with
 > all Python reference counting and error checking
 > completely automated.
 >
 > An example Pyrex module is shown below. For more
 > information, see:
 >
 > http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

Greg, this is beautiful.  Exactly what I've been wanting.

:-)

Comments/suggestions:

1- Any chance of having "typecast" spelled as one of:

	char*(value)		#consistent w/ Python
	(char *)value		#consistent w/ C
	cdef char*(value)	#cdef + Python
	(cdef char *)value	#cdef + C
	cdef(char*, value)	#umm. Maybe easier for parsing?

I prefer any of the above over the <> syntax, in rough order of preference...

2- I like the use of the explicit extern declarations to "import" external C 
items. It is analogous to "from X import NAME", rather than the "from X import 
*" behavior suggested by parsing header files.  Also, from previous experience 
w/ SWIG, et al, I think parsing C (& especially C++!) headers is likely to 
bring in more headaches than it is worth. How deeply do you want to get into 
macros, typedefs, other extern declarations, etc.?

3- Any thoughts on accessing C++ classes? It need only include the info  to 
access the subset of members that are needed in Python/Pyrex... Maybe 
something like:

	cdef extern class Grail:
	    # all members implicity "cdef extern"
	    int age
	    float volume

	    # To self or not to self?
	    # Let declarations be consistent w/ C++?
	    def __init__( int age, float volume )
	    def float quaff( float amount )

Wow!  Look at those worms wiggle out of that can!  ;-)  (Next, I'll hit you up 
for exceptions and default args, then overloading, then templates & RTTI... )

kb