Extending Python and Zope in C
By Michael Roberts2004-12-04
Building the Extension
All that remains now is building the module. There are two ways you can do this. The first is to follow the instructions in the documentation, and run make -f Makefile.pre.in boot, which builds a Makefile using your Setup. Then you use that to build your project. This route works only on UNIX. For Windows, there is a script called "compile.py" (see Resources later in this article). The original script is hard to find; I found a highly modified copy in a mailing list posting from Robin Dunn (the man behind wxPython). This script is supposed to work on UNIX and Windows; on Windows it builds MSVC++ project files starting from your Setup.
To run the build, you'll need to have includes and libraries available. The standard Zope installation of Python doesn't contain these files, so you'll need to install the regular Python installation from www.python.org (see Resources). On Windows, you'll also have to get the config.h file from the PC directory of the source installation; it's a manual version of the config.h that the UNIX installation builds for you. On UNIX, therefore, you should already have it.
Once all this is complete, the result is a file with the extension ".pyd". Put this file into your Python installation's "lib" directory (under Zope, Python lives in the "bin" directory, so you want your extension to end up in the "bin/lib" directory, oddly.) Then you can call it, just like any native Python module!
>>> import foo;My first question when I got this far was to ask myself how I would define a class in C that would be visible from Python. It turns out I was probably asking the wrong question. From examples I've studied, anything that Python-specific is simply done in Python and calls the C functions you export from your extension.
>>> foo.bar ("This is a test");
14
Tutorial Pages:
» The Best of Both Worlds
» Extending Python for Fun and Profit
» Writing the Code
» Building the Extension
» Taking it to Zope
» Creating an External Method
» Zope scripts: Cliff Notes version
» Going all out With a Product
» Where to go From Here
» Resources
First published by IBM DeveloperWorks
