Skip to main content

Getting to know PyXPCOM

Installation and setup

Uche Ogbuji (uche@ogbuji.net), Principal Consultant, Fourthought, Inc.
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 uche@ogbuji.net.

Summary:  Cross-platform component object model (XPCOM) is the component system developed in the Mozilla project. ActiveState has developed an open-source Python library for XPCOM. This three-part series provides a developer's introduction to XPCOM programming in Python. This first part covers installation of PyXPCOM.

Date:  01 May 2004
Level:  Introductory
Activity:  2433 views

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.

Getting started

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.


Building with full Mozilla

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

to ~/.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 mozilla/extensions/python/xpcom.


Building XPCOM stand-alone

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.


Building PyXPCOM

Set up your environment with the following entries:

  • MOZILLA_FIVE_HOME set 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_PATH should have the $MOZILLA_FIVE_HOME value as one of its components.
  • * PYTHONPATH should 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

end

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.


Next stop: PyXPCOM as client

In the next article in this series, I'll show you how to use PyXPCOM as a client to established XPCOM objects.


Resources

About the author

Uche Ogbuji

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 uche@ogbuji.net.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=86944
ArticleTitle=Getting to know PyXPCOM
publish-date=05012004
author1-email=uche@ogbuji.net
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers