[C++-sig] TnFOX with the latest py++ from SVN

Roman Yakovenko roman.yakovenko at gmail.com
Mon Oct 2 21:06:00 CEST 2006


On 10/2/06, Niall Douglas <s_sourceforge at nedprod.com> wrote:
> On 2 Oct 2006 at 9:15, David Abrahams wrote:
>
> > >> Surely BPL can rebind the value of sx on output back to the input? I
> > >> remember something in the tutorial about this just working out of the
> > >> box.
> > >
> > > No it can not. In near future it will be very easy to instruct Py++ to
> > > create wrapper
> > > for such functions.
> >
> > But even with a wrapper it's never going to automagically rebind
> > anything, FWIW.
>
> I'm probably being stupid, but surely if you pass by non-const
> reference then you *expect* it to change?
>
> Of course, modified in C++ is not modified in Python where basic
> types are immutable. But it does seem a reasonable approximation that
> as a default, the python parameter is rebound to the output value for
> integral types.

I am not sure whether it reasonable. In my opinion the behaviour, you
propose, will
surprise any Python developer and I am sure will break few invariants in Python.

Solutions:

 "Out of the box" Boost.Python has a weaknesses if you work with C-like code.
Consider next function:

void do_smth( int& arg );

It is not possible to expose this function as-is to Python, a wrapper should be
created:

int do_smth_wrapper( int arg ){
    int tmp( arg );
    do_smth( tmp );
    return tmp;
}

In my opinion Boost.Python could introduce interface similar to CTypes module:
http://docs.python.org/dev/lib/node452.html . It will allow users to expose
such functions without additional work. Users who want to call this function
from Python will have to create special object - c_int:

import my_module
from boost_python_core import c_int

arg = c_int( 23 )
my_module.do_smth( arg )

This approach has few advantages over the wrapper creation one: smaller
compilation time, customization\\convenience wrappers could be created from
Python.

I am not sure whether it is possible to implement this in BPL or not. Meanwhile
Matthias Baas introduced new feature to Py++ - "function transformation".
It allows user to instruct Py++ what wrapper to create. The approach
is semi-automated:

( The API of "function transformation" feature is not closed yet. )

mb = module_builder_t( ... )
do_smth = mb.free_function( 'do_smth' )
do_smth.function_transformations.append( inout( 0 ) )

Py++ will generate do_smth_wrapper function.
Also in this case Py++ can( and some day it will ) generate a wrapper version
without user intervention.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list