[C++-sig] cvs update breaks some code (class_metadata.hpp)

Brett Calcott brett at coombs.anu.edu.au
Tue Jan 17 03:15:02 CET 2006


David Abrahams wrote:
> 
> When you register a class derived from wrapper<B>, don't declare B to
> be a base class (with bases<B>).  The library does that for you.
> 
> 

Ok. It compiles now. But the derived classes don't appear to have 
inherited the base class. In my code, both PythonBase and CPPDerived 
inherit from _BaseNotUsed. One by using wrapper<> and the other by using 
bases<>.

Has something else changed that I haven't seen?

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import simple
 >>> a = simple._BaseNotUsed()
 >>> b = simple.PythonBase()
 >>> c = simple.CPPDerived()
 >>> a.normal_function()
99
 >>> b.normal_function()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'PythonBase' object has no attribute 'normal_function'
 >>> c.normal_function()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'CPPDerived' object has no attribute 'normal_function'
 >>> isinstance(b, simple._BaseNotUsed)
False
 >>> isinstance(c, simple._BaseNotUsed)
False
 >>>

The code is as follows:

#include <boost/python/call_method.hpp>
#include <boost/python/wrapper.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/list.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>

using namespace boost::python;

struct base {
     base() {}
     int normal_function() { return 99; }
     virtual int a_function(int a) { return 0; }
};

struct python_base : base, wrapper<base>
{
     python_base() : base() {}

     int a_function(int a)
     {
         if (override f = this->get_override("a_function"))
             return f(a);
         return base::a_function(a);
     }

     int default_a_function(int a) { return this->base::a_function(a); }
};

struct cpp_derived : base, wrapper<base>
{
     cpp_derived() : base() {}

     int a_function(int a)
     {
         return a * 666;
     }
};

int call_a_function(base &base, int i)
{
     return base.a_function(i);
}

BOOST_PYTHON_MODULE(simple)
{
     class_<base
         , boost::noncopyable
         >("_BaseNotUsed")
         .def("normal_function", &base::normal_function)
         ;

     // Base for all python classes
     class_<python_base
//        , bases<base>
         , boost::noncopyable
         >("PythonBase")
         .def(init<python_base>())
         .def("a_function", &base::a_function, 
&python_base::default_a_function)
         ;

     // A C++ derived class
     class_<cpp_derived
         , bases<base>
         , boost::noncopyable
         >("CPPDerived")
         .def(init<cpp_derived>())
         ;

     def("call_a_function", &call_a_function);
}













More information about the Cplusplus-sig mailing list