cppclass_typehandlers: type handlers for C++ classes (or C structures)

class pybindgen.cppclass_typehandlers.CppClassParameter(ctype, name, direction=1, is_const=False, default_value=None)

Bases: pybindgen.cppclass_typehandlers.CppClassParameterBase

Class parameter “by-value” handler

Parameters:
  • ctype – C type, normally ‘MyClass*’
  • name – parameter name
CTYPES = []
DIRECTIONS = [1]
convert_c_to_python(wrapper)

Write some code before calling the Python method.

convert_python_to_c(wrapper)

parses python args to get C++ value

cpp_class = <pybindgen.CppClass None>
class pybindgen.cppclass_typehandlers.CppClassParameterBase(ctype, name, direction=1, is_const=False, default_value=None)

Bases: pybindgen.typehandlers.base.Parameter

Base class for all C++ Class parameter handlers

Parameters:
  • ctype – C type, normally ‘MyClass*’
  • name – parameter name
CTYPES = []
DIRECTIONS = [1]
cpp_class = <pybindgen.CppClass None>
class pybindgen.cppclass_typehandlers.CppClassPtrParameter(ctype, name, direction=1, transfer_ownership=None, custodian=None, is_const=False, null_ok=False, default_value=None)

Bases: pybindgen.cppclass_typehandlers.CppClassParameterBase

Class* handlers

Type handler for a pointer-to-class parameter (MyClass*)

Parameters:
  • ctype – C type, normally ‘MyClass*’
  • name – parameter name
  • transfer_ownership – if True, the callee becomes responsible for freeing the object. If False, the caller remains responsible for the object. In either case, the original object pointer is passed, not a copy. In case transfer_ownership=True, it is invalid to perform operations on the object after the call (calling any method will cause a null pointer dereference and crash the program).
  • custodian

    if given, points to an object (custodian) that keeps the python wrapper for the parameter alive. Possible values are:

    • None: no object is custodian;
    • -1: the return value object;
    • 0: the instance of the method in which
      the ReturnValue is being used will become the custodian;
    • integer > 0: parameter number, starting at 1
      (i.e. not counting the self/this parameter), whose object will be used as custodian.
  • is_const – if true, the parameter has a const attached to the leftmost
  • null_ok – if true, None is accepted and mapped into a C NULL pointer
  • default_value – default parameter value (as C expression string); probably, the only default value that makes sense here is probably ‘NULL’.

Note

Only arguments which are instances of C++ classes wrapped by PyBindGen can be used as custodians.

CTYPES = []
DIRECTIONS = [1, 2, 3]
SUPPORTS_TRANSFORMATIONS = True
convert_c_to_python(wrapper)

foo

convert_python_to_c(wrapper)

parses python args to get C++ value

cpp_class = <pybindgen.CppClass None>
class pybindgen.cppclass_typehandlers.CppClassPtrReturnValue(ctype, caller_owns_return=None, custodian=None, is_const=False, reference_existing_object=None, return_internal_reference=None)

Bases: pybindgen.cppclass_typehandlers.CppClassReturnValueBase

Class* return handler

Parameters:
  • ctype – C type, normally ‘MyClass*’
  • caller_owns_return – if true, ownership of the object pointer is transferred to the caller
  • custodian

    bind the life cycle of the python wrapper for the return value object (ward) to that of the object indicated by this parameter (custodian). Possible values are:

    • None: no object is custodian;
    • 0: the instance of the method in which
      the ReturnValue is being used will become the custodian;
    • integer > 0: parameter number, starting at 1
      (i.e. not counting the self/this parameter), whose object will be used as custodian.
  • reference_existing_object – if true, ownership of the pointed-to object remains to be the caller’s, but we do not make a copy. The callee gets a reference to the existing object, but is not responsible for freeing it. Note that using this memory management style is dangerous, as it exposes the Python programmer to the possibility of keeping a reference to an object that may have been deallocated in the mean time. Calling methods on such an object would lead to a memory error.
  • return_internal_reference – like reference_existing_object, but additionally adds custodian/ward to bind the lifetime of the ‘self’ object (instance the method is bound to) to the lifetime of the return value.

Note

Only arguments which are instances of C++ classes wrapped by PyBindGen can be used as custodians.

CTYPES = []
SUPPORTS_TRANSFORMATIONS = True
convert_c_to_python(wrapper)

See ReturnValue.convert_c_to_python

convert_python_to_c(wrapper)

See ReturnValue.convert_python_to_c

cpp_class = <pybindgen.CppClass None>
get_c_error_return()

See ReturnValue.get_c_error_return

class pybindgen.cppclass_typehandlers.CppClassRefParameter(ctype, name, direction=1, is_const=False, default_value=None, default_value_type=None)

Bases: pybindgen.cppclass_typehandlers.CppClassParameterBase

Class& handlers

Parameters:
  • ctype – C type, normally ‘MyClass*’
  • name – parameter name
CTYPES = []
DIRECTIONS = [1, 2, 3]
convert_c_to_python(wrapper)

Write some code before calling the Python method.

convert_python_to_c(wrapper)

parses python args to get C++ value

cpp_class = <pybindgen.CppClass None>
class pybindgen.cppclass_typehandlers.CppClassRefReturnValue(ctype, is_const=False, caller_owns_return=False, reference_existing_object=None, return_internal_reference=None)

Bases: pybindgen.cppclass_typehandlers.CppClassReturnValueBase

Class return handlers

CTYPES = []
REQUIRES_ASSIGNMENT_CONSTRUCTOR = True
convert_c_to_python(wrapper)

see ReturnValue.convert_c_to_python

convert_python_to_c(wrapper)

see ReturnValue.convert_python_to_c

cpp_class = <pybindgen.CppClass None>
get_c_error_return()

See ReturnValue.get_c_error_return

class pybindgen.cppclass_typehandlers.CppClassReturnValue(ctype, is_const=False)

Bases: pybindgen.cppclass_typehandlers.CppClassReturnValueBase

Class return handlers

override to fix the ctype parameter with namespace information

CTYPES = []
REQUIRES_ASSIGNMENT_CONSTRUCTOR = True
convert_c_to_python(wrapper)

see ReturnValue.convert_c_to_python

convert_python_to_c(wrapper)

see ReturnValue.convert_python_to_c

cpp_class = <pybindgen.CppClass None>
get_c_error_return()

See ReturnValue.get_c_error_return

class pybindgen.cppclass_typehandlers.CppClassReturnValueBase(ctype, is_const=False)

Bases: pybindgen.typehandlers.base.ReturnValue

Class return handlers – base class

CTYPES = []
cpp_class = <pybindgen.CppClass None>
pybindgen.cppclass_typehandlers.common_shared_object_return(value, py_name, cpp_class, code_block, type_traits, caller_owns_return, reference_existing_object, type_is_pointer)
pybindgen.cppclass_typehandlers.implement_parameter_custodians_postcall(wrapper)
pybindgen.cppclass_typehandlers.implement_parameter_custodians_precall(wrapper)
pybindgen.cppclass_typehandlers.scan_custodians_and_wards(wrapper)

Scans the return value and parameters for custodian/ward options, converts them to add_custodian_and_ward API calls. Wrappers that implement custodian_and_ward are: CppMethod, Function, and CppConstructor.

Previous topic

typehandlers.base: abstract base classes for type handlers and wrapper generators

Next topic

typehandlers.codesink: classes that receive generated source code

This Page