Last year, ActiveState, perennial home of goodies for scripting languages, announced a Python interface to XPCOM as part of the Komodo project. (Komodo is a cross-platform developer's environment based on Mozilla). PyXPCOM was initially developed by Trent Mick, and has benefited by the additional efforts of Mark Hammond, who is also the main force behind the Python binding for COM. PyXPCOM allows the developer to access XPCOM objects from Python code and implement XPCOM objects in Python code.
This article assumes knowledge of both XPCOM and Python. (See the Resources section for links to articles and other resources that provide the needed background.) In particular, don't miss Rick Parrish's excellent series on XPCOM in which he covers C++. But even if you're not familiar with that language, there is much there to learn about XPCOM after skipping the C++ code.
Also, I developed the sample code on Linux, but since Python and XPCOM are cross-platform, you should be largely able to emulate my instructions on Windows and other platforms.
As of the time of writing, the official PyXPCOM page listed version 0.91 as the latest, but there have been many important fixes and updates since then. Luckily, PyXPCOM has since been incorporated into Mozilla CVS and the updates are maintained there (see Resources). In this article, I'll present two approaches to building PyXPCOM: using the full Mozilla build or just standalone XPCOM. In both cases I work with the CVS versions of Mozilla. This might seem to be living a bit too close to the edge of the cliff, but if you are playing with XPCOM (whether from Python or otherwise), it is quite wise to hone your Mozilla development skills anyway.
I built XPCOM on a Pentium II 366 laptop with 192MB RAM running heavily updated Red Hat 7.0 Linux.
Note that if you're using Red Hat Linux 7.0, you'll probably have to upgrade your GCC in order to get this to compile. I had mysterious failures on perfectly legitimate code running gcc-2.96-69 and I had to upgrade to gcc-2.96-84 to get a working compile. Some people even recommend "upgrading" to gcc 2.95.3, which was released after 2.96 and represents a more stable branch than Red Hat's. See Mozilla's Unix build instructions for more information. The relevant package versions on my system are:
[uogbuji@borgia uogbuji]$ rpm -qa | grep "gcc\|cpp\|glibc" glibc-2.2-12 kgcc-1.1.2-40 gcc-c++-2.96-79 glibc-devel-2.2-12 gcc-g77-2.96-79 cpp-2.96-79 gcc-2.96-79 glibc-profile-2.2-12 gcc-objc-2.96-79
Next, follow the instruction below in either the full Mozilla section or the stand-alone XPCOM section as you prefer.
The most important preparatory step is setting up so that the Mozilla configuration will use the
--disable-short-wchar option. Otherwise you will probably have linker errors when building PyXPCOM. To do so, make sure you have (or add) a line such as:
\ ac_add_options --disable-short-wchar
~/.mozconfig in your home directory. Note that you can create a
~/.mozconfig file with all the options you need by using Mozilla's neat online Unix configurator utility. Otherwise you can just manually create a file with the above line as the only contents. If you are short on disk space you might also want to use the "--disable-debug" option.
Next you will want to get Mozilla built and set up. Rick's article has good instructions for this, and the Mozilla platform-specific build pages are essential, but I'll detail the steps that I used.
These commands retrieve Mozilla from the sidefile, including the latest PyXPCOM code, from CVS. They also build Mozilla -- but not PyXPCOM, which we'll build separately.
Now you could go and read the complete short stories of Kafka, or find some other pleasant way to wait out the hours that the download/compilation takes to complete. Once it's done, PyXPCOM lives in
This is a quicker and more lightweight option. However, later in this series I'll show how to invoke XPCOM objects implemented in Python from Mozilla, so if you choose this route I recommend obtaining Mozilla through other easy means. Installing a binary distribution such as RPM is fine: just be careful not to mix up your full Mozilla install with the stand-alone XPCOM build.
Obtaining and building stand-alone XPCOM is quite straightforward, as shown in this example in the sidefile.
Set up your environment with the following entries:
MOZILLA_FIVE_HOMEset to the location of your Mozilla or XPCOM binaries. For example,
~/src/mozilla/dist/bin.Note that some of the Mozilla docs erroneously tell you to set this to the directory above the binary directory (dist), it will cause you problems.
LD_LIBRARY_PATHshould have the
$MOZILLA_FIVE_HOMEvalue as one of its components.
* PYTHONPATHshould include the location where you will be installing PyXPCOM.
[uogbuji@borgia mozilla]$ cd extensions/python/xpcom
Now copy the file
makefile.stupid.linux to a backup file, say
makefile.stupid.linux.orig, open it in an editor, and modify the variables at the top to match your system's settings (for example, location of Python). For instance, I run Python 2.1, built from Sean Reifschneider's SRPMs, and a relevant excerpt from the top of my makefile.
Note that the distributed makefile is also hard coded for Python 2.0 in a couple of places. If you use Python 2.1, you'll want to change the following lines:
LOADER_LIBS=-lpython2.0 -ldl -lpthread -lutil -shared -lxpcom
ENGINE_LIBS=-lpython2.0 -ldl -lpthread -lutil -lxpcom \
Now you can build PyXPCOM:
[uogbuji@borgia mozilla]$ cd extensions/python/xpcom [uogbuji@borgia xpcom]$ make -f makefile.stupid.linux DEBUG=1 [uogbuji@borgia xpcom]$ make -f makefile.stupid.linux DEBUG=1 install
of course the "DEBUG=1" setting is optional.
The package also comes with a regression test, which you can invoke by:
[uogbuji@borgia xpcom]$ make -f makefile.stupid.linux test
However, I've never been able to get it all the way through the test suite without error, yet things work fine for me, so I wouldn't be terribly concerned at errors from the above step. Certainly, try poking at things from Python to see if they fall over. One useful way is to just try a few imports from the command line. If you get no errors (and no segmentation fault) then most likely you're all set.
In the next article in this series, I'll show you how to use PyXPCOM as a client to established XPCOM objects.
- See Rick Parrish's series on XPCOM, beginning with XPCOM Part 1: An introduction to XPCOM.
- View the PyXPCOM home page.
- See The Mozilla home and CVS access information. If you're working on Unix see the Mozilla Unix build instructions for any tips and warnings.
Uche Ogbuji is a consultant and co-founder of Fourthought, Inc., a software vendor and consultancy specializing in XML solutions for enterprise knowledge management. Fourthought has developed 4Suite, an open source platform for XML, RDF, and knowledge-management applications. Uche is a computer engineer and writer born in Nigeria; he lives and works in Boulder, Colorado. You can contact Uche at email@example.com.