1 [section boost/python/pytype_function.hpp]
3 To support Pythonic signatures the converters should supply a `get_pytype` function returning a pointer to the associated `PyTypeObject`. See for example [link concepts.resultconverter `ResultConverter`] or [link to_from_python_type_conversion.boost_python_to_python_converter.class_template_to_python_convert.class_template_to_python_convert `to_python_converter`]. The classes in this header file are meant to be used when implmenting `get_pytype`. There are also `_direct` versions of the templates of `class T` which should be used with undecorated type parameter, expected to be in the conversion registry when the module loads.
5 [section Class `wrap_pytype`]
6 This template generates a static `get_pytype` member returning the template parameter.
8 namespace boost { namespace python { namespace converter{
10 template < PyTypeObject const *pytype >
14 static PyTypeObject const *get_pytype(){return pytype; }
20 [section Class `registered_pytype`]
21 This template should be used with template parameters which are (possibly decorated) types exported to python using [link high_level_components.boost_python_class_hpp.class_template_class_t_bases_hel `class_`]. The generated a static `get_pytype` member returns the corresponding python type.
23 namespace boost { namespace python { namespace converter{
26 class registered_pytype
29 static PyTypeObject const *get_pytype();
35 [section Class `expected_from_python_type`]
36 This template generates a static `get_pytype` member which inspects the registered `from_python` converters for the type `T` and returns a matching python type.
38 namespace boost { namespace python { namespace converter{
41 class expected_from_python_type
44 static PyTypeObject const *get_pytype();
50 [section Class `to_python_target_type`]
51 This template generates a static `get_pytype` member returning the python type to which `T` can be converted.
53 namespace boost { namespace python { namespace converter{
56 class to_python_target_type
59 static PyTypeObject const *get_pytype();
66 This example presumes that someone has implemented the standard noddy example module from the Python documentation, and placed the corresponding declarations in "noddy.h". Because `noddy_NoddyObject` is the ultimate trivial extension type, the example is a bit contrived: it wraps a function for which all information is contained in the type of its return value.
68 C++ module definition:
70 #include <boost/python/reference.hpp>
71 #include <boost/python/module.hpp>
75 tag make_tag() { return tag(); }
77 using namespace boost::python;
80 #if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
81 : wrap_pytype<&noddy_NoddyType> //inherits get_pytype from wrap_pytype
84 static PyObject* convert(tag const& x)
86 return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
90 BOOST_PYTHON_MODULE(to_python_converter)
92 def("make_tag", make_tag);
93 to_python_converter<tag, tag_to_noddy
94 #if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
97 >(); //"true" because tag_to_noddy has member get_pytype
100 The following example registers to and from python converters using the templates expected_from_python_type and to_pyhton_target_type.
102 #include <boost/python/module.hpp>
103 #include <boost/python/def.hpp>
104 #include <boost/python/extract.hpp>
105 #include <boost/python/to_python_converter.hpp>
106 #include <boost/python/class.hpp>
108 using namespace boost::python;
117 B(const A& a_):a(a_){}
120 // Converter from A to python int
122 #if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
123 : converter::to_python_target_type<A> //inherits get_pytype
126 static PyObject* convert(const B& b)
128 return incref(object(b.a).ptr());
132 // Conversion from python int to A
137 boost::python::converter::registry::push_back
141 #if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
142 , &converter::expected_from_python_type<A>::get_pytype//convertible to A can be converted to B
147 static void* convertible(PyObject* obj_ptr)
149 extract<const A&> ex(obj_ptr);
150 if (!ex.check()) return 0;
154 static void construct(
156 converter::rvalue_from_python_stage1_data* data)
159 (converter::rvalue_from_python_storage< B >*)data)-> storage.bytes;
161 extract<const A&> ex(obj_ptr);
162 new (storage) B(ex());
163 data->convertible = storage;
168 B func(const B& b) { return b ; }
170 BOOST_PYTHON_MODULE(pytype_function_ext)
172 to_python_converter< B , BToPython
173 #if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
176 >(); //has get_pytype
187 >>> from pytype_function_ext import *
188 >>> print func.__doc__
189 func( (A)arg1) -> A :
191 struct B func(struct B)