Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo


#36 typemap support for python buffer objects

GL (10)
J Robert Ray

This is a patch for the void* typemap that adds support
for using python buffer objects.

It allows passing a pointer wrapped in python with
PyBuffer_FromMemory() to glTexImage2D et. al.

The patch also makes the typemap more strict in that it
will only take a buffer- or string-like object.

The old code would construct a temporary string
PyObject and return a pointer to the internal storage
of that PyObject.

But because it use Py_DECREF() immediately on the
temporary string object, the storage may be immediately
freed by python.

In the common case of passing a string object in, the
new temporary object would share the storage of the
string object and the memory would not be freed.

But some other object may be passed in that causes the
temporary string object to allocate its own copy (e.g.
an instance with __str__ method), the old typemap would
in this case end up with an invalid pointer.

The patch uses PyString_Check() to ensure that the
object passed in is a string-like object, and avoids
using a temporary PyObject.

Note: it could be possible to arrange for the old code
to work correctly by removing the Py_DECREF and using
SWIG's 'freearg' typemap functionality to clean up the
temporary PyObject.

This patch is against I have visually
compared the same file in and the typemap is
unchanged so this patch should apply there too.


  • J Robert Ray
    J Robert Ray

    typemap buffer object patch