How to use PyBindGen - 10 common examples

To help you get started, we’ve selected a few PyBindGen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gjcarneiro / pybindgen / tests / test-generation.py View on Github external
def try_wrapper(param, wrapper_number):
                    if 'const' in param.ctype and direction&Parameter.DIRECTION_OUT:
                        return
                    code_out.writeln("/* Test %s (%s) param type  */" % (param_type, param_handler))
                    wrapper = MyReverseWrapper(ReturnValue.new('void'), [param])
                    try:
                        wrapper.generate(code_out,
                                         '_test_wrapper_number_%i' % (wrapper_number,),
                                         ['static'])
                    except NotImplementedError:
                        sys.stderr.write("ReverseWrapper void(%s) could not be generated: not implemented"
                                         % (param.ctype))
                    sys.stdout.write("\n")
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
ManipulatedObject.add_method('GetValue', 'int', [], is_const=True)
    ManipulatedObject.add_method('SetValue', 'void', [Parameter.new('int', 'value')])

    ReferenceManipulator = mod.add_class('ReferenceManipulator', allow_subclassing=True)
    ReferenceManipulator.add_constructor([])
    ReferenceManipulator.add_method('manipulate_object', 'int', [])
    ReferenceManipulator.add_method('do_manipulate_object', 'void',
                                    [Parameter.new('ManipulatedObject&', 'obj', direction=Parameter.DIRECTION_INOUT)],
                                    is_virtual=True, is_pure_virtual=True)


    VectorLike = mod.add_class('VectorLike')
    VectorLike.add_constructor([])
    VectorLike.add_constructor([Parameter.new("VectorLike&", "obj")])
    VectorLike.add_method('get_len', 'size_t', [], custom_name='__len__')
    VectorLike.add_method('add_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__add__')
    VectorLike.add_method('iadd_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__iadd__')
    VectorLike.add_method('mul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__mul__')
    VectorLike.add_method('imul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__imul__')
    VectorLike.add_method('set_item', 'int', [Parameter.new('int', 'index'), Parameter.new('double', 'value')],
                          custom_name='__setitem__')
    VectorLike.add_method('get_item', 'double', [Parameter.new('int', 'index')], custom_name='__getitem__')
    VectorLike.add_method('set_slice', 'int', [Parameter.new('int', 'index1'),
                                               Parameter.new('int', 'index2'),
                                               Parameter.new('VectorLike', 'values')], custom_name='__setslice__')
    VectorLike.add_method('get_slice', 'VectorLike', [Parameter.new('int', 'index1'),
                                                      Parameter.new('int', 'index2')], custom_name='__getslice__')
    VectorLike.add_method('contains_value', 'int', [Parameter.new('double', 'value')], custom_name='__contains__')
    VectorLike.add_method('append', 'void', [Parameter.new('double', 'value')])

    VectorLike2 = mod.add_class('VectorLike2')
    VectorLike2.add_constructor([])
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
SomeObject.add_method('get_pyobject',
                          ReturnValue.new('PyObject*', caller_owns_return=True),
                          [],
                          is_virtual=True)

    ## add a function that appears as a method of an object
    SomeObject.add_function_as_method('some_object_get_something_prefixed',
                                      ReturnValue.new('std::string'),
                                      [Parameter.new('const SomeObject*', 'obj', transfer_ownership=False),
                                       Parameter.new('std::string', 'something')],
                                      custom_name='get_something_prefixed')

    ## add a function that appears as a method of an object
    SomeObject.add_function_as_method('some_object_val_get_something_prefixed',
                                      ReturnValue.new('std::string'),
                                      [Parameter.new('SomeObject', 'obj'),
                                       Parameter.new('std::string', 'something')],
                                      custom_name='val_get_something_prefixed')

    ## add a function that appears as a method of an object
    SomeObject.add_function_as_method('some_object_ref_get_something_prefixed',
                                      ReturnValue.new('std::string'),
                                      [Parameter.new('const SomeObject&', 'obj'),
                                       Parameter.new('std::string', 'something')],
                                      custom_name='ref_get_something_prefixed')

    # ---
    SomeObject.add_method('call_get_prefix', ReturnValue.new('std::string'), [])

    SomeObject.add_method('set_foo_value', None, [Parameter.new('Foo', 'foo')])
    SomeObject.add_method('get_foo_value', ReturnValue.new('Foo'), [])
github gjcarneiro / pybindgen / tests / boost / barmodulegen.py View on Github external
def my_module_gen(out_file):

    mod = Module('bar')

    mod.add_include ('"bar.h"')

    Foo = mod.add_class('Foo', automatic_type_narrowing=True,
                        memory_policy=BoostSharedPtr('::Foo'))

    Foo.add_static_attribute('instance_count', ReturnValue.new('int'))
    Foo.add_constructor([Parameter.new('std::string', 'datum')])
    Foo.add_constructor([])
    Foo.add_method('get_datum', ReturnValue.new('const std::string'), [])
    Foo.add_method('is_initialized', ReturnValue.new('bool'), [], is_const=True)
    Foo.add_output_stream_operator()


    mod.add_function('function_that_takes_foo', ReturnValue.new('void'),
                               [param('boost::shared_ptr', 'foo')])
    mod.add_function('function_that_returns_foo', retval('boost::shared_ptr'), [])
    
    cls = mod.add_class('ClassThatTakesFoo', allow_subclassing=True)
    cls.add_constructor([Parameter.new('boost::shared_ptr', 'foo')])
    cls.add_method('get_foo', ReturnValue.new('boost::shared_ptr'), [])
    cls.add_method('get_modified_foo', retval('boost::shared_ptr'),
                   [param('boost::shared_ptr', 'foo')],
                   is_virtual=True, is_const=True)
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
mod.add_function('invoke_some_object_get_prefix', ReturnValue.new('std::string'),
                     [])
    mod.add_function('take_some_object', ReturnValue.new('SomeObject*', caller_owns_return=True), [])
    mod.add_function('delete_some_object', ReturnValue.new('void'), [])

    xpto = mod.add_cpp_namespace("xpto")
    xpto.add_function('some_function', ReturnValue.new('std::string'), [])

    ## enums..
    xpto.add_enum('FooType', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC'])
    xpto.add_function('get_foo_type', ReturnValue.new('FooType'), [])
    xpto.add_function('set_foo_type', ReturnValue.new('void'), [Parameter.new("FooType", 'type')])
    xpto.add_function('set_foo_type_inout', ReturnValue.new('void'),
                      [Parameter.new("FooType&", 'type', direction=Parameter.DIRECTION_INOUT)])
    xpto.add_function('set_foo_type_ptr', ReturnValue.new('void'),
                      [Parameter.new("FooType*", 'type', direction=Parameter.DIRECTION_INOUT)])


    xpto_SomeClass = xpto.add_class('SomeClass', docstring="This is the docstring for SomeClass")
    xpto_SomeClass.add_constructor([])

    xpto.add_typedef(Foo, 'FooXpto')
    xpto.add_function('get_foo_datum', 'std::string', [Parameter.new('const xpto::FooXpto&', 'foo')])

    typehandlers.add_type_alias('uint32_t', 'xpto::FlowId')
    xpto.add_function('get_flow_id', 'xpto::FlowId', [Parameter.new('xpto::FlowId', 'flowId')])

    # bug #798383
    XptoClass = xpto.add_struct('XptoClass')
    XptoClass.add_method("GetSomeClass", retval("xpto::SomeClass*", caller_owns_return=True), [])
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
SomeObject = mod.add_class('SomeObject', allow_subclassing=True)

    SomeObject.add_instance_attribute('foo', ReturnValue.new('Foo'),
                                      getter='get_foo_value',
                                      setter='set_foo_value')
    SomeObject.add_instance_attribute('m_prefix', ReturnValue.new('std::string'))
    SomeObject.add_static_attribute('staticData', ReturnValue.new('std::string'))

    SomeObject.add_static_attribute('instance_count', ReturnValue.new('int'))

    SomeObject.add_method('add_prefix', ReturnValue.new('int'),
                          [Parameter.new('std::string&', 'message',
                                         direction=Parameter.DIRECTION_INOUT)])
    SomeObject.add_constructor([Parameter.new('std::string', 'prefix')])
    SomeObject.add_constructor([Parameter.new('int', 'prefix_len')])

    SomeObject.add_method('operator()', ReturnValue.new('int'),
                          [Parameter.new('std::string&', 'message',
                                         direction=Parameter.DIRECTION_INOUT)],
                          custom_name='__call__')


    # --- some virtual methods ---
    SomeObject.add_method('get_prefix', ReturnValue.new('std::string'), [],
                          is_virtual=True, is_const=True)

    SomeObject.add_method('get_prefix_with_foo_value', ReturnValue.new('std::string'),
                          [Parameter.new('Foo', 'foo')],
                          is_virtual=True, is_const=True)

    SomeObject.add_method('get_prefix_with_foo_ref',
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
SomeObject.add_method('get_int', ReturnValue.new('int'),
                          [Parameter.new('double', 'from_float')],
                          custom_name="get_int")

    # Bug #508577
    SomeObject.add_method('protected_method_that_is_not_virtual',
                          ReturnValue.new('std::string'),
                          [Parameter.new('std::string', 'arg')],
                          is_const=True, visibility='protected')

    SomeObject.add_method('method_returning_cstring', ReturnValue.new('const char *'),
                          [], is_virtual=True, is_const=True)


    mod.add_function('store_some_object', ReturnValue.new('void'),
                     [Parameter.new('SomeObject*', 'obj', transfer_ownership=True)])
    mod.add_function('invoke_some_object_get_prefix', ReturnValue.new('std::string'),
                     [])
    mod.add_function('take_some_object', ReturnValue.new('SomeObject*', caller_owns_return=True), [])
    mod.add_function('delete_some_object', ReturnValue.new('void'), [])

    xpto = mod.add_cpp_namespace("xpto")
    xpto.add_function('some_function', ReturnValue.new('std::string'), [])

    ## enums..
    xpto.add_enum('FooType', ['FOO_TYPE_AAA', 'FOO_TYPE_BBB', 'FOO_TYPE_CCC'])
    xpto.add_function('get_foo_type', ReturnValue.new('FooType'), [])
    xpto.add_function('set_foo_type', ReturnValue.new('void'), [Parameter.new("FooType", 'type')])
    xpto.add_function('set_foo_type_inout', ReturnValue.new('void'),
                      [Parameter.new("FooType&", 'type', direction=Parameter.DIRECTION_INOUT)])
    xpto.add_function('set_foo_type_ptr', ReturnValue.new('void'),
                      [Parameter.new("FooType*", 'type', direction=Parameter.DIRECTION_INOUT)])
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
TestContainer.add_instance_attribute('m_floatSet', 'std::set')
    TestContainer.add_method('get_simple_list', ReturnValue.new('SimpleStructList'), [], is_virtual=True)
    TestContainer.add_method('set_simple_list', 'int', [Parameter.new('SimpleStructList', 'list')], is_virtual=True)
    TestContainer.add_method('set_simple_list_by_ref', 'int', [Parameter.new('SimpleStructList&', 'inout_list',
                                                                             direction=Parameter.DIRECTION_INOUT)],
                             is_virtual=True)

    mod.add_container('std::vector', ReturnValue.new('simple_struct_t'), 'vector')
    TestContainer.add_method('get_simple_vec', ReturnValue.new('std::vector'), [], is_virtual=True)
    TestContainer.add_method('set_simple_vec', 'int', [Parameter.new('std::vector', 'vec')], is_virtual=True)

    mod.add_container('std::vector', 'std::string', 'vector')
    TestContainer.add_method('get_vec', 'void', [Parameter.new('std::vector &', 'outVec',
                                                               direction=Parameter.DIRECTION_OUT)])

    TestContainer.add_method('set_vec_ptr', 'void', [Parameter.new('std::vector*', 'inVec',
                                                                   direction=Parameter.DIRECTION_IN, transfer_ownership=True)])
    TestContainer.add_method('get_vec_ptr', 'void', [Parameter.new('std::vector*', 'outVec',
                                                                   direction=Parameter.DIRECTION_OUT)])


    mod.add_container('std::map',
                      (ReturnValue.new('std::string'), ReturnValue.new('simple_struct_t')),
                      'map')
    TestContainer.add_method('get_simple_map', ReturnValue.new('std::map'), [], is_virtual=True)
    TestContainer.add_method('set_simple_map', 'int', [Parameter.new('std::map', 'map')], is_virtual=True)


    Tupl = mod.add_class('Tupl')
    Tupl.add_binary_comparison_operator('<')
    Tupl.add_binary_comparison_operator('<=')
    Tupl.add_binary_comparison_operator('>=')
github gjcarneiro / pybindgen / tests / foomodulegen.py View on Github external
ReferenceManipulator = mod.add_class('ReferenceManipulator', allow_subclassing=True)
    ReferenceManipulator.add_constructor([])
    ReferenceManipulator.add_method('manipulate_object', 'int', [])
    ReferenceManipulator.add_method('do_manipulate_object', 'void',
                                    [Parameter.new('ManipulatedObject&', 'obj', direction=Parameter.DIRECTION_INOUT)],
                                    is_virtual=True, is_pure_virtual=True)


    VectorLike = mod.add_class('VectorLike')
    VectorLike.add_constructor([])
    VectorLike.add_constructor([Parameter.new("VectorLike&", "obj")])
    VectorLike.add_method('get_len', 'size_t', [], custom_name='__len__')
    VectorLike.add_method('add_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__add__')
    VectorLike.add_method('iadd_VectorLike', 'VectorLike', [Parameter.new('VectorLike', 'rhs')], custom_name='__iadd__')
    VectorLike.add_method('mul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__mul__')
    VectorLike.add_method('imul_VectorLike', 'VectorLike', [Parameter.new('unsigned int', 'n')], custom_name='__imul__')
    VectorLike.add_method('set_item', 'int', [Parameter.new('int', 'index'), Parameter.new('double', 'value')],
                          custom_name='__setitem__')
    VectorLike.add_method('get_item', 'double', [Parameter.new('int', 'index')], custom_name='__getitem__')
    VectorLike.add_method('set_slice', 'int', [Parameter.new('int', 'index1'),
                                               Parameter.new('int', 'index2'),
                                               Parameter.new('VectorLike', 'values')], custom_name='__setslice__')
    VectorLike.add_method('get_slice', 'VectorLike', [Parameter.new('int', 'index1'),
                                                      Parameter.new('int', 'index2')], custom_name='__getslice__')
    VectorLike.add_method('contains_value', 'int', [Parameter.new('double', 'value')], custom_name='__contains__')
    VectorLike.add_method('append', 'void', [Parameter.new('double', 'value')])

    VectorLike2 = mod.add_class('VectorLike2')
    VectorLike2.add_constructor([])
    VectorLike2.add_method('append', 'void', [Parameter.new('double', 'value')])
github gjcarneiro / pybindgen / tests / boost / barmodulegen.py View on Github external
mod = Module('bar')

    mod.add_include ('"bar.h"')

    Foo = mod.add_class('Foo', automatic_type_narrowing=True,
                        memory_policy=BoostSharedPtr('::Foo'))

    Foo.add_static_attribute('instance_count', ReturnValue.new('int'))
    Foo.add_constructor([Parameter.new('std::string', 'datum')])
    Foo.add_constructor([])
    Foo.add_method('get_datum', ReturnValue.new('const std::string'), [])
    Foo.add_method('is_initialized', ReturnValue.new('bool'), [], is_const=True)
    Foo.add_output_stream_operator()


    mod.add_function('function_that_takes_foo', ReturnValue.new('void'),
                               [param('boost::shared_ptr', 'foo')])
    mod.add_function('function_that_returns_foo', retval('boost::shared_ptr'), [])
    
    cls = mod.add_class('ClassThatTakesFoo', allow_subclassing=True)
    cls.add_constructor([Parameter.new('boost::shared_ptr', 'foo')])
    cls.add_method('get_foo', ReturnValue.new('boost::shared_ptr'), [])
    cls.add_method('get_modified_foo', retval('boost::shared_ptr'),
                   [param('boost::shared_ptr', 'foo')],
                   is_virtual=True, is_const=True)


    
    #### --- error handler ---
    class MyErrorHandler(pybindgen.settings.ErrorHandler):
        def __init__(self):
            super(MyErrorHandler, self).__init__()