Extending Python with C API

Thierry Masson massonmousson at gmail.com
Fri Sep 14 01:11:10 EDT 2007


On 9/13/07, Carsten Haese wrote:
>Your module C code uses an unknown function by the name of PyBuildValue.
>The actual name of the function you mean is Py_BuildValue.

Thank you so much, Carsten. I can't believe I missed that underscore! I'm
posting the working code below in case it ever turns out to be useful for
someone else who is new to the Python C API. (I also added the init
function, which I forgot to include in my original post). I've verified that
this code works on Red Hat Enterprise Linux ES release 3 (Taroon Update 8).

Code for writing a C API Python Extension (cobbled together from so many
online and printed sources that it's hard to know where to start with the
acknowledgements):

File gtestmodule.c:
==============
#include "Python.h"

/* First, a couple of C functions that do the real work */
/* In this test example, "real work" is perhaps exaggerating a little */
int GetInt(int iVal)
{
    // Double the supplied value and pass it back
    return iVal * 2;
}

char* GetString(void)
{
    return "This is the message";
}

/* Now a couple of corresponding wrapper functions so that Python can get
access to them */

/* Python requires this return data type and these arguments */
PyObject* gtestmodule_GetInt(PyObject* pSelf, PyObject* pArgs)
{
    int x = 0, iRet = 0;

    /* Validate and assign the single integer argument (that's what GetInt()
expects) */
    /* The "i" means just one integer */
    /* Two integers would be "ii"; an integer, a string, a float and a long
would be "isdl" */
    if (!PyArg_ParseTuple(pArgs, "i", &x))
        return NULL;

    iRet = GetInt(x);
    return Py_BuildValue("i", iRet);
}

PyObject* gtestmodule_GetString(PyObject* pSelf, PyObject* pArgs)
{
    char* szMsg = GetString();
    return Py_BuildValue("s", szMsg);
}

/* Method table for mapping function names to wrapper functions */
static PyMethodDef gtestmoduleMethods[] =
{
    {"GetInt", gtestmodule_GetInt, METH_VARARGS, "Description goes here"},
    {"GetString", gtestmodule_GetString, METH_VARARGS, "Description goes
here"},
    {NULL, NULL}
};

/* Module initialization function */
initgtestmodule(void)
{
    Py_InitModule("gtestmodule", gtestmoduleMethods);
}

File setup.py:
=========
# To build and install the module, use: python setup.py install
# Running this will generate a library file (gtestmodule.so on Linux)
#
from distutils.core import setup, Extension
setup(name = "gtestmodule",
      version = "1.0",
      maintainer = "your name here",
      maintainer_email = "you at yourdomain.com",
      description = "test module",
      ext_modules = [Extension("gtestmodule",[" gtestmodule.c"])],
)

File test.py:
========
# Simple test routine to run after building the library file
import gtestmodule
print gtestmodule.GetInt(36);
print gtestmodule.GetString();
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070913/8eee6062/attachment.html>


More information about the Python-list mailing list