docstring_options.hpp header
+ control the content of docstrings.
+
+ to_python_converter.hpp,
+ default_call_policies,
+ ResultConverter,
+ CallPolicies and some others.
+ Efforts were made not to have interface breaking changes.
+ postcall - Python argument tuple and result management
after the wrapped object is invokedextract_return_type - metafunction for extracting the return type from a given signature type sequenceP::extract_return_typeR.
href="http://www.python.org/doc/current/api/exceptionHandling.html#l2h-71">PyErr_Occurred
should return non-zero.
+ c.get_pytype()PyTypeObject const*0. Used for documentation generation. If 0 is returned
+ the generated type in the documentation will be object .typeid(T).name() instead of using and comparing
the std::type_info objects directly.
+ BOOST_PYTHON_NO_PY_SIGNATURESdocstring_options.enable_py_signatures() is set to false.
+ BOOST_PYTHON_SUPPORTS_PY_SIGNATURESBOOST_PYTHON_NO_PY_SIGNATURES is undefinedBOOST_PYTHON_PY_SIGNATURES_PROPER_INIT_SELF_TYPE__init__ method "self" parameters
+ is properly generated, otherwise object is used. It is undefined
+ by default because it increases the binary size of the module by about 14% (gcc compiled).Revised
- 13 November, 2002
+ 11 June, 2007
diff --git a/doc/v2/docstring_options.html b/doc/v2/docstring_options.html
index 0e4a9cde..a6e9041e 100644
--- a/doc/v2/docstring_options.html
+++ b/doc/v2/docstring_options.html
@@ -103,6 +103,8 @@ namespace boost { namespace python {
docstring_options(bool show_user_defined, bool show_signatures);
+ docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures);
+
~docstring_options();
void
@@ -117,6 +119,18 @@ namespace boost { namespace python {
void
enable_signatures();
+ void
+ disable_py_signatures();
+
+ void
+ enable_py_signatures();
+
+ void
+ disable_cpp_signatures();
+
+ void
+ enable_cpp_signatures();
+
void
disable_all();
@@ -139,7 +153,7 @@ docstring_options(bool show_all=true);
object which controls the appearance of function and
member-function docstrings defined in the code that follows. If
show_all is true, both the
- user-defined docstrings and the automatically generated C++
+ user-defined docstrings and the automatically generated Python and C++
signatures are shown. If show_all is
false the __doc__ attributes are
None.
@@ -154,12 +168,29 @@ docstring_options(bool show_user_defined, bool show_signatures);
member-function docstrings defined in the code that follows.
Iff show_user_defined is true, the
user-defined docstrings are shown. Iff
- show_signatures is true, C++
+ show_signatures is true, Python and C++
signatures are automatically added. If both
show_user_defined and show_signatures
are false, the __doc__ attributes are
None.
+docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures); ++ +
docstring_options
+ object which controls the appearance of function and
+ member-function docstrings defined in the code that follows.
+ Iff show_user_defined is true, the
+ user-defined docstrings are shown. Iff
+ show_py_signatures is true, Python
+ signatures are automatically added. Iff
+ show_cpp_signatures is true, C++
+ signatures are automatically added. If all parameters are
+ false, the __doc__ attributes are
+ None.*_user_defined() and *_signatures()
member functions are provided for fine-grained control. The
*_all() member functions are convenient shortcuts
- to manipulate both settings simultaneously.
+ to manipulate all settings simultaneously.
>>> import demo
>>> print demo.foo.__doc__
-foo doc
+foo() -> None : foo doc
C++ signature:
foo(void) -> void
If compiled with
@@ -253,21 +288,33 @@ BOOST_PYTHON_MODULE(demo)
def("foo3", foo3, arg("f"), "foo3 doc");
doc_options.enable_user_defined();
def("foo4", foo4, arg("d"), "foo4 doc");
+ doc_options.enable_py_signatures();
+ def("foo5", foo4, arg("d"), "foo5 doc");
+ doc_options.disable_py_signatures();
+ doc_options.enable_cpp_signatures();
+ def("foo6", foo4, arg("d"), "foo6 doc");
}
Python code:
>>> import demo
>>> print demo.foo1.__doc__
-foo1 doc
+foo1( (int)i) -> int : foo1 doc
C++ signature:
foo1(int i) -> int
>>> print demo.foo2.__doc__
+foo2( (int)l) -> int :
C++ signature:
foo2(long l) -> int
>>> print demo.foo3.__doc__
None
>>> print demo.foo4.__doc__
foo4 doc
+>>> print demo.foo5.__doc__
+foo5( (float)d) -> int : foo5 doc
+>>> print demo.foo6.__doc__
+foo6 doc
+C++ signature:
+ foo6(double d) -> int
enum_
constructors-enum_(char const* name); +enum_(char const* name, char const* doc=0);
x to the type's dictionary as the
- named attributenamed attribute.
*thisscope with the
same names and values as all enumeration values exposed so far
- by calling value()value().
*this|
+ |
+
+
+ Boost.Python+ +Header + <boost/python/object/function_doc_signature.hpp>+ |
+
function_doc_signature_generatorfunction_doc_signature_generator synopsisBoost.Python supports docstrings with automatic
+ appending of Pythonic and C++ signatures. This feature is implemented
+ by class function_doc_signature_generator
+ The class uses all of the overloads, supplied arg names and default values, as well as
+ the user-defined docstrings, to generate documentation for a given function.
function_doc_signature_generator+ The class has only one public function which returns a list of strings documenting the + overloads of a function. +
+ +function_doc_signature_generator synopsis
+namespace boost { namespace python { namespace objects {
+
+ class function_doc_signature_generator
+ {
+ public:
+ static list function_doc_signatures(function const *f);
+ };
+
+}}}
+
+
+
+ function_doc_signature_generator
+#include <boost/python/module.hpp>
+#include <boost/python/def.hpp>
+#include <boost/python/args.hpp>
+#include <boost/python/tuple.hpp>
+#include <boost/python/class.hpp>
+#include <boost/python/overloads.hpp>
+#include <boost/python/raw_function.hpp>
+
+using namespace boost::python;
+
+tuple f(int x = 1, double y = 4.25, char const* z = "wow")
+{
+ return make_tuple(x, y, z);
+}
+
+BOOST_PYTHON_FUNCTION_OVERLOADS(f_overloads, f, 0, 3)
+
+
+struct X
+{
+ tuple f(int x = 1, double y = 4.25, char const* z = "wow")
+ {
+ return make_tuple(x, y, z);
+ }
+};
+
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_f_overloads, X::f, 0, 3)
+
+tuple raw_func(tuple args, dict kw)
+{
+ return make_tuple(args, kw);
+}
+
+BOOST_PYTHON_MODULE(args_ext)
+{
+ def("f", f, (arg("x")=1, arg("y")=4.25, arg("z")="wow")
+ , "This is f's docstring"
+ );
+
+ def("raw", raw_function(raw_func));
+
+ def("f1", f, f_overloads("f1's docstring", args("x", "y", "z")));
+
+
+ class_<X>("X", "This is X's docstring", init<>(args("self")))
+ .def("f", &X::f
+ , "This is X.f's docstring"
+ , args("self","x", "y", "z"))
+
+ ;
+
+}
+
+
+Python code:
+
+>>> import args_ext
+>>> help(args_ext)
+Help on module args_ext:
+
+NAME
+ args_ext
+
+FILE
+ args_ext.pyd
+
+CLASSES
+ Boost.Python.instance(__builtin__.object)
+ X
+
+ class X(Boost.Python.instance)
+ | This is X's docstring
+ |
+ | Method resolution order:
+ | X
+ | Boost.Python.instance
+ | __builtin__.object
+ |
+ | Methods defined here:
+ |
+ | __init__(...)
+ | __init__( (object)self) -> None :
+ | C++ signature:
+ | void __init__(struct _object *)
+ |
+ | f(...)
+ | f( (X)self, (int)x, (float)y, (str)z) -> tuple : This is X.f's docstring
+ | C++ signature:
+ | class boost::python::tuple f(struct X {lvalue},int,double,char const *)
+ |
+ | .................
+ |
+FUNCTIONS
+ f(...)
+ f([ (int)x=1 [, (float)y=4.25 [, (str)z='wow']]]) -> tuple : This is f's docstring
+ C++ signature:
+ class boost::python::tuple f([ int=1 [,double=4.25 [,char const *='wow']]])
+
+ f1(...)
+ f1([ (int)x [, (float)y [, (str)z]]]) -> tuple : f1's docstring
+ C++ signature:
+ class boost::python::tuple f1([ int [,double [,char const *]]])
+
+ raw(...)
+ object raw(tuple args, dict kwds) :
+ C++ signature:
+ object raw(tuple args, dict kwds)
+
+
+
+
+ © Copyright Nikolay Mladenov 2007.
+ + diff --git a/doc/v2/pytype_function.html b/doc/v2/pytype_function.html new file mode 100644 index 00000000..fcc2a7f9 --- /dev/null +++ b/doc/v2/pytype_function.html @@ -0,0 +1,370 @@ + + + + + + + + + + +|
+ |
+
+
+ Boost.Python+ +Header + <boost/python/converter/pytype_function.hpp>+ |
+
wrap_pytypewrap_pytype synopsisregistered_pytyperegistered_pytype synopsisexpected_from_python_typeexpected_from_python_type synopsisto_python_target_typeto_python_target_type synopsisTo support Pythonic signatures the converters should supply a get_pytype function
+ returning a pointer to the associated PyTypeObject. See for example
+ ResultConverter or
+ 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.
+
wrap_pytype
+ This template generates a static get_pytype member returning the template parameter.
+
wrap_pytype synopsis
+namespace boost { namespace python { namespace converter{
+
+ template < PyTypeObject const *pytype >
+ class wrap_pytype
+ {
+ public:
+ static PyTypeObject const *get_pytype(){return pytype; }
+ };
+
+}}}
+
+
+
+ registered_pytype
+ This template should be used with template parameters which are (possibly decorated)
+ types exported to python using class_.
+ The generated a static get_pytype member
+ returns the corresponding python type.
+
registered_pytype synopsis
+namespace boost { namespace python { namespace converter{
+
+ template < class T >
+ class registered_pytype
+ {
+ public:
+ static PyTypeObject const *get_pytype();
+ };
+
+}}}
+
+
+
+ expected_from_python_type
+ 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.
+
expected_from_python_type synopsis
+namespace boost { namespace python { namespace converter{
+
+ template < class T >
+ class expected_from_python_type
+ {
+ public:
+ static PyTypeObject const *get_pytype();
+ };
+
+}}}
+
+
+
+ to_python_target_type
+ This template generates a static get_pytype member returning the
+ python type to which T can be converted.
+
to_python_target_type synopsis
+namespace boost { namespace python { namespace converter{
+
+ template < class T >
+ class to_python_target_type
+ {
+ public:
+ static PyTypeObject const *get_pytype();
+ };
+
+}}}
+
+
+
+ "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.
+
+
+#include <boost/python/reference.hpp>
+#include <boost/python/module.hpp>
+#include "noddy.h"
+
+struct tag {};
+tag make_tag() { return tag(); }
+
+using namespace boost::python;
+
+struct tag_to_noddy
+#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
+: wrap_pytype<&noddy_NoddyType> //inherits get_pytype from wrap_pytype
+#endif
+{
+ static PyObject* convert(tag const& x)
+ {
+ return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
+ }
+};
+
+BOOST_PYTHON_MODULE(to_python_converter)
+{
+ def("make_tag", make_tag);
+ to_python_converter<tag, tag_to_noddy
+#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
+ , true
+#endif
+ >(); //"true" because tag_to_noddy has member get_pytype
+}
+
+
+
+The following example registers to and from python converters using the templates
+expected_from_python_type and to_pyhton_target_type.
+
+#include <boost/python/module.hpp>
+#include <boost/python/def.hpp>
+#include <boost/python/extract.hpp>
+#include <boost/python/to_python_converter.hpp>
+#include <boost/python/class.hpp>
+
+using namespace boost::python;
+
+struct A
+{
+};
+
+struct B
+{
+ A a;
+ B(const A& a_):a(a_){}
+};
+
+// Converter from A to python int
+struct BToPython
+#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //unnecessary overhead if py signatures are not supported
+ : converter::to_python_target_type<A> //inherits get_pytype
+#endif
+{
+ static PyObject* convert(const B& b)
+ {
+ return incref(object(b.a).ptr());
+ }
+};
+
+// Conversion from python int to A
+struct BFromPython
+{
+ BFromPython()
+ {
+ boost::python::converter::registry::push_back
+ ( &convertible
+ , &construct
+ , type_id< B >()
+#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
+ , &converter::expected_from_python_type<A>::get_pytype//convertible to A can be converted to B
+#endif
+ );
+ }
+
+ static void* convertible(PyObject* obj_ptr)
+ {
+ extract<const A&> ex(obj_ptr);
+ if (!ex.check()) return 0;
+ return obj_ptr;
+ }
+
+ static void construct(
+ PyObject* obj_ptr,
+ converter::rvalue_from_python_stage1_data* data)
+ {
+ void* storage = (
+ (converter::rvalue_from_python_storage< B >*)data)-> storage.bytes;
+
+ extract<const A&> ex(obj_ptr);
+ new (storage) B(ex());
+ data->convertible = storage;
+ }
+};
+
+
+B func(const B& b) { return b ; }
+
+BOOST_PYTHON_MODULE(pytype_function_ext)
+{
+ to_python_converter< B , BToPython
+#if defined BOOST_PYTHON_SUPPORTS_PY_SIGNATURES //invalid if py signatures are not supported
+ ,true
+#endif
+ >(); //has get_pytype
+ BFromPython();
+
+ class_<A>("A") ;
+
+ def("func", &func);
+
+}
+
+
+
+>>> from pytype_function_ext import *
+>>> print func.__doc__
+func( (A)arg1) -> A :
+ C++ signature:
+ struct B func(struct B)
+
+
+
+ © Copyright Nikolay Mladenov 2007.
+ + diff --git a/doc/v2/reference.html b/doc/v2/reference.html index 06a76d2c..7d8252fa 100644 --- a/doc/v2/reference.html +++ b/doc/v2/reference.html @@ -609,6 +609,66 @@convert
does the real work of the conversion.bool has_get_pytype = falsePyTypeObject const * p = Conversion::get_pytype() .Conversion has get_pytype member supply
+ true for this parameters.
+ If present get_pytype is used to document the return type
+ of functions using this conversion. The get_pytype may be implemented
+ using the classes and functions
+ from pytype_function.hpp
+ NOTE : For backward compatibility this parameter may be passed after
+ checking if BOOST_PYTHON_SUPPORTS_PY_SIGNATURES is defined (see
+ here).
+
namespace boost { namespace python
{
- template <class T, class Conversion>
+ template <class T, class Conversion, bool convertion_has_get_pytype_member=false>
struct to_python_converter
{
to_python_converter();
@@ -160,12 +177,16 @@ struct tag_to_noddy
{
return PyObject_New(noddy_NoddyObject, &noddy_NoddyType);
}
+ static PyTypeObject const* get_pytype()
+ {
+ return &noddy_NoddyType;
+ }
};
BOOST_PYTHON_MODULE(to_python_converter)
{
def("make_tag", make_tag);
- to_python_converter<tag, tag_to_noddy>();
+ to_python_converter<tag, tag_to_noddy, true>(); //"true" because tag_to_noddy has member get_pytype
}
@@ -195,7 +216,7 @@ BOOST_PYTHON_MODULE(to_python_converter)
Revised - 13 November, 2002 + 11 June, 2007
diff --git a/example/std_pair.cpp b/example/std_pair.cpp index 4a696ba5..edf98dd6 100644 --- a/example/std_pair.cpp +++ b/example/std_pair.cpp @@ -18,6 +18,7 @@ namespace { // Avoid cluttering the global namespace. return boost::python::incref( boost::python::make_tuple(p.first, p.second).ptr()); } + static PyTypeObject const *get_pytype () {return &PyTuple_Type; } }; // Helper for convenience. @@ -28,7 +29,9 @@ namespace { // Avoid cluttering the global namespace. { boost::python::to_python_converter< std::pair