I l@ve RuBoard |
16.1 IntroductionCredit: David Beazley, University of Chicago One of Python's most powerful features is its ability to hook to libraries and programs written in compiled languages, such as C, C++, and Fortran. In fact, a large number of Python's built-in library modules are written as extension modules in C, so that operating-system services, networking functions, databases, and other features can be easily accessed from the interpreter. In addition, a number of application programmers are writing extensions, which can use Python as a framework for controlling large software packages written in compiled languages. The gory details of how Python interfaces with other languages can be found in various Python programming books and at http://www.python.org. However, the general approach revolves around the creation of special wrapper functions that hook into the interpreter. For example, say you have a C function such as this: int gcd(int x, int y) { int g = y; while (x > 0) { g = x; x = y % x; y = g; } return g; } If you want to access it from Python in a spam module, you'd have to write a special wrapper code like this: #include "Python.h" extern int gcd(int, int); PyObject *wrap_gcd(PyObject *self, PyObject *args) { int x,y,g; if(!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL; g = gcd(x, y); return Py_BuildValue("i", g); } /* List of all functions in the module */ static PyMethodDef spammethods[] = { {"gcd", wrap_gcd, METH_VARARGS }, { NULL, NULL } }; /* Module initialization function */ void initspam(void) { Py_InitModule("spam", spammethods); } Once this code is compiled into an extension module, the gcd function is used as you would expect. For example: >>> import spam >>> spam.gcd(63,56) 7 >>> spam.gcd(71,89) 1 This short example extends in a natural way to larger programming libraries�each function that you want to access from Python simply gets its own wrapper. Although writing simple extension functions is fairly straightforward, the process of writing wrappers quickly becomes tedious and prone to error if you are building anything of reasonable complexity. Therefore, a lot programmers rely on automatic module-building tools to simplify the process. Python is fortunate to have a variety of such tools:
Regardless of the approach used to build Python extension modules, certain topics remain somewhat mysterious to many extension programmers. Therefore, the recipes in this chapter describe some of the common problems and extension-building tricks that are rarely covered in the standard documentation or other Python books. Topics include interacting with threads, returning NULL values, defining classes from C, implementing C/C++ functions in Python, creating extension types, and debugging. |
I l@ve RuBoard |