[C++-sig] Inserting custom code into pyplusplus output

Roman Yakovenko roman.yakovenko at gmail.com
Tue Jun 13 10:59:10 CEST 2006


On 6/13/06, Niall Douglas <s_sourceforge at nedprod.com> wrote:
> How do I insert custom code into the BOOST_PYTHON_MODULE() output
> from pyplusplus?
>
>
> I want:
>
> extern void InitialiseTnFOXPython();
>
> BOOST_PYTHON_MODULE(TnFOX){
>     InitialiseTnFOXPython();

from pyplusplus import code_creators

mb = module_builder_t( ... )
mb.build_code_creator( ... )

code = your code
mb.code_creator.body.adopt_creator( code_creators.custom_text_t( code ), 0 )

> I can't see any way from the sources on how to do this :(

I will try to document code creators before next release

>
> Next bit of custom code:
>
> In FXApp::init(), it takes mandatory parameters of void init( int &
> argc, char * * argv, ::FX::FXbool connect ). Obviously, BPL doesn't
> know to match a list of strings to a char **argv, so under pyste I
> inserted the following custom code:
>
> DEFINE_MAKECARRAYITER(FXApp, const FX::FXchar *, getArgv, (),
> c.getArgc())
> static void FXApp_init(FXApp &app, int argc, list argv, unsigned char
> connect=TRUE)
> {
>         int n, size=PyList_Size(argv.ptr());
>         static QMemArray<const char *> array;
>         array.resize(size+1);
>         for(n=0; n<size; n++)
>         {
>                 array[n]=PyString_AsString(PyList_GetItem(argv.ptr(), n));
>         }
>         array[n]=0;
>         app.init(argc, (char **)(array.data()), connect);
> }
> static void FXApp_init2(FXApp &app, int argc, list argv)
> {
>         FXApp_init(app, argc, argv);
> }
>
> ... and then I replaced:
>
> -        .def("init", &FX::FXApp::init,
> &TnFOX::FX_FXApp_Wrapper::default_init_3)
> -        .def("init", &TnFOX::FX_FXApp_Wrapper::default_init_2)
> +        .def("init", &FXApp_init)
> +        .def("init", &FXApp_init2)
>
> -        .def("getArgv", &FX::FXApp::getArgv,
> return_internal_reference< 1 >())
> +        .def("getArgv", &FXApp_getArgv)
>
> So can pyplusplus be extended to automatically generate this kind of
> accessor code

We are working on this:
http://language-binding.net/pyplusplus/peps/call_wrapper_policies.html

> if not how can I insert this custom code via pyplusplus?

1. You should exclude FXApp::init functions from being exposed, right?

mb = module_builder_t( ... _
FXApp = mb.class_( 'FXApp' )
FXApp.member_functions( 'init' ).exclude()
init = """def("init", &FX::FXApp::init,
&TnFOX::FX_FXApp_Wrapper::default_init_3)"""
FXApp.add_code( init )
...

> We are very close to a working TnFOX python bindings. Yay!

It is really good news :-). I am glad you find the time to work on this project.
I have few ideas about new GUI to pyplusplus. May be I will implement it using
Python bindings to TnFOX, we'll see.

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



More information about the Cplusplus-sig mailing list