[C++-sig] Questions about doing advanced things with pyplusplus

Roman Yakovenko roman.yakovenko at gmail.com
Thu Feb 16 07:46:28 CET 2006


On 2/16/06, Allen Bierbaum <abierbaum at gmail.com> wrote:
> Roman et al:
>
> As part of my expanding use of pyplusplus I would like to start doing
> some more advanced things with the code/bindings.  I think most of
> these require using some of the interfaces to the code creators but I
> don't have a clear idea of how to do it.

In general code creators are classes with few properties and 1 function create()
That's all. Almost every line or block of code is created by code creators.

> q1: How do I rename a method, class, or namespace?

There is declaration_based_t code creator. All code creators that
expose a declaration
derive from this one. This class has alias property.

x.alias = "new_name"

> q2: How do I finalize a virtual class or method (so it doesn't get an
> polymorphic wrapper)?

x.finalize()

Also you can check, whether class or member function are finalizables.
x.is_finalizable(). For example pure virtual function is not finalizable

> q3: How do I set a call policy on a method?  (can you provide example
> code to do this directly)

x.call_policies = code_creators.return_value_policy(
code_creators.return_by_value )

An other way is to setup custom call policies factory. See py_date_time example:
http://tinyurl.com/e44kh

> q4: How do I add support for smart ptrs and holders?  (like pyste's
> use_shared_ptr() and hold_with_shared_ptr())

In most cases, pyplusplus can find out by itself what is holder is.
Any way,
x.held_type = smart_ptr #string that will be added/generated

Also take a look on pyplusplus/module_creator/types_database.py file.
This file contains code that manages all class usages. For example in order to
decide if shared_ptr is a holder of class, there should be 1 function
that takes as
argument non const shared_ptr< the class >

> q5: How do I add methods?  (can you provide example code to do this)

Take a look on py_date_time example, _customize_classes method.
http://tinyurl.com/e44kh

> q6: What is the recommended "best" way to add an additional include?
> (example code?)

module.adopt_include( "x.h" )
module.precompiled_header = "xx.h"

> q7: What is the recommended "best" way to add custom code to a module?
>  (for example to add custom wrappers or helpers)  (example code?)

There is no best way in this case. You have to find out by yourself.

Here few options to complete the task:
1. To develop custom wrappers as usual. After this add include and
registration function
    to module. py_qtxml example does this. See
http://tinyurl.com/8vddu. It create wrapper
    for QString class and then registers it.

2. Use custom_text_t code creator.

3. Contribute to boost.python and\or pyplusplus :-)
    For example pyplusplus provides wrapper for static arrays. This is
very simple wrapper:
    __get__, __set__, __len__. If you think that your case is very
common, then you
    can create wrapper and contribute it. I will try to add relevant
functionality to pyplusplus
    to identify the case and to generate relevant code.

    Functionality, that I think is missing in pyplusplus, is wrapping
function that use
    input arguments for return value, for unmodifiable Python types:

    int do_smth( int& i ){...}

    boost::python tuple do_smth_wrapper( int i ){
        int answer = do_smth(i);
        return boost::python::tuple( answer, i );
    }

> Thanks,
> Allen

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



More information about the Cplusplus-sig mailing list