Example #1
0
def generate_from(module_name: str, func_decl: ast_pb2.FuncDecl,
                  class_decl: Optional[ast_pb2.ClassDecl]):
    """Generates pybind11 bindings code for functions.

  Args:
    module_name: String containing the superclass name.
    func_decl: Function declaration in proto format.
    class_decl: Outer class declaration in proto format. None if the function is
      not a member of a class.

  Yields:
    pybind11 function bindings code.
  """

    operator_index = utils.find_operator(func_decl.name.cpp_name)
    if operator_index >= 0:
        for s in operators.generate_operator(module_name, func_decl,
                                             operator_index):
            yield I + s
        return

    func_def = I + f'{module_name}.def("{func_decl.name.native}", '
    func_def += _generate_cpp_function_cast(func_decl, class_decl)
    func_def += f'&{func_decl.name.cpp_name}'
    if func_decl.params:
        func_def += f', {_generate_params_list(func_decl.params)}'
    if func_decl.docstring:
        func_def += f', {_generate_docstring(func_decl.docstring)}'
    func_def += ');'
    yield func_def
Example #2
0
def generate_from(module_name: str, func_decl: ast_pb2.FuncDecl,
                  class_decl: Optional[ast_pb2.ClassDecl]):
    """Generates pybind11 bindings code for functions.

  Args:
    module_name: String containing the superclass name.
    func_decl: Function declaration in proto format.
    class_decl: Outer class declaration in proto format. None if the function is
      not a member of a class.

  Yields:
    pybind11 function bindings code.
  """

    if len(func_decl.returns) >= 2 or (len(func_decl.returns) >= 1
                                       and func_decl.cpp_void_return
                                       and func_decl.params):
        yield from _generate_return_args_lambda(func_decl, module_name)
        return

    cpp_lambda_return_type = _has_bytes_return(func_decl)
    if cpp_lambda_return_type:
        yield from _generate_cpp_lambda(func_decl, cpp_lambda_return_type,
                                        module_name)
        return

    if func_decl.classmethod:
        for line in _generate_static_method(module_name, func_decl.name.native,
                                            func_decl.name.cpp_name):
            yield I + line
        return

    operator_index = utils.find_operator(func_decl.name.cpp_name)
    if operator_index >= 0 and utils.is_special_operation(
            func_decl.name.native):
        for s in operators.generate_operator(module_name, func_decl,
                                             operator_index):
            yield I + s
            return

    func_name = utils.format_func_name(func_decl.name.native)
    func_def = I + f'{module_name}.def("{func_name}", '
    func_def += _generate_cpp_function_cast(func_decl, class_decl)
    func_def += f'&{func_decl.name.cpp_name}'
    if func_decl.params:
        func_def += _generate_params_list(func_decl.params,
                                          func_decl.is_extend_method)
    if func_decl.docstring:
        func_def += f', {_generate_docstring(func_decl.docstring)}'
    func_def += ');'
    yield func_def
Example #3
0
def _generate_function(
    module_name: str,
    func_decl: ast_pb2.FuncDecl,
    capsule_types: Set[str],
    class_decl: Optional[ast_pb2.ClassDecl] = None,
) -> Generator[str, None, None]:
    """Generates pybind11 bindings code for ast_pb2.FuncDecl."""
    if operators.needs_operator_overloading(func_decl):
        yield from operators.generate_operator(module_name, func_decl)
    elif lambdas.needs_lambda(func_decl, capsule_types, class_decl):
        yield from lambdas.generate_lambda(module_name, func_decl,
                                           capsule_types, class_decl)
    else:
        yield from _generate_simple_function(module_name, func_decl,
                                             class_decl)
Example #4
0
def generate_from(
    module_name: str,
    func_decl: ast_pb2.FuncDecl,
    class_decl: Optional[ast_pb2.ClassDecl] = None
) -> Generator[str, None, None]:
    """Generates pybind11 bindings code for functions.

  Args:
    module_name: String containing the superclass name.
    func_decl: Function declaration in proto format.
    class_decl: Outer class declaration in proto format. None if the function is
      not a member of a class.

  Yields:
    pybind11 function bindings code.
  """
    if lambdas.needs_lambda(func_decl):
        yield from lambdas.generate_lambda(module_name, func_decl, class_decl)
    elif operators.needs_operator_overloading(func_decl):
        yield from operators.generate_operator(module_name, func_decl)
    else:
        yield from _generate_simple_function(module_name, func_decl,
                                             class_decl)