Boost logo

Boost :

From: Mike Owen (mikeowen_at_[hidden])
Date: 2001-07-03 02:33:23


I'm getting unexpected behaviour when using methods on attributes of BPL
wrapped objects. For instance, if I have a wrapped class A that has another
wrapped class B as a member, calling the methods of the B instance in A does
not affect this B instance. It looks like temporaries are being constructed
and then destroyed when I do this. Here is a concrete example:

struct Interior {
  double x;
};

struct Top {
  Interior interiorInstance;
};

#include <boost/python/class_builder.hpp>
namespace python = boost::python;

namespace {
  BOOST_PYTHON_MODULE_INIT(TestAttr) {
    try {
      python::module_builder module("TestAttr");

      python::class_builder<Interior> InteriorClass(module, "Interior");
      InteriorClass.def(python::constructor<>());
      InteriorClass.def_read_write(&Interior::x, "x");

      python::class_builder<Top> TopClass(module, "Top");
      TopClass.def(python::constructor<>());
      TopClass.def_read_write(&Top::interiorInstance, "interiorInstance");
    }
    catch(...) {
      python::handle_exception();
    }
  }
}

So the class "Top" has a member instance of "Interior". Both classes are
exposed to python with BPL. However, when I try to set the "x" value of
Top::Interior, it always fails. Here's the output that results from
compiling the above and trying the experiment:

HodgePodge{owen}: python
Python 2.1 (#2, May 9 2001, 22:11:49)
[GCC 2.96 20000731 (Linux-Mandrake 8.0 2.96-0.48mdk)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import TestAttr
>>> a = TestAttr.Interior()
>>> a.x
1.750725915200081e-313
>>> a.x = 100
>>> a.x
100.0
>>> b = TestAttr.Top()
>>> b.interiorInstance
<Interior object at 0x8115c20>
>>> b.interiorInstance.x
1.750725915200081e-313
>>> b.interiorInstance.x = 100
>>> b.interiorInstance.x
1.750725915200081e-313
>>>

This seems like incorrect behaviour to me, but am I wrong? How should I be
constructing the BPL wrap code so that I can call methods on the member
class? Thanks!

Mike.

-- 
 "Hey...where are the sunflower seeds?" |       J. Michael Owen         
        o_o /                           |       
        (")                             |       Email:  mikeowen_at_[hidden]
       \/'\/                            |
____(__(,_,)_______________________________________________________________

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk