Call C functions from Python

"Martin v. Löwis" martin at v.loewis.de
Tue Oct 4 17:13:39 EDT 2005


Java and Swing wrote:
> Is there some other way, besides SWIG, which will allow me to call
> functions inside an Ansi C DLL?

You could write an extension module. See Modules/xxmodule.c in
the Python source tree, as well as

http://docs.python.org/ext/ext.html

In the specific case, if it weren't for the digits argument,
the wrapper function would read

static PyObject*
Py_DoSomeStuff(PyObject*unused, PyObject* args)
{
   char *input;
   if (!PyArg_ParseTuple("s:DoSomeStuff", &input))
     return NULL;
   return PyString_FromString(DoSomeStuff(input));
}

I cannot extend this to digits, as I don't know how
the digits are represented if there is more than one
(i.e. how does DoSomeStuff know how many digits are
being passed?)

If DoSomeStuff returns NULL on errors, additional
exception throwing is necessary. If DoSomeStuff returns
memory that the caller needs to release, you need to
do so before returning from Py_DoSomeStuff.

Regards,
Martin



More information about the Python-list mailing list