8 Models of the CallPolicies concept are used to specialize the behavior of Python callable objects
9 generated by Boost.Python to wrapped C++ objects like function and member function pointers,
10 providing three behaviors:
12 # `precall` - Python argument tuple management before the wrapped object is invoked
13 # `result_converter` - C++ return value handling
14 # `postcall` - Python argument tuple and result management after the wrapped object is invoked
15 # `extract_return_type` - metafunction for extracting the return type from a given signature type sequence
18 [section CallPolicies Composition]
20 In order to allow the use of multiple models of CallPolicies in the same callable object,
21 Boost.Python's CallPolicies class templates provide a chaining interface which allows them to be
22 recursively composed. This interface takes the form of an optional template parameter, `Base`, which
23 defaults to `default_call_policies`. By convention, the `precall` function of the `Base` is invoked after
24 the `precall` function supplied by the `outer` template, and the `postcall` function of the `Base` is invoked
25 before the `postcall` function of the `outer` template. If a `result_converter` is supplied by the `outer`
26 template, it replaces any `result_converter` supplied by the `Base`. For an example, see
27 `return_internal_reference`.
30 [section Concept Requirements]
32 [[Expression][Type][Result/Semantics]]
33 [[`x.precall(a)`][convertible to `bool`]
34 [returns `false` and `PyErr_Occurred() != 0` upon failure, `true` otherwise.]]
35 [[`P::result_converter`][A model of `ResultConverterGenerator`.]
36 [An MPL unary Metafunction Class used produce the "preliminary" result object.]]
37 [[`x.postcall(a, r)`][convertible to `PyObject*`]
38 [`0` and `PyErr_Occurred() != 0` upon failure. Must "conserve references" even in the event of an exception. In other words, if `r` is not returned, its reference count must be decremented; if another existing object is returned, its reference count must be incremented.]]
39 [[`P::extract_return_type`][A model of Metafunction.]
40 [An MPL unary Metafunction used extract the return type from a given signature. By default it is derived from `mpl::front`.]]
44 [section Dereferenceable]
45 [section Introduction]
46 Instances of a `Dereferenceable` type can be used like a pointer to access an lvalue.
48 [section Concept Requirements]
49 In the table below, `T` is a model of Dereferenceable, and `x` denotes an object of type `T`. In addition, all pointers are `Dereferenceable`.
51 [[Expression][Result][Operational Semantics]]
52 [[`get_pointer(x)`][convertible to `pointee<T>::type*`]
53 [`&*x`, or a null pointer ]]
58 [section Introduction]
59 An Extractor is a class which Boost.Python can use to extract C++ objects from Python objects, and is typically used by facilities that define `from_python` conversions for "traditional" Python extension types.
61 [section Concept Requirements]
62 In the table below, `X` denotes a model of `Extractor` and `a` denotes an instance of a Python object type.
64 [[Expression][Type][Semantics]]
65 [[`X::execute(a)`][non-void]
66 [Returns the C++ object being extracted. The execute function must not be overloaded.]]
67 [[`&a.ob_type`][`PyTypeObject**`]
68 [Points to the `ob_type` field of an object which is layout-compatible with `PyObject`]]
72 Informally, an Extractor's execute member must be a non-overloaded static function whose single argument is a Python object type. Acceptable Python object types include those publicly (and unambiguously) derived from PyObject, and POD types which are layout-compatible with PyObject.
75 [section HolderGenerator]
76 [section Introduction]
77 A HolderGenerator is a unary metafunction class which returns types suitable for holding instances of its argument in a wrapped C++ class instance.
79 [section Concept Requirements]
80 In the table below, `G` denotes an type which models `HolderGenerator`, and `X` denotes a class type.
82 [[Expression][Requirements]]
83 [[`G::apply<X>::type`][A concrete subclass of `instance_holder` which can hold objects of type `X`. ]]
87 [section ResultConverter]
88 [section Introduction]
89 A ResultConverter for a type `T` is a type whose instances can be used to convert C++ return values of type `T` `to_python`. A ResultConverterGenerator is an MPL unary metafunction class which, given the return type of a C++ function, returns a ResultConverter for that type. ResultConverters in Boost.Python generally inspect library's registry of converters to find a suitable converter, but converters which don't use the registry are also possible.
91 [section ResultConverter Concept Requirements]
92 In the table below, `C` denotes a ResultConverter type for a type `R`, `c` denotes an object of type `C`, and `r` denotes an object of type `R`.
94 [[Expression][Type][Semantics]]
96 [Constructs a `c` object.]]
97 [[`c.convertible()`][convertible to `bool`]
98 [`false` iff no conversion from any `R` value to a Python object is possible.]]
99 [[`c(r)`][convertible to `PyObject*`]
100 [A pointer to a Python object corresponding to `r`, or `0` iff `r` could not be converted `to_python`, in which case `PyErr_Occurred` should return non-zero.]]
101 [[`c.get_pytype()`][`PyTypeObject const *`]
102 [A pointer to a Python Type object corresponding to result of the conversion, or `0`. Used for documentation generation. If `0` is returned the generated type in the documentation will be object.]]
105 [section ResultConverterGenerator Concept Requirements]
106 In the table below, `G` denotes a ResultConverterGenerator type and `R` denotes a possible C++ function return type.
108 [[Expression][Requirements]]
109 [[`G::apply<R>::type`][A ResultConverter type for `R`.]]
113 [section ObjectWrapper]
114 [section Introduction]
115 This page defines two concepts used to describe classes which manage a Python objects, and which are intended to support usage with a Python-like syntax.
117 [section ObjectWrapper Concept Requirements]
118 Models of the ObjectWrapper concept have [link object_wrappers.boost_python_object_hpp.class_object object] as a publicly-accessible base class, and are used to supply special construction behavior and/or additional convenient functionality through (often templated) member functions. Except when the return type R is itself an [link concepts.objectwrapper.typewrapper_concept_requirements TypeWrapper], a member function invocation of the form ``x.some_function(a1, a2,...an)`` always has semantics equivalent to:
119 ``extract<R>(x.attr("some_function")(object(a1), object(a2),...object(an)))()`` (see [link concepts.objectwrapper.caveat caveat] below).
121 [section TypeWrapper Concept Requirements]
122 TypeWrapper is a refinement of [link concepts.objectwrapper.objectwrapper_concept_requiremen ObjectWrapper] which is associated with a particular Python type `X`. For a given TypeWrapper `T`, a valid constructor expression ``T(a1, a2,...an)`` builds a new T object managing the result of invoking X with arguments corresponding to ``object(a1), object(a2),...object(an)``.
123 When used as arguments to wrapped C++ functions, or as the template parameter to [link to_from_python_type_conversion.boost_python_extract_hpp.class_template_extract extract<>], only instances of the associated Python type will be considered a match.
126 The upshot of the special member function invocation rules when the return type is a TypeWrapper is that it is possible for the returned object to manage a Python object of an inappropriate type. This is not usually a serious problem; the worst-case result is that errors will be detected at runtime a little later than they might otherwise be. For an example of how this can occur, note that the [link object_wrappers.boost_python_dict_hpp.class_dict dict] member function `items` returns an object of type [link object_wrappers.boost_python_list_hpp.class_list list]. Now suppose the user defines this `dict` subclass in Python:
128 >>> class mydict(dict):
130 ... return tuple(dict.items(self)) # return a tuple
132 Since an instance of `mydict` is also an instance of `dict`, when used as an argument to a wrapped C++ function, [link object_wrappers.boost_python_dict_hpp.class_dict boost::python::dict] can accept objects of Python type `mydict`. Invoking `items()` on this object can result in an instance of [link object_wrappers.boost_python_list_hpp.class_list boost::python::list] which actually holds a Python `tuple`. Subsequent attempts to use `list` methods (e.g. `append`, or any other mutating operation) on this object will raise the same exception that would occur if you tried to do it from Python.