function: C/C++ function wrappers

C function wrapper

class pybindgen.function.CustomFunctionWrapper(function_name, wrapper_name, wrapper_body=None, flags=('METH_VARARGS', 'METH_KEYWORDS'))

Bases: pybindgen.function.Function

Adds a custom function wrapper. The custom wrapper must be prepared to support overloading, i.e. it must have an additional “PyObject **return_exception” parameter, and raised exceptions must be returned by this parameter.

Parameters:
  • function_name – name for function, Python side
  • wrapper_name – name of the C wrapper function
  • wrapper_body – if not None, the function wrapper is generated containing this parameter value as function body
NEEDS_OVERLOADING_INTERFACE = True
generate(code_sink, dummy_wrapper_name=None, extra_wrapper_params=())
generate_call(*args, **kwargs)
class pybindgen.function.Function(function_name, return_value, parameters, docstring=None, unblock_threads=None, template_parameters=(), custom_name=None, deprecated=False, foreign_cpp_namespace=None, throw=())

Bases: pybindgen.typehandlers.base.ForwardWrapperBase

Class that generates a wrapper to a C function.

Parameters:
  • function_name – name of the C function
  • return_value (L{ReturnValue}) – the function return value
  • parameters (list of L{Parameter}) – the function parameters
  • custom_name – an alternative name to give to this function at python-side; if omitted, the name of the function in the python module will be the same name as the function in C++ (minus namespace).
  • deprecated – deprecation state for this API: - False: Not deprecated - True: Deprecated - “message”: Deprecated, and deprecation warning contains the given message
  • foreign_cpp_namespace – if set, the function is assumed to belong to the given C++ namespace, regardless of the C++ namespace of the python module it will be added to.
  • throw (list of L{CppException}) – list of C++ exceptions that the function may throw
add_custodian_and_ward(custodian, ward, postcall=None)

Add a custodian/ward relationship to the function wrapper

A custodian/ward relationship is one where one object (custodian) keeps a references to another object (ward), thus keeping it alive. When the custodian is destroyed, the reference to the ward is released, allowing the ward to be freed if no other reference to it is being kept by the user code. Please note that custodian/ward manages the lifecycle of Python wrappers, not the C/C++ objects referenced by the wrappers. In most cases, the wrapper owns the C/C++ object, and so the lifecycle of the C/C++ object is also managed by this. However, there are cases when a Python wrapper does not own the underlying C/C++ object, only references it.

The custodian and ward objects are indicated by an integer with the following meaning:

  • C{-1}: the return value of the function
  • value > 0: the nth parameter of the function, starting at 1
Parameters:
  • custodian – number of the object that assumes the role of custodian
  • ward – number of the object that assumes the role of ward
  • postcall – if True, the relationship is added after the C function call, if False it is added before the call. If not given, the value False is assumed if the return value is not involved, else postcall=True is used.
clone()

Creates a semi-deep copy of this function wrapper. The returned function wrapper clone contains copies of all parameters, so they can be modified at will.

generate(code_sink, wrapper_name=None, extra_wrapper_params=())

Generates the wrapper code

Parameters:
  • code_sink – a CodeSink instance that will receive the generated code
  • wrapper_name – name of wrapper function
generate_call()

virtual method implementation; do not call

generate_declaration(code_sink, extra_wrapper_parameters=())
get_module()

Get the Module object this function belongs to

get_py_method_def(name)

Returns an array element to use in a PyMethodDef table. Should only be called after code generation.

Parameters:name – python function/method name
module

Get the Module object this function belongs to

set_module(module)

Set the Module object this function belongs to

class pybindgen.function.OverloadedFunction(wrapper_name)

Bases: pybindgen.overloading.OverloadedWrapper

Adds support for overloaded functions

wrapper_name – C/C++ name of the wrapper

ERROR_RETURN = 'return NULL;'
RETURN_TYPE = 'PyObject *'