Recipe 17.2. Implementing a Simple Extension Type with PyrexCredit: Alex Martelli ProblemYou want to code and build an extension type for Python with a minimal amount of hard work. SolutionThe Pyrex language is the simplest and fastest way to build Python extensions. Once we have Pyrex installed, the next step is to create a setup.py file to use the distutils package to build and install our module: from distutils.core import setup, Extension from Pyrex.Distutils import build_ext setup(name = "elemlist", version = "1.0", maintainer = "Alex Martelli", maintainer_email = "amcx@aleax.it", description = "Simple Python extension module in Pyrex", ext_modules = [Extension('elemlist',sources=['elemlist.pyx'])], cmdclass = {'build_ext': build_ext}, ) Then, we need a file elemlist.pyx with our module's source code: cdef class cons: cdef public object car, cdr def _ _init_ _(self, car, cdr): self.car = car self.cdr = cdr def _ _repr_ _(self): return 'cons(%r, %r)' % (self.car, self.cdr) DiscussionPyrex is a language for writing Python extension modules. It was developed by Greg Ewing and is freely downloadable and installable. The Pyrex language is a large subset of Python, with the addition of a few language constructs to allow easy generation of fast C code. In this recipe, the only special Pyrex construct we use is the cdef statement, which lets us express C-level declarations. This module is roughly equivalent to the Python-coded module: class cons(object): _ _slots_ _ = ('car', 'cdr') def _ _init_ _(self, car, cdr): self.car = car self.cdr = cdr def _ _repr_ _(self): return 'cons(%r, %r)' % (self.car, self.cdr) As you see, Pyrex code is very similar to Python code. Pyrex code gets compiled into C, which in turn gets compiled to machine code, while Python code is compiled into bytecode instead. For such a simple type as cons, the Pyrex version is not much faster than the Python version, but a pure C version, such as the one that I demonstrated previously in Recipe 17.1, despite having 25 times as much code, wouldn't be any faster either. Building a compiled extension module is just as simple when it's coded in Pyrex as when it's coded directly in C, thanks to the distutils package, which does all of the hard work. (You need to have Pyrex installed.) When you build a Pyrex-coded module, you may get warnings from your C compiler about symbols with names starting with _ _pyx or _ _pyx that are defined but not used, or declared but not defined. Do not let these warning messages worry you: your C compiler is running with the highest possible level of warnings, and the little anomalies it's warning about are just perfectly normal and innocuous artifacts in the C sources that Pyrex automatically generates. Pyrex is not quite finished yet (the Pyrex version at the time of writing is 0.9.3), so no attention has yet been spent on purely cosmetic warts. (By the time you read this, a mature version of Pyrex may have been released, with all i's dotted and all t's crossed. Nevertheless, I would recommend Pyrex even if the latest version still causes numerous warnings.)
See AlsoAbundant documentation on Pyrex, as well as examples, can be found in the directory (and particularly in subdirectories Doc and Demos) where you unpacked Pyrex's .tar.gz file; essentially the same documentation can also be read online, starting from the Pyrex web site at http://nz.cosc.canterbury.ac.nz/~greg/python/Pyrex/. |