Extending Python and Zope in C
By Michael Roberts2004-12-04
Extending Python for Fun and Profit
To extend Zope, you first extend Python. While extending Python is not brain surgery, it's no walk in the park either. There are two basic components to a Python extension. The first is obviously the C code. I'll cover that in a minute. The other component is the Setup file. The Setup file describes the module by supplying its module name, the location of its C code, and any compiler flags you may need. This file is preprocessed to create a makefile (on UNIX) or MSVC++ project files (on Windows). Before you ask -- Python on Windows is indeed built using the Microsoft compilers. The folks at Python.org recommend using MSVC++ to build extensions as well. It stands to reason that you should be able to persuade GNU compilers to do the trick, but I haven't tried that myself.
At any rate, let's define a little module called 'foo'. The 'foo' module will have a function called 'bar'. When we get things running, we will be able to import this function into a Python script using import foo;, just like any module. The Setup file is very simple:
# You can include comment lines. The *shared* directive indicatesWhat is Zope?
# that the following module(s) are to be compiled and linked for
# dynamic loading as opposed to static: .so on Unix, .dll on Windows.
*shared*
# Then you can use the variables later using the $(variable) syntax
# that 'make' uses. This next line defines our module and tells
# Python where its source code is.
foo foomain.c
Zope stands for "Z Object Publishing Environment", and it's an application server implemented in Python. "Great," you say, "but what exactly is an application server?" An application server is simply a long-running process that provides services for active content. The Web server then makes calls to the application server in order to have pages built at runtime.
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
