1[chapter Concepts 2 [quickbook 1.7] 3] 4 5[section CallPolicies] 6[section Introduction] 7 8Models of the CallPolicies concept are used to specialize the behavior of Python callable objects 9generated by Boost.Python to wrapped C++ objects like function and member function pointers, 10providing three behaviors: 11 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 16 17[endsect] 18[section CallPolicies Composition] 19 20In order to allow the use of multiple models of CallPolicies in the same callable object, 21Boost.Python's CallPolicies class templates provide a chaining interface which allows them to be 22recursively composed. This interface takes the form of an optional template parameter, `Base`, which 23defaults to `default_call_policies`. By convention, the `precall` function of the `Base` is invoked after 24the `precall` function supplied by the `outer` template, and the `postcall` function of the `Base` is invoked 25before the `postcall` function of the `outer` template. If a `result_converter` is supplied by the `outer` 26template, it replaces any `result_converter` supplied by the `Base`. For an example, see 27`return_internal_reference`. 28 29[endsect] 30[section Concept Requirements] 31[table 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`.]] 41] 42[endsect] 43[endsect] 44[section Dereferenceable] 45[section Introduction] 46Instances of a `Dereferenceable` type can be used like a pointer to access an lvalue. 47[endsect] 48[section Concept Requirements] 49In the table below, `T` is a model of Dereferenceable, and `x` denotes an object of type `T`. In addition, all pointers are `Dereferenceable`. 50[table 51 [[Expression][Result][Operational Semantics]] 52 [[`get_pointer(x)`][convertible to `pointee<T>::type*`] 53 [`&*x`, or a null pointer ]] 54] 55[endsect] 56[endsect] 57[section Extractor] 58[section Introduction] 59An 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. 60[endsect] 61[section Concept Requirements] 62In the table below, `X` denotes a model of `Extractor` and `a` denotes an instance of a Python object type. 63[table 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`]] 69] 70[endsect] 71[section Notes] 72Informally, 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. 73[endsect] 74[endsect] 75[section HolderGenerator] 76[section Introduction] 77A HolderGenerator is a unary metafunction class which returns types suitable for holding instances of its argument in a wrapped C++ class instance. 78[endsect] 79[section Concept Requirements] 80In the table below, `G` denotes an type which models `HolderGenerator`, and `X` denotes a class type. 81[table 82 [[Expression][Requirements]] 83 [[`G::apply<X>::type`][A concrete subclass of `instance_holder` which can hold objects of type `X`. ]] 84] 85[endsect] 86[endsect] 87[section ResultConverter] 88[section Introduction] 89A 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. 90[endsect] 91[section ResultConverter Concept Requirements] 92In 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`. 93[table 94 [[Expression][Type][Semantics]] 95 [[`C c`][] 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.]] 103] 104[endsect] 105[section ResultConverterGenerator Concept Requirements] 106In the table below, `G` denotes a ResultConverterGenerator type and `R` denotes a possible C++ function return type. 107[table 108 [[Expression][Requirements]] 109 [[`G::apply<R>::type`][A ResultConverter type for `R`.]] 110] 111[endsect] 112[endsect] 113[section ObjectWrapper] 114[section Introduction] 115This 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. 116[endsect] 117[section ObjectWrapper Concept Requirements] 118Models 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). 120[endsect] 121[section TypeWrapper Concept Requirements] 122TypeWrapper 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)``. 123When 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. 124[endsect] 125[section Caveat] 126The 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: 127`` 128 >>> class mydict(dict): 129 ... def items(self): 130 ... return tuple(dict.items(self)) # return a tuple 131`` 132Since 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. 133[endsect] 134[endsect] 135