<span class="q">On 9/13/07, <b class="gmail_sendername">Carsten Haese</b> wrote:<br>>Your module C code uses an unknown function by the name of PyBuildValue.<br>>The actual name of the function you mean is Py_BuildValue.
<br><br></span>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).<br><br>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):<br><br>File gtestmodule.c:<br>==============
<br>#include "Python.h"<br><br>/* First, a couple of C functions that do the real work */<br>/* In this test example, "real work" is perhaps exaggerating a little */<span class="q"><br>int GetInt(int iVal)
<br>{<br>    // Double the supplied value and pass it back    <br>    return iVal * 2;<br>}<br><br>char* GetString(void)<br>{<br>    return "This is the message";<br>}<br><br></span>/* Now a couple of corresponding wrapper functions so that Python can get access to them */
<br><br>/* Python requires this return data type and these arguments */
<span class="q"><br>PyObject* gtestmodule_GetInt(PyObject* pSelf, PyObject* pArgs)<br>{<br></span><span class="q">    int x = 0, iRet = 0;<br><br></span>    /* Validate and assign the single integer argument (that's what GetInt() expects) */
<br>    /* The "i" means just one integer */
<br>    /* Two integers would be "ii"; an integer, a string, a float and a long would be "isdl" */<span class="q"><br>    if (!PyArg_ParseTuple(pArgs, "i", &x))<br>        return NULL;<br>
<br>    iRet = GetInt(x);<br>    return Py_BuildValue("i", iRet);<br></span><span class="q">}<br><br>PyObject* gtestmodule_GetString(PyObject* pSelf, PyObject* pArgs)<br>{<br>    char* szMsg = GetString();<br></span>
    return Py_BuildValue("s", szMsg);<br>}<br><br>
/* Method table for mapping function names to wrapper functions */<span class="q"><br>static PyMethodDef gtestmoduleMethods[] = <br>{<br>    {"GetInt", gtestmodule_GetInt, METH_VARARGS, "Description goes here"},
<br>    {"GetString", gtestmodule_GetString, METH_VARARGS, "Description goes here"},
<br>    {NULL, NULL}<br>};<br><br></span>/* Module initialization function */<br>initgtestmodule(void)<br>{<br>    Py_InitModule("gtestmodule", gtestmoduleMethods);<br>}<br><br>File setup.py:<br>=========<br># To build and install the module, use: python 
setup.py install<br># Running this will generate a library file (gtestmodule.so on Linux)<span class="q"><br>#<br>from distutils.core import setup, Extension<br>setup(name = "gtestmodule",<br>      version = "
1.0",<br></span>
      maintainer = "your name here",<br>      maintainer_email = "<a href="mailto:you@yourdomain.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">you@yourdomain.com</a>",<span class="q">
<br>      description = "test module",<br>      ext_modules = [Extension("gtestmodule",["
gtestmodule.c"])],<br>)<br><br></span>File test.py:<br>========<br># Simple test routine to run after building the library file<span class="q" id="q_11501ec1f57b6ace_16"><br>import gtestmodule<br>print gtestmodule.GetInt
(36);<br>print gtestmodule.GetString();<br><br><br></span>