Original Post
I've tried to get an answer to this in two other location (comp.lang.python and the SWIG Users List), but I've been pretty much ignored. I hope there are people here who can handle it. I've exported a C++ class into the Python environment using SWIG. Instances of this C++ class are used by the application and, via SWIG, now there are also instances within Python. I need to (preferably elegantly) exchange the data contained in the C++ and Python instances bidirectionally. In other words, I can exchange integral types between C++ and Python using the Python conversion functions (e.g., PyLong_AsLong() and PyLong_FromLong()). How then do I do something similar for my user-defined C++ class instances? There is the brute-force approach of writing long lines of C++ conversion code for the class, reading individual attributes from each and cross-pollinating to create opposing instances. This is the worst-case approach, because it requires constant maintenance of the code to keep up with any changes to the class. Not an option. An approach that I'm considering is adding helper functions within the exported SWIG code that will convert the PyObject of my class instance into a pointer to a C++ instance, and return that for class copy operations (i.e., *python_instance = cpp_instance, or vice-versa). I've not yet tried this approach to see if it would actually work, and I am aware of the need to keep memory allocations localized to the correct heaps. Is there some SWIG- or Python-specific approach I'm missing for elegantly exchanging user-defined data types between C++ and Python? I'm sure I could modify the Python interpreter to add my own conversion functions, but that means then maintaining versions of Python along with my application, which just adds more overhead. Thanks for any help.