Python extension, where am I going wrong

John Vines (CISD/HPCD) jvines at arl.army.mil
Wed Sep 10 15:17:00 CEST 2008


All,
    I am trying to create an extension module and keep getting an error, 
"/usr/local/lib/python2.5/site-packages/mytest.so: undefined symbol: 
PyInitModule"

thanks in advance,

*Here is my source code and setup.py information:

*/* mytest.c */
#include <Python.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

static PyObject *ErrorObject;

static char isEven__doc__[] = "Method to determine if number is odd or 
even\n";

static PyObject *isEven(PyObject *self, PyObject *args){
    int inputValue;
    int returnValue;
   
    if (!PyArg_ParseTuple(args, "i", &inputValue)){
    PyErr_SetString(PyExc_ValueError, "Argument parsing Error");
    return NULL;
    }
   
    if ( 0 == inputValue%2 ){
    returnValue = 1;
    } else {
    returnValue = 0;
    }
    return Py_BuildValue("i", returnValue);
}

static char getFactorial__doc__[] = "This module takes a number as a 
parameter \
    and returns the factorial of that number\n";

static PyObject *getFactorial(PyObject *self, PyObject *args){
    int inputValue;
    int resultValue;
   
    if (!PyArg_ParseTuple(args, "i", &inputValue)){
    PyErr_SetString(PyExc_ValueError, "Argument parsing error");
    return NULL;
    }
   
    resultValue = factorialHelper(inputValue);
    return PyInt_FromLong(resultValue);
}

int factorialHelper(int factor){
    if (factor <= 0){
    return 0;
    }
    if (factor == 1){
    return 1;
    }
    return factor*factorialHelper(factor-1);
}

static struct PyMethodDef mytest_methods[] = {
    {"isEven", isEven, METH_VARARGS, "Determine odd/even of a number"},
    {"getFactorial", getFactorial, METH_VARARGS, "Calc the factorial 
value of a number"},
    {NULL, NULL, 0, NULL}
};

void initmytest(){
    PyObject *m, *d;
   
    m = PyInitModule("mytest", mytest_methods);
    d = PyModule_GetDict(m);
   
    ErrorObject = PyBuildValue("s", "mytest module error");
    PyDict_SetItemString(d, "Error", ErrorObject);
   
    if (PyErr_Occurred())
    Py_FatalError("Can't initialize module mytest!");
}

*My setup.py code:*
#!/usr/bin/env python
from distutils.core import setup, Extension

setup(name="mytest", version="1.0",
      ext_modules=[Extension("mytest", ["mytest.c"])])



More information about the Python-list mailing list