[wxPython] Large blocks of data

I'm currently working on a project involving lots of image processing.
Most of the graphics intensive work is done in C++ using the boost::python
wrapping mechanism, while all of the GUI is written in wxPython.

When it comes to displaying the image, I currently return it to Python as
a string. The Python code then passes that string to wxImage::SetData and
the wxImage is later displayed.

This works fine, except the string, which is often very large, must be
copied from C++ into a Python string. I would like to avoid this copy. I
have tried allocating a Python string from within the C++ and filling that
with the data and then returning it. However, it seems that the data is
deallocated almost immediately, no matter how many times I call Py_INCREF
on it.

The second approach I would like to try is to create a wxImage directly in
C++ and return that. However, since I'm not using SWIG, I'm not entirely
sure how to do this so that the returned object will look to Python just
like a _Python_ wxImage. boost::python does allow returning unmodified
'PyObject *'s so this should be theoretically possible, right?

Any assistance would be greatly appreciated.

···

--
Michael Droettboom
mdboom@peabody.jhu.edu
410.625.7596

Computer Music Research
Peabody Conservatory of Music
Johns Hopkins University

The second approach I would like to try is to create a wxImage directly in
C++ and return that. However, since I'm not using SWIG, I'm not entirely
sure how to do this so that the returned object will look to Python just
like a _Python_ wxImage. boost::python does allow returning unmodified
'PyObject *'s so this should be theoretically possible, right?

This is probably the best approach. Take a look in the wxPython sources at
wxPython/src/export.h and helpers.h. When you include export.h then it will
give you a function to call to import an API structure from the core
wxPython module into your code, and a bunch of macros for calling those
APIs. You will use something like this:

    wxPyCoreAPI_IMPORT(); // probably in your module's init function

    ...

    myPyObject = wxPyConstructObject((void*)myImage, "wxImage", 1);

where the last parameter specifies whether the C++ object should be deleted
when the Python object is.

HTH,

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!