Some GNOME developers have gathered in Boston for for a Python GNOME hackfest that is hosted by the One Laptop Per Child (OLPC) project. The primary goals behind the hackfest include establishing a strategy for delivering Python 3.0 compatibility for the GNOME platform and advancing the Python GObject introspection project.

The Gtk+ toolkit, which provides the underlying widget system of the GNOME desktop environment, is an important part of the desktop Linux ecosystem. Although the toolkit itself is built with C, it can also be used with other programming languages—including Python, Ruby, JavaScript, C#, Java, and Scheme. Python is widely used by Gtk+ application developers and is important to the GNOME community. OLPC, the host of the hackfest, uses Python in conjunction with Gtk+ for its Sugar environment—the core user experience and activity collection that is shipped on its XO laptops.

How the PyGtk language bindings work

The ability to develop Gtk+ applications with alternate programming languages is facilitated by bindings—frameworks that expose the toolkit and map its functionality to the object model and programming idioms of the target language so that it can be used seamlessly like any other programming library. Gtk+ bindings are generally created and maintained individually for each programming language. The same is true of other libraries that are part of the GNOME platform in addition to Gtk+ itself. For example, there are bindings for Pango, WebKit, GConf, Cairo, Clutter, GIO, and others.

These bindings are a very important part of the GNOME platform. It's useful to have support for high-level languages that are conducive to rapid development because they lower the barriers to entry by enabling third-party developers to build software for GNOME without having to use C. The downside, however, is that maintaining the bindings can be somewhat complicated and requires a lot of work.

The existing Python bindings for Gtk+ and other GNOME libraries that use the GLib object model (GObject) largely consist of object and method definitions that are written in a lisp-like syntax. These are partly generated from the C header files of the underlying library. In addition to definition files, the Python bindings also have "override" files that include a lot of glue code to facilitate a clean wrapping. A code generator tool processes the definition and override files in order to build the actual Python extension module.

Although some parts of the process are easy to automate, there is still quite a bit of dirty work that has to be done by hand. When you consider the fact that this work has to be repeated using different mechanisms for each programming language, you can see that offering broad programming language support for GNOME libraries is a pretty burdensome undertaking.

Building better Python bindings with GObject introspection

Fortunately, there is a compelling project that simplifies the development of bindings for GObject-based libraries and reduces the amount of maintenance that is required to keep the bindings in working order. The underlying technology is called GObject introspection. Instead of maintaining separate sets of type definitions with different mechanisms for each programming language, GObject introspection offers a more consistent and standardized approach.

Annotations and type info are extracted from the C code and used to generate XML files that describe the structure of the library. These XML files serve as a centralized repository of type metadata that can be used by all of the language bindings. The annotations in the C code are written in comments using a standard format that is both human-readable and easily parsed by the GObject introspection tools. For a high-level visual overview of how GObject introspection works, you can refer to the diagram on the GNOME Live wiki.

The GObject introspection project has been around for a few years and has achieved a level of maturity that makes it broadly suitable for adoption by language bindings. It is already used successfully in that capacity by Vala (a programming language modeled after C# with a compiler that statically generates C code) and Gjs (a SpiderMonkey-based JavaScript runtime for desktop application development). As the PyGtk bindings are about to get a major overhaul to deliver compatibility with Python 3.0, the developers are taking the opportunity to also begin migrating the bindings to use GObject introspection. Both of those efforts are getting a big boost at the GNOME Python hackfest.

Moving forward with a hackfest

Developer John Palmieri concisely described the plan for the transition in a blog entry that he wrote about the first day of the event. He says that the Python GObject introspection (PyGI) framework will be developed alongside the existing conventional PyGtk bindings. PyGI will initially only be used to wrap the non-overridden functions, but will eventually replace PyGtk entirely. Colin Walters elaborated on how the hybrid model will work in his own blog entry about the hackfest.

"In the combined architecture, the API people are used to from PyGTK is preserved, however we begin to 'hollow out' the core so that for more of the simple functions that aren't overridden, instead of generating a static C blob for them, we look up dynamically through PyGI," he wrote.

Ensuring minimal disruption to the existing APIs during the transition is obviously a high priority. It's important to avoid breaking the large number of third-party applications that currently depend on the PyGtk bindings. Walters compares the transition to "performing engine maintenance while the car is running."

One of the most significant breakthroughs to emerge from the hackfest is a working implementation of callback support for PyGI. The original callback implementation patch was written last year by Zach Goldberg and was reviewed by PyGI developer Simon van der Linen. Goldberg and Walters substantially reworked it during the hackfest and got it merged on Saturday. The developers also appear to have made good progress on the Python 3.0 support.

The hackfest is clearly delivering tangible improvements to the state of Python support on the GNOME platform. As they mature, the PyGI-based bindings will make it easier to take advantage of GNOME libraries from Python and will also reduce the amount of work that is needed to maintain the bindings as new features are added to the toolkit.

Listing image by Ian C.