[C++-sig] When You Create an Python Object Using Boost.Python, How Do You Find Out Its Name?

Nat Goodspeed ngoodspeed at solidworks.com
Thu Jul 12 02:02:14 CEST 2007


> -----Original Message-----
> From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org]
On
> Behalf Of Lawrence Spector
> Sent: Tuesday, July 10, 2007 4:33 PM
> To: Development of Python/C++ integration
> Subject: Re: [C++-sig] When You Create an Python Object Using
> Boost.Python, How Do You Find Out Its Name?
> 
> I illustrate the issue by printing the 'this' pointer at different
> sections.  Note that the memory locations are different depending on
how I
> call it.  Hopefully it makes sense.

[Nat] These are the key lines:

1.	TestClass testClass(std::cout, 3, 9.6f, 'Q');
2.	TestClassWrap testClassWrapper(testClass);
3.	boost::python::object pyTestClass(testClassWrapper);
4.	main_namespace["pyTestClass"] = pyTestClass;

It's clear from the definition of TestClassWrap that testClassWrapper
(line 2) instantiates a different TestClass object than testClass (line
1). As you asked earlier, the question is: how should line 3 behave? My
copy of the boost::python::object documentation says:

template <class T>
explicit object(T const& x);

Effects: converts x to python and manages a reference to it.
Throws: error_already_set and sets a Python TypeError exception if no
such conversion is possible.

So what does it mean to convert x to Python? boost::python::object
manages a PyObject*. (object::ptr() returns that PyObject*;
object::~object() decrements its ref count.)

But your testClassWrapper instance doesn't have a PyObject* or a ref
count. My assumption is that object's templated constructor allocates a
chunk of Python storage and constructs a fresh TestClassWrap instance
there.

Once that's in place, though, I'd assume that the assignment on line 4
above would NOT construct yet another instance. I would expect that
main_namespace["pyTestClass"] and your C++ variable pyTestClass should
both reference the same TestClassWrap instance.

If that's not what you're seeing (I haven't run your example) then I'll
have to defer to others to account for it.

> > -----Original Message-----
> > From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org]
> On
> > Behalf Of Lawrence Spector
> > Sent: Monday, July 09, 2007 5:35 PM
> > To: Development of Python/C++ integration
> > Subject: Re: [C++-sig] When You Create an Python Object Using
> > Boost.Python, How Do You Find Out Its Name?
> >
> > Any idea why?  Does the constructor of boost::python::object make a
> copy?



More information about the Cplusplus-sig mailing list