Problem using python C API

shi dingan shi.dingan at gmail.com
Fri Jan 16 05:31:02 EST 2009


Hi,
I'm trying to use the Python C API but I have
a problem when importing my new module.
Here is my (short) code:
#include "Python.h"
long sumList(PyObject *list) {
  int i, n;
  long total = 0;
  PyObject *item;

  n = PyList_Size(list);
  if (n < 0) return NULL;

  for (i = 0; i < n; i++) {
      item = PyList_GetItem(list, i);
      if (!PyInt_Check(item)) continue;
      total += PyInt_AsLong(item);
  }
  return total;
}

PyObject *wrap_sumList(PyObject *self, PyObject *args) {
  PyObject *list;
  PyObject *total;
  if (!PyArg_ParseTuple(args, "0&", &list)) {
      return NULL;
  }
  total = Py_BuildValue("l", sumList(list));
  return total;
}

static PyMethodDef exampleMethods[] = {
  {"sumList", wrap_sumList, METH_O},
  {NULL}
};

void initexample() {
  PyObject *m;
  m = Py_InitModule("example", exampleMethods);
}

My compilations lines:
gcc -Wall -fpic -c  -I/usr/include/python2.4 -I/usr/lib/python2.4/config
mytest.c
gcc -shared mytest.o -o examplemodule.so

When I try to import examplemodule, I obtain the following message:
>>> import examplemodule
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function
(initexamplemodule)

Someone has an idea ?
Thx,
Marc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090116/e04902f5/attachment.html>


More information about the Python-list mailing list