Section 25.2. Extending Python Without Python s C API


25.2. Extending Python Without Python's C API

You can code Python extensions in other classic compiled languages besides C. For Fortran, the choice is between Paul Dubois's Pyfort (available at http://pyfortran.sf.net) and Pearu Peterson's F2PY (available at http://cens.ioc.ee/projects/f2py2e/). Both packages support and require the Numeric package covered in "The Numeric Package" on page 378, since numeric processing is Fortran's typical application area.

For C++, you have many choices. SCXX (available at http://davidf.sjsoft.com/mirrors/mcmillan-inc/scxx.html) is a simple, lightweight package that uses no templates and is thus suitable for older C++ compilers. PyCXX (available at http://cxx.sf.net) uses a modest amount of templates, essentially ones from the C++ standard library. SIP (available at http://www.riverbankcomputing.co.uk/sip/index.php) also supports the C++ extensions needed to use the powerful Qt cross-platform libraries, but, while it fully supports Qt, it does not require it. The Boost Python Library (available at http://www.boost.org/libs/python/doc) is part of Boost, a vast treasury of powerful, template-rich C++ libraries, of uniformly high quality, that need and support modern C++ compilers that support templates very well.

Of course, you may also choose to use Python's C API from your C++ code, using C++ in this respect as if it were C, and foregoing the extra convenience that C++ affords. However, if you're already using C++ rather than C anyway, then using SCXX, PyCXX, SIP, or Boost can substantially improve your programming productivity when compared to using Python's underlying C API.

If your Python extension is basically a wrapper over an existing C or C++ library (as many are), consider SWIG, the Simplified Wrapper and Interface Generator (available at http://www.swig.org). SWIG generates the C source code for your extension based on the library's header files, generally with some help in terms of further annotations in an interface description file. If you specifically need to wrap an existing dynamic library (a .dll on Windows, and a .so on most Unix-like systems, including Linux and Mac OS X) in order to use the library from your Python code, look into the ctypes extension (http://starship.python.net/crew/theller/ctypes/), which offers excellent support for this task. In Python 2.5, ctypes is scheduled to be included in the standard Python library.

Greg Ewing is developing a language called Pyrex, which is specifically for coding Python extensions. Pyrex (found at http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/) is an interesting mix of Python and C concepts, and is already quite usable despite being a relatively new development. Pyrex is suitable both for wrapping existing C libraries and for writing fast extension modules for Python that are compiled to machine language, like C-coded extensions would be. Pyrex works by generating intermediate C code that is then compiled. Pyrex is covered in "Pyrex" on page 650.

The weave package (available at http://www.scipy.org/site_content/weave) lets you run inline C/C++ code within Python. The blitz function, in particular, generates and runs C++ code from expressions using the Numeric package, and thus requires Numeric.

If your application runs only on Windows, the most practical way to extend and embed Python may sometimes be through COM. In particular, COM is by far the best way to use Visual Basic modules (packaged as ActiveX classes) from Python. COM is also the best way to make Python-coded functionality (packaged as COM servers) available to Visual Basic programs. The standard Python distribution for Windows (as of Python 2.4) does not directly support COM: you need to use ctypes (which is part of the standard Python library starting only with Python 2.5, but can be downloaded and installed for use with previous versions of Python too), or download and install the platform-specific win32all extension package (available at http://starship.python.net/crew/mhammond/). I do not cover Windows-specific functionality, including COM, any further in this book. For excellent coverage of platform-specific Python use on Windows, I recommend Python Programming on Win32, by Mark Hammond and Andy Robinson (O'Reilly).




Python in a Nutshell
Python in a Nutshell, Second Edition (In a Nutshell)
ISBN: 0596100469
EAN: 2147483647
Year: 2004
Pages: 192
Authors: Alex Martelli

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net