|
Boost : |
From: John Hunter (jdhunter_at_[hidden])
Date: 2001-12-29 18:17:01
>>>>> "David" == David Abrahams <david.abrahams_at_[hidden]> writes:
David> Hi John, What you want to do is certainly possible. If you
David> know how to verify that a given Python object holds a
David> wrapped wxDateTime, and extract a reference to that
David> wxDateTime object, then you have everything you need. You
David> just need to declare and define the appropriate
David> from_python(PyObject, type<...>) functions
Ok, thanks, that example was very helpful. I learned enough about
SWIG to get everything working. In a nutshell, if Foo is a SWIG
wrapped object, you need
BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE
extern "C" char * SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type);
Foo from_python(PyObject* p, python::type<const Foo&>) {
Foo *pf;
if (SWIG_GetPtrObj(p, (void **) &pf, "_Foo_p")) {
PyErr_SetString(PyExc_TypeError, "Type error in Bar. Expected a Foo.");
throw python::error_already_set();
}
return *pf;
}
BOOST_PYTHON_END_CONVERSION_NAMESPACE
And then link the boost::python extension with the SWIG runtime
library -lswigpy.
As I was figuring this out, I put together some example files to
illustrate how to do this. I will include them as a tar file with
this email for anyone else needing to do this.
Thanks for your help.
John Hunter
Here is the README included in the tar file:
MOTIVATION:
The files here show how to use python extensions built with swig and
boost together. There are two simple classes, Foo and Bar. Bar
takes a Foo in it's constructor:
Bar(const Foo& f)
The goal is to take a Foo extension built with SWIG and pass it to a
Bar constructor built with boost::python. The motivation for this
is that some extensive class libraries (such as wx windows) have
python extensions built with SWIG. I would like to use those python
extensions with the extensions I build with boost::python.
In other words, I want to be able to do this:
from swigmod import Foo #Foo was exposed with SWIG
from boostmod import Bar #Bar was exposed with Boost::Python
f = Foo(3)
b = Bar(f)
print b.whats_foo_got() #should print 3
FILES:
Makefile - The flags, required libs, etc...
Foo.h - The Foo class
mixedmod.cpp - The Bar boost::python extension with
from_python for SWIG wrapped objects
swigmod.i - The Foo SWIG extension
test_swigmod.py - Test the SWIG extension
test_mixed.py - Test the SWIG and boost extensions together
USE:
Edit the Makefile to point to your SWIG Makefile.template and your
boost and python distributions.
make swig - Build the SWIG extension
make mixed - Build the boost::python extension
make clean - You know
Each of the test files output '3' if all is well.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk