Chapter 6. Wrapping the Python/C API

Table of Contents
Motivation
Theory and first steps
Helper functions in C--making Python helper macros accessable to SWIG
Additional added value--the PYTHON_CLIENT class
Using SWIG and supplemental functions to cleanly access the Python/C API from within SmallEiffel

Python is a fascinating language which is very easily extended with C--or embedded in a C application. Using the voluminous Python/C API, anything that can be done within Python can be done using C, or a language which interfaces to C. Using SWIG (and a few supplemental C helper functions) we can access all of the relevant parts of the Python/C API from within Eiffel--an important stepping stone to embedding Python.

Motivation

Python, an interpreted "scripting" language, is ideally suited for extension via the C programming language or embedding within a larger software application. Since much of the Python interpreter is written in C, it is no surprise that the libpython library offers many of its features via a C API.

The API itself seems fairly straightforward--there are functions for initializing the Python interpreter, functions for running snippets of Python code, and discrete families of functions for manipulating Python objects. The online Python documentation does cover the API in a fair amount of detail, although some elements were mysteriously omitted and required a bit of poring through the header files to uncover.

Unfortunately, while the API is voluminous and powerful, it is also (obviously) intended to work entirely with C. While this isn't a huge problem, it does mean that it needed some filtering and tweaking to work with Eiffel--and for this, as mentioned earlier, Swig with Alex Cozzi's Eiffel extensions was the workhorse necessary to make this work.