How to use the pybindgen.Parameter function in PyBindGen

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 gergo- / pylibjit / libjit-python / modulegen.py View on Github external
wrapper.after_call.write_code("%s = jit_function::from_raw(%s);" % (func_t, elem))
        wrapper.after_call.write_code("if (%s != NULL) {" % (func_t))
        wrapper.after_call.write_code("    %s->obj = new jit_function(*%s);" % (py_func, func_t))
        wrapper.after_call.write_code("} else {")
        wrapper.after_call.write_code("    %s->obj = new PyJit_function__PythonHelper(%s);" % (py_func, elem))
        wrapper.after_call.write_code("    ((PyJit_function__PythonHelper*) %s->obj)->set_pyobj((PyObject *)%s);" % (py_func, py_func))
        wrapper.after_call.write_code("}")
        wrapper.after_call.write_code("%s->flags = PYBINDGEN_WRAPPER_FLAG_NONE;" % (py_func))
        wrapper.after_call.write_code("PyList_Append(%s, (PyObject*)%s);" % (py_list, py_func))
        wrapper.after_call.unindent()
        wrapper.after_call.write_code('}')
        wrapper.build_params.add_parameter("N", [py_list], prepend=True)


class FreeFuncParam(Parameter):
    DIRECTIONS = [Parameter.DIRECTION_IN]
    CTYPES = ['jit_meta_free_func']
    def convert_python_to_c(self, wrapper):
        wrapper.call_params.append(self.name)


class JitMemoryPolicy(cppclass.FreeFunctionPolicy):
    def get_pointer_type(self, class_full_name):
        return class_full_name+'_t '

    def __repr__(self):
        return 'JitMemoryPolicy(%r)' % self.free_function


def module_init():
    root_module = Module('jit', cpp_namespace='::')
github Gabrielcarvfer / NS3 / bindings / python / ns3modulegen_core_customizations.py View on Github external
('ate', 'std::ios_base::ate'),
            ('binary', 'std::ios_base::binary'),
            ('in', 'std::ios_base::in'),
            ('out', 'std::ios_base::out'),
            ('trunc', 'std::ios_base::trunc'),
            ])
    ofstream.add_constructor([Parameter.new("const char *", 'filename'),
                              Parameter.new("::std::ofstream::openmode", 'mode', default_value="std::ios_base::out")])
    ofstream.add_method('close', None, [])

    add_std_ios_openmode(module)


class IosOpenmodeParam(Parameter):

    DIRECTIONS = [Parameter.DIRECTION_IN]
    CTYPES = ['std::ios::openmode', 'std::_Ios_Openmode']

    def convert_c_to_python(self, wrapper):
        assert isinstance(wrapper, ReverseWrapperBase)
        wrapper.build_params.add_parameter('i', [self.value])

    def convert_python_to_c(self, wrapper):
        assert isinstance(wrapper, ForwardWrapperBase)
        name = wrapper.declarations.declare_variable("std::ios::openmode", self.name, self.default_value)
        wrapper.parse_params.add_parameter('i', ['&'+name], self.name, optional=bool(self.default_value))
        wrapper.call_params.append(name)



def add_std_ios_openmode(module):
    for flag in 'in', 'out', 'ate', 'app', 'trunc', 'binary':
github Gabrielcarvfer / NS3 / src / core / bindings / modulegen_customizations.py View on Github external
from pybindgen.typehandlers.base import CodeGenerationError


class ArgvParam(Parameter):
    """
    Converts a python list-of-strings argument to a pair of 'int argc,
    char *argv[]' arguments to pass into C.

    One Python argument becomes two C function arguments -> it's a miracle!

    Note: this parameter type handler is not registered by any name;
    must be used explicitly.
    """

    DIRECTIONS = [Parameter.DIRECTION_IN]
    CTYPES = []
    
    def convert_c_to_python(self, wrapper):
        raise NotImplementedError

    def convert_python_to_c(self, wrapper):
        py_name = wrapper.declarations.declare_variable('PyObject*', 'py_' + self.name)
        argc_var = wrapper.declarations.declare_variable('int', 'argc')
        name = wrapper.declarations.declare_variable('char**', self.name)
        idx = wrapper.declarations.declare_variable('Py_ssize_t', 'idx')
        wrapper.parse_params.add_parameter('O!', ['&PyList_Type', '&'+py_name], self.name)

        #wrapper.before_call.write_error_check('!PyList_Check(%s)' % py_name) # XXX

        wrapper.before_call.write_code("%s = (char **) malloc(sizeof(char*)*PyList_Size(%s));"
                                       % (name, py_name))
github gjcarneiro / pybindgen / examples / d / modulegen.py View on Github external
def my_module_gen(out_file):

    mod = Module('d')
    mod.add_include('"d.h"')

    D = mod.add_class('D', memory_policy=cppclass.FreeFunctionPolicy('DDestroy'))
    D.add_instance_attribute('d', ReturnValue.new('bool'))
    D.add_function_as_constructor("DCreate", ReturnValue.new("D*", caller_owns_return=True), [])
    mod.add_function('DDoA', None, [Parameter.new('D*', 'd', transfer_ownership=False)])
    mod.add_function('DDoB', None, [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN)])
    mod.add_function('DDoC', None, [Parameter.new('const D&', 'd',
                                                  direction=Parameter.DIRECTION_IN)])


    mod.generate(FileCodeSink(out_file) )
github Gabrielcarvfer / NS3 / bindings / python / ns3modulegen_core_customizations.py View on Github external
ofstream.add_enum('openmode', [
            ('app', 'std::ios_base::app'),
            ('ate', 'std::ios_base::ate'),
            ('binary', 'std::ios_base::binary'),
            ('in', 'std::ios_base::in'),
            ('out', 'std::ios_base::out'),
            ('trunc', 'std::ios_base::trunc'),
            ])
    ofstream.add_constructor([Parameter.new("const char *", 'filename'),
                              Parameter.new("::std::ofstream::openmode", 'mode', default_value="std::ios_base::out")])
    ofstream.add_method('close', None, [])

    add_std_ios_openmode(module)


class IosOpenmodeParam(Parameter):

    DIRECTIONS = [Parameter.DIRECTION_IN]
    CTYPES = ['std::ios::openmode', 'std::_Ios_Openmode']

    def convert_c_to_python(self, wrapper):
        assert isinstance(wrapper, ReverseWrapperBase)
        wrapper.build_params.add_parameter('i', [self.value])

    def convert_python_to_c(self, wrapper):
        assert isinstance(wrapper, ForwardWrapperBase)
        name = wrapper.declarations.declare_variable("std::ios::openmode", self.name, self.default_value)
        wrapper.parse_params.add_parameter('i', ['&'+name], self.name, optional=bool(self.default_value))
        wrapper.call_params.append(name)
github gergo- / pylibjit / libjit-python / modulegen.py View on Github external
def get_common_header_code_sink(self):
        return self.header_sink

    def get_common_header_include(self):
        return '"%s"' % self.header_name

    def close(self):
        self.header_sink.file.close()
        self.main_sink.file.close()
        for sink in self.section_sinks.itervalues():
            sink.file.close()


class VoidPtrParam(Parameter):
    DIRECTIONS = [Parameter.DIRECTION_IN, Parameter.DIRECTION_INOUT, Parameter.DIRECTION_OUT]
    CTYPES = ['void *']

    def convert_python_to_c(self, wrapper):
        name = wrapper.declarations.declare_variable("PyObject *", self.name)
        wrapper.call_params.append('(%s)PyLong_AsVoidPtr(%s)' % (self.ctype, name))
        if self.direction & self.DIRECTION_IN:
            wrapper.parse_params.add_parameter('O!', ['&PyLong_Type', '&'+name], self.name)
        if self.direction & self.DIRECTION_OUT:
            wrapper.build_params.add_parameter('N', ["PyLong_FromVoidPtr((void *)%s)" % name])

class VoidPtrReturn(ReturnValue):
    CTYPES = ['void *']

    def get_c_error_return(self):
        return "return NULL;"
github gjcarneiro / pybindgen / examples / g / modulegen.py View on Github external
GInner = G.add_cpp_namespace("GInner")
    GInner.add_function('GDoC', None, [])

    G.add_include('')

    ofstream = G.add_class('ofstream', foreign_cpp_namespace='::std')
    ofstream.add_enum('openmode', [
            ('app', 'std::ios_base::app'),
            ('ate', 'std::ios_base::ate'),
            ('binary', 'std::ios_base::binary'),
            ('in', 'std::ios_base::in'),
            ('out', 'std::ios_base::out'),
            ('trunc', 'std::ios_base::trunc'),
            ])
    ofstream.add_constructor([Parameter.new("const char *", 'filename'),
                              Parameter.new("::std::ofstream::openmode", 'mode', default_value="std::ios_base::out")])
    ofstream.add_method('close', None, [])

    mod.generate(FileCodeSink(out_file))
github nyuwireless-unipd / ns3-mmwave / bindings / python / ns3modulegen_core_customizations.py View on Github external
Warning)
            continue

        arguments = []
        ok = True
        callback_parameters = [arg for arg in template_parameters[1:] if arg != 'ns3::empty']
        for arg_num, arg_type in enumerate(callback_parameters):
            arg_name = 'arg%i' % (arg_num+1)

            param_ctype = ctypeparser.parse_type(arg_type)
            if ('const' in param_ctype.remove_modifiers()):
                kwargs = {'is_const': True}
            else:
                kwargs = {}
            try:
                arguments.append(Parameter.new(str(param_ctype), arg_name, **kwargs))
            except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError) as ex:
                warnings.warn("***** Unable to register callback; parameter '%s %s' error (used in %s): %r"
                              % (arg_type, arg_name, cls_name, ex),
                              Warning)
                ok = False
        if not ok:
            try:
                typehandlers.return_type_matcher.lookup(cls_name)[0].DISABLED = True
            except typehandlers.TypeLookupError:
                pass
            try:
                typehandlers.param_type_matcher.lookup(cls_name)[0].DISABLED = True
            except typehandlers.TypeLookupError:
                pass
            continue
github Gabrielcarvfer / NS3 / bindings / python / ns3modulegen_core_customizations.py View on Github external
def add_std_ofstream(module):
    module.add_include('')
    ostream = module.add_class('ostream', foreign_cpp_namespace='::std')
    ostream.set_cannot_be_constructed("abstract base class")
    ofstream = module.add_class('ofstream', foreign_cpp_namespace='::std', parent=ostream)
    ofstream.add_enum('openmode', [
            ('app', 'std::ios_base::app'),
            ('ate', 'std::ios_base::ate'),
            ('binary', 'std::ios_base::binary'),
            ('in', 'std::ios_base::in'),
            ('out', 'std::ios_base::out'),
            ('trunc', 'std::ios_base::trunc'),
            ])
    ofstream.add_constructor([Parameter.new("const char *", 'filename'),
                              Parameter.new("::std::ofstream::openmode", 'mode', default_value="std::ios_base::out")])
    ofstream.add_method('close', None, [])

    add_std_ios_openmode(module)
github Gabrielcarvfer / NS3 / 3rd-party / pybindgen / examples / d / modulegen.py View on Github external
def my_module_gen(out_file):

    mod = Module('d')
    mod.add_include('"d.h"')

    D = mod.add_class('D', memory_policy=cppclass.FreeFunctionPolicy('DDestroy'))
    D.add_instance_attribute('d', ReturnValue.new('bool'))
    D.add_function_as_constructor("DCreate", ReturnValue.new("D*", caller_owns_return=True), [])
    mod.add_function('DDoA', None, [Parameter.new('D*', 'd', transfer_ownership=False)])
    mod.add_function('DDoB', None, [Parameter.new('D&', 'd', direction=Parameter.DIRECTION_IN)])
    mod.add_function('DDoC', None, [Parameter.new('const D&', 'd',
                                                  direction=Parameter.DIRECTION_IN)])


    mod.generate(FileCodeSink(out_file) )