c bindings

Thomas Wouters thomas at xs4all.net
Sun Jul 8 19:37:27 EDT 2001


On Fri, Jul 06, 2001 at 01:25:21PM -0700, Sebastien Loisel wrote:


> I am writing an extension module. For this purpose, I have written a very
> short code generator, to create wrappers around my C library. For some
> simple test cases, it seems to work fine, but I am encountering core dumps
> with a more complete program.

Your best bet is really to examine the corefile in gdb, it can often point
you directly to the problem. Changing the 'gtc_must_malloc's in your example
to just plain 'malloc', your code works, and you don't give any clues about
how to break it. Still, here are some hints.

> static PyObject *example_py(PyObject *self, PyObject *args)
> {
>   int j;
>   PyObject *_;

Bad style... Don't use _* names unless you know what you're doing, it might
clash with something platformish, 'hidden'.

>   if(!PyArg_ParseTuple(args,"O", &param_helper)) return 0;

Bad style... Your function returns a pointer, you should return 'NULL', not
0. Unlikely to be the cause of the problem, though.

>   ret_size=param_size;
>   ret=gtc_must_malloc(sizeof(float)*ret_size);
>   for(j=0;j<ret_size;j++) ret[j]=param[j]+j/2.0;
>   if(ret_size<0) abort();

Too late for this check. If reg_size is smaller than 0, the malloc() would
already have failed and you die in the for loop. What's more, you didn't
check the return value of the malloc. And cramming the for statement and
body all on one line is, well, bad style :-)

>   ret_list=PyList_New(ret_size);
>   for(j=0;j<ret_size;j++) {
>     PyList_SetItem(ret_list,j,PyFloat_FromDouble(ret[j]));

Note: you can use PyList_SETITEM here, it works the same but is faster, and
you already know the list is a real list and is long enough to hold the
result.

>   free(ret);
>   return ret_list;
> }
> static PyMethodDef example_methods[] = {
>   {"example",example_py,1},

Better use the define 'METH_VARARS' than a literal 1 here... not just better
style, but more guaranteed to work later :)

>   {0,0} // sentinel

And these should be NULL's.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list