[C++-sig] [Py++] Registration order corner case

Roman Yakovenko roman.yakovenko at gmail.com
Thu Mar 25 20:09:12 CET 2010

On Thu, Mar 25, 2010 at 8:34 PM, Christopher Bruns <cmbruns at stanford.edu> wrote:
> Py++ has produced for me over 97,000 lines of beautiful boost.python
> wrapping code on my project, using less than 2500 lines of python.

:-)))). I would say this is pretty good ratio

> But I am still experiencing some minor troubles that prevent me from
> completing my project.
> Suppose I wish to wrap the following:
> //###### foo.h ###########
> struct Foo1 { struct Foo2; };
> struct Foo3 : public Foo1 {};
> struct Foo1::Foo2 : public Foo3 {};
> ...

I am pretty sure Py++ doesn't export such hierarchies right. The class
sorting algorithm is a simple DFS, which doesn't take into account
such use case - "derived from the inner class"

> The only way to avoid the registration order problem is to register
> these classes to boost.python in exactly the order Foo1, Foo3, Foo2
> (just like the definitions in C++).
> #include "boost/python.hpp"
> #include "foo.h"
> namespace bp = boost::python;
>    typedef bp::class_< Foo1 > Foo1_exposer_t;
>    Foo1_exposer_t Foo1_exposer = Foo1_exposer_t( "Foo1" );
>    bp::class_< Foo3, bp::bases< Foo1 > >( "Foo3" );
>    bp::scope Foo1_scope( Foo1_exposer );
>    bp::class_< Foo1::Foo2, bp::bases< Foo3 > >( "Foo2" );
> }
> Is there a way to coax pyplusplus to produce this registration order?


if <you have some free time >:

I suggest you to take a look on
"pyplusplus/creators_factory/sort_algorithms.py" file. It contains
class_organizer_t class, which is responsible for defining the order
of the classes for exporting. As I already said, it implements a
simple DFS that doesn't take many details into account. It could be
nice to get this algorithm right in your use case too.

The class tester is located under
pyplusplus_dev/unittests/algorithms_tester.py - class

else: #If you don't have time:

pyplusplus/creators_factory/sort_algorithms.py contains sort_classes
function, which is a convenience wrapper around class_organizer_t
class. You can replace this function with your own one:

pypp_sort_classes = pyplusplus/creators_factory/sort_algorithms.sort_classes

def my_sort_classes( classes, include_vars=False )
    sorted = pypp_sort_classes( classes, include_vars )
    < put class X before class Y >
    return sorted

pyplusplus/creators_factory/sort_algorithms.sort_classes = my_sort_classes

If you have even less time, you always can modified file by hands,
create the patch and apply it every time after code generation


Roman Yakovenko
C++ Python language binding

More information about the Cplusplus-sig mailing list