docstring_options.qbk 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. [section boost/python/docstring_options.hpp]
  2. [section Introduction]
  3. Boost.Python supports user-defined docstrings with automatic appending of C++ signatures. These features are enabled by default. The class docstring_options is available to selectively suppress the user-defined docstrings, signatures, or both.
  4. [endsect]
  5. [section Class `docstring_options`]
  6. Controls the appearance of docstrings of wrapped functions and member functions for the life-time of the instance. The instances are noncopyable to eliminate the possibility of surprising side effects.
  7. ``namespace boost { namespace python {
  8. class docstring_options : boost::noncopyable
  9. {
  10. public:
  11. docstring_options(bool show_all=true);
  12. docstring_options(bool show_user_defined, bool show_signatures);
  13. docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures);
  14. ~docstring_options();
  15. void disable_user_defined();
  16. void enable_user_defined();
  17. void disable_signatures();
  18. void enable_signatures();
  19. void disable_py_signatures();
  20. void enable_py_signatures();
  21. void disable_cpp_signatures();
  22. void enable_cpp_signatures();
  23. void disable_all();
  24. void enable_all();
  25. };
  26. }}
  27. ``
  28. [endsect]
  29. [section Class dostring_options constructors]
  30. ``
  31. docstring_options(bool show_all=true);
  32. ``
  33. [variablelist
  34. [[Effects][Constructs a docstring_options 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 Python and C++ signatures are shown. If show_all is false the `__doc__` attributes are `None`.]]
  35. ]
  36. ``
  37. docstring_options(bool show_user_defined, bool show_signatures);
  38. ``
  39. [variablelist
  40. [[Effects][Constructs a `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_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`.]]
  41. ]
  42. ``
  43. docstring_options(bool show_user_defined, bool show_py_signatures, bool show_cpp_signatures);
  44. ``
  45. [variablelist
  46. [[Effects][Constructs a `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`.]]
  47. ]
  48. [endsect]
  49. [section Class docstring_options destructor]
  50. ``~docstring_options();``
  51. [variablelist
  52. [[Effects][Restores the previous state of the docstring options. In particular, if `docstring_options` instances are in nested C++ scopes the settings effective in the enclosing scope are restored. If the last `docstring_options` instance goes out of scope the default "all on" settings are restored.]]]
  53. [endsect]
  54. [section Class `docstring_options` modifier functions]
  55. ``
  56. void disable_user_defined();
  57. void enable_user_defined();
  58. void disable_signatures();
  59. void enable_signatures();
  60. void disable_py_signatures();
  61. void enable_py_signatures();
  62. void disable_cpp_signatures();
  63. void enable_cpp_signatures();
  64. void disable_all();
  65. void enable_all();
  66. ``
  67. These member functions dynamically change the appearance of docstrings in the code that follows. The `*_user_defined()` and `*_signatures()` member functions are provided for fine-grained control. The `*_all()` member functions are convenient shortcuts to manipulate all settings simultaneously.
  68. [endsect]
  69. [section Example]
  70. [section Docstring options defined at compile time]
  71. ``
  72. #include <boost/python/module.hpp>
  73. #include <boost/python/def.hpp>
  74. #include <boost/python/docstring_options.hpp>
  75. void foo() {}
  76. BOOST_PYTHON_MODULE(demo)
  77. {
  78. using namespace boost::python;
  79. docstring_options doc_options(DEMO_DOCSTRING_SHOW_ALL);
  80. def("foo", foo, "foo doc");
  81. }
  82. ``
  83. If compiled with `-DDEMO_DOCSTRING_SHOW_ALL=true`:
  84. ``
  85. >>> import demo
  86. >>> print demo.foo.__doc__
  87. foo() -> None : foo doc
  88. C++ signature:
  89. foo(void) -> void
  90. ``
  91. If compiled with `-DDEMO_DOCSTRING_SHOW_ALL=false`:
  92. ``
  93. >>> import demo
  94. >>> print demo.foo.__doc__
  95. None
  96. ``
  97. [endsect]
  98. [section Selective suppressions]
  99. ``
  100. #include <boost/python/module.hpp>
  101. #include <boost/python/def.hpp>
  102. #include <boost/python/args.hpp>
  103. #include <boost/python/docstring_options.hpp>
  104. int foo1(int i) { return i; }
  105. int foo2(long l) { return static_cast<int>(l); }
  106. int foo3(float f) { return static_cast<int>(f); }
  107. int foo4(double d) { return static_cast<int>(d); }
  108. BOOST_PYTHON_MODULE(demo)
  109. {
  110. using namespace boost::python;
  111. docstring_options doc_options;
  112. def("foo1", foo1, arg("i"), "foo1 doc");
  113. doc_options.disable_user_defined();
  114. def("foo2", foo2, arg("l"), "foo2 doc");
  115. doc_options.disable_signatures();
  116. def("foo3", foo3, arg("f"), "foo3 doc");
  117. doc_options.enable_user_defined();
  118. def("foo4", foo4, arg("d"), "foo4 doc");
  119. doc_options.enable_py_signatures();
  120. def("foo5", foo4, arg("d"), "foo5 doc");
  121. doc_options.disable_py_signatures();
  122. doc_options.enable_cpp_signatures();
  123. def("foo6", foo4, arg("d"), "foo6 doc");
  124. }
  125. ``
  126. Python code:
  127. ``
  128. >>> import demo
  129. >>> print demo.foo1.__doc__
  130. foo1( (int)i) -> int : foo1 doc
  131. C++ signature:
  132. foo1(int i) -> int
  133. >>> print demo.foo2.__doc__
  134. foo2( (int)l) -> int :
  135. C++ signature:
  136. foo2(long l) -> int
  137. >>> print demo.foo3.__doc__
  138. None
  139. >>> print demo.foo4.__doc__
  140. foo4 doc
  141. >>> print demo.foo5.__doc__
  142. foo5( (float)d) -> int : foo5 doc
  143. >>> print demo.foo6.__doc__
  144. foo6 doc
  145. C++ signature:
  146. foo6(double d) -> int
  147. ``
  148. [endsect]
  149. [section Wrapping from multiple C++ scopes]
  150. ``
  151. #include <boost/python/module.hpp>
  152. #include <boost/python/def.hpp>
  153. #include <boost/python/args.hpp>
  154. #include <boost/python/docstring_options.hpp>
  155. int foo1(int i) { return i; }
  156. int foo2(long l) { return static_cast<int>(l); }
  157. int bar1(int i) { return i; }
  158. int bar2(long l) { return static_cast<int>(l); }
  159. namespace {
  160. void wrap_foos()
  161. {
  162. using namespace boost::python;
  163. // no docstring_options here
  164. // -> settings from outer C++ scope are in effect
  165. def("foo1", foo1, arg("i"), "foo1 doc");
  166. def("foo2", foo2, arg("l"), "foo2 doc");
  167. }
  168. void wrap_bars()
  169. {
  170. using namespace boost::python;
  171. bool show_user_defined = true;
  172. bool show_signatures = false;
  173. docstring_options doc_options(show_user_defined, show_signatures);
  174. def("bar1", bar1, arg("i"), "bar1 doc");
  175. def("bar2", bar2, arg("l"), "bar2 doc");
  176. }
  177. }
  178. BOOST_PYTHON_MODULE(demo)
  179. {
  180. boost::python::docstring_options doc_options(false);
  181. wrap_foos();
  182. wrap_bars();
  183. }
  184. ``
  185. Python code:
  186. ``
  187. >>> import demo
  188. >>> print demo.foo1.__doc__
  189. None
  190. >>> print demo.foo2.__doc__
  191. None
  192. >>> print demo.bar1.__doc__
  193. bar1 doc
  194. >>> print demo.bar2.__doc__
  195. bar2 doc
  196. ``
  197. [endsect]
  198. [endsect]
  199. [endsect]