[C++-sig] Passing a reference to my object to python

Lawrence Spector Lawrence.Spector at CanfieldSci.com
Thu Jul 5 18:24:12 CEST 2007


Sorry for the slow response to this.  July 4th and all.  Hope everyone had a good fourth of July.  Anyway, I guess I'm stuck and I was wondering if someone might be able to supply or direct me to some working sample code.  I'm sure I'm missing an important concept.

As for providing more information, for one thing, the error was a boost::python::error_already_set.  What I want to do is something like this:

MyFile.cpp:

...
CppClass cppClass;

// somehow pass cppClass to Python
// Something like: boost::python::object pyClass(cppClass);

And from the python side (once pyClass is set):

pyClass.callMethod();

What I'm having, when it tries to callMethod() is the exception occurs and seems to be a type exception.

All works just fine if I create the object in Python first, e.g.:

pyClass = CppClass()

So, the problem is in how I'm trying to pass it to Python.

Also, I appreciate your suggestions.  I'm trying them now to see if they solve my issue, but I think my issue is probably of a more basic level.

One other thing is I'm using bjam to build the python extensions, but the other exes and libraries are done strictly in Visual Studio 2005.

Thanks in advance,

Lawrence

From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Roman Yakovenko
Sent: Tuesday, July 03, 2007 2:54 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Passing a reference to my object to python

On 7/3/07, Lawrence Spector <Lawrence.Spector at canfieldsci.com<mailto:Lawrence.Spector at canfieldsci.com>> wrote:
> So far, what I've done is build a Boost.Python extension for my python
> script to use.  It exposes one class for the script to use, let's say called
> CppClass.  This seems to work okay.  I'm using Boost.Python and the Python
> API to interpret the script from C++ code.  During this process, I want to
> pass a reference to an instance of the object that already exists in my C++
> code.  I tried the following:
>
> boost::python::object myObject(CppClass);
>
>
>  When I run, I get a first chance exception:
> boost::python::error_already_set.
>
>
>
> I'm sure I'm missing something important.  Any idea what's going wrong?

I will try to clarify, but I could be wrong.

The default behavior of the object constructor is to copy the object. If you want it to keep reference to existing object, than you should use boost::ref or boost::python::ptr functionality.

I also found another method to create object - using call policies:

template< typename CallPolicies, class T >
bpl::object make_object( T x ){
    //constructs object using CallPolicies result_converter
    typedef BOOST_DEDUCED_TYPENAME CallPolicies::result_converter:: template apply< T >::type result_converter_t;
    result_converter_t rc;
    return bpl::object( bpl::handle<>( rc( x ) ) );
}

You select call policy and it handles for you the conversion. The method doesn't work in all cases. It will not work if post_call function, of the call policies class does some job.

It is not clear to me why you get exception. You should provide additional information.

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070705/f4570fa3/attachment.htm>


More information about the Cplusplus-sig mailing list