[C++-sig] How to use own C++/python bindings in embeddedinterpreter.
Pierre Bourgault
pierre.bourgault at autodesk.com
Mon Jan 22 20:15:43 CET 2007
Hi Roman,
Thanks for your reply. I know how to expose C++ classes to Python. The
problem I have is that the class registration is done in
init_module_whatever() (BOOST_PYTHON_MODULE) and I want to a python
instance of an object of this class in C++ and giving it to a script,
without having to duplicate the code.
I found a solution (which is to obtain the class object from my module's
dictionary) and I thought I should share it. At the same time, if a
better way exists, I am sure someone will point it out (I suppose that I
could also replace most of the Python/C API calls by boost::python
calls).
In myClass.hpp, I have the following C++ class:
-----------------------------------------------
class MyClass {
public:
MyClass();
void doit()
}
In whateverInit.cpp I have:
---------------------------
BOOST_PYTHON_MODULE(whatever)
{
class_<MyClass>("MyClass").def( "doit", MyClass::doit);
}
That allows me to have a python script that does this:
------------------------------------------------------
# useMyClass.py
import whatever
a = new MyClass()
a.doit()
==================================================================
Now I want to call func() defined in scriptWithFunc.py from C++,
passing it a python instance of MyClass, but created first in C++:
# scriptWithFunc.py
import whatever
def func(myobj):
myobj.doit()
SOLUTION: The C++ code that calls func():
-----------------------------------------
// callScriptFunc.cpp
//
str script = "scriptWithFunc";
str function = "func";
object moduleWithFunc = PyImport_Import(script);
object func = PyObject_GetAttrSring( moduleWithFunc, function );
if (!(func && PyCallable_Check(func))) {
return;
}
// import module "whatever"
handle<> module(PyImport_Import("whatever"));
object theModule(handle<>(borrowed(PyImport_AddModule("whatever"))));
// Obtain its dictionary and get the object representing class MyClass.
dict d = extract<dict>(dict(theModule.attr("__dict__"));
object MyClass_Class = d[ 'MyClass' ]
// Create an instance
myObj = MyClass_Class()
PyTuple_SetItem(args, myObj.ptr());
PyObject_CallObject(function, args);
-----Original Message-----
From: Roman Yakovenko [mailto:roman.yakovenko at gmail.com]
Sent: January 17, 2007 9:43 AM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] How to use own C++/python bindings in
embeddedinterpreter.
On 1/17/07, Pierre Bourgault <pierre.bourgault at autodesk.com> wrote:
> I have the following C++ class defined in myClass.h in my application:
Google is your friend :-). Py++ site (
http://www.language-binding.net/ ) uses Google Custom Search engine,
which is configured to provide better results for Boost.Python related
searches.
Second result for "construct python object" search string brings next
link http://wiki.python.org/moin/boost.python/class
BOOST_PYTHON_MODULE(example1)
{
object class_a = class_<A>("A");
object instance_a = class_a();
}
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list