[C++-sig] BPL and embedding python

David Abrahams david.abrahams at rcn.com
Wed Jun 12 00:49:17 CEST 2002


From: "Dave Hawkes" <daveh at cadlink.com>


> I've noticed that more and more people are attempting to embed python in
> applications and documentation  on how to do this effectively is quite
> scarce. One of the strengths of boost is the ability to create a c++
class
> instance and override some of its members in python, but use the actual
> class instance from within c++. In fact you can create a class instance,
> modify some of its members using on the fly generated python code and
then
> pass the c++ instance back to yor c++ code.
>
> I've done this with with boost V1 along these lines:

Thanks for your post. Just to clarify, what you're demonstrating below is
how to create a Python instance of your wrapped C++ class from C++.


<snip>

> Remember this is NOT complete code and just an outline of how to proceed.
>
> The fly in the ointment is not being able to directly delete the class
> instances (Dave, do you have any ideas ?)

Hmm. Unfortunately, no. The class instance itself is a data member of
another class; that would lead to double-destruction. Why do you want to
directly delete the class instance?

I've been thinking about a smart pointer type which manages a python object
but "points to" some internally-held C++ object for v2, so you could write:

    lvalue_ptr<MyClass> p(call<PyObject*>(pPyClass));

and then,

    p->some_member_function();

Would that help?

Hmm, lvalue_ptr<> would probably replace back_reference<> in the v2
interface...

-Dave






More information about the Cplusplus-sig mailing list