Wrapping c functions

David M. Cooke cookedm+news at physics.mcmaster.ca
Sun May 1 17:27:28 EDT 2005


Andrew Dalke <dalke at dalkescientific.com> writes:

> Glenn Pierce wrote:
>>         if (!PyArg_ParseTuple(args, "isi", &format, filename, &flags))
>>             return NULL;
>
> Shouldn't that be &filename ?  See
>   http://docs.python.org/ext/parseTuple.html
> for examples.
>
>
>>         dib = FreeImage_Load(format, filename, flags);
>
>> Also I have little Idea what to return from the function. FIBITMAP * is
>> an opaque pointer
>> that I pass to other FreeImage functions, I pretty certain
>> Py_BuildValue("o", dib) is wrong.
>
> If it's truly opaque and you trust your use of the code you can
> cast it to an integer, use the integer in the Python code, and
> at the Python/C interface cast the integer back to a pointer.
> Of course if it no longer exists you'll get a segfault.
>
> If you want more type safety you can use the SWIG approach and
> encode the pointers as a string, with type information and
> pointer included.

Better yet, use a CObject. That way, a destructor can be added so as
to not leak memory. Type info could be included in the desc field.

return PyCObject_FromVoidPtr(dib, NULL)

(the NULL can be replaced with a routine that will free the image.)

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca



More information about the Python-list mailing list