docstring.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/python/operators.hpp>
  6. #include <boost/python/class.hpp>
  7. #include <boost/python/module.hpp>
  8. #include <boost/python/def.hpp>
  9. #include <boost/python/docstring_options.hpp>
  10. #include <boost/python/scope.hpp>
  11. #include <boost/python/manage_new_object.hpp>
  12. #include "test_class.hpp"
  13. // Just use math.h here; trying to use std::pow() causes too much
  14. // trouble for non-conforming compilers and libraries.
  15. #include <math.h>
  16. using namespace boost::python;
  17. typedef test_class<> X;
  18. X* create(int x)
  19. {
  20. return new X(x);
  21. }
  22. unsigned long fact(unsigned long n)
  23. {
  24. return n <= 1 ? n : n * fact(n - 1);
  25. }
  26. BOOST_PYTHON_MODULE(docstring_ext)
  27. {
  28. scope().attr("__doc__") =
  29. "A simple test module for documentation strings\n"
  30. "Exercised by docstring.py"
  31. ;
  32. class_<X>("X",
  33. "A simple class wrapper around a C++ int\n"
  34. "includes some error-checking"
  35. , init<int>(
  36. "this is the __init__ function\n"
  37. "its documentation has two lines."
  38. , args("self", "value")
  39. )
  40. )
  41. .def("value", &X::value,
  42. "gets the value of the object"
  43. , args("self"))
  44. .def( "value", &X::value,
  45. "also gets the value of the object"
  46. , args("self"))
  47. ;
  48. def("create", create, return_value_policy<manage_new_object>(),
  49. "creates a new X object", args("value"));
  50. def("fact", fact, "compute the factorial", args("n"));
  51. {
  52. docstring_options doc_options;
  53. doc_options.disable_user_defined();
  54. def("fact_usr_off_1", fact, "usr off 1", args("n"));
  55. doc_options.enable_user_defined();
  56. def("fact_usr_on_1", fact, "usr on 1", args("n"));
  57. doc_options.disable_user_defined();
  58. def("fact_usr_off_2", fact, "usr off 2", args("n"));
  59. }
  60. def("fact_usr_on_2", fact, "usr on 2", args("n"));
  61. {
  62. docstring_options doc_options(true, false);
  63. def("fact_sig_off_1", fact, "sig off 1", args("n"));
  64. doc_options.enable_signatures();
  65. def("fact_sig_on_1", fact, "sig on 1", args("n"));
  66. doc_options.disable_signatures();
  67. def("fact_sig_off_2", fact, "sig off 2", args("n"));
  68. }
  69. def("fact_sig_on_2", fact, "sig on 2", args("n"));
  70. {
  71. docstring_options doc_options(false);
  72. def("fact_usr_off_sig_off_1", fact, "usr off sig off 1", args("n"));
  73. {
  74. docstring_options nested_doc_options;
  75. def("fact_usr_on_sig_on_1", fact, "usr on sig on 1", args("n"));
  76. nested_doc_options.disable_all();
  77. nested_doc_options.enable_user_defined();
  78. def("fact_usr_on_sig_off_1", fact, "usr on sig off 1", args("n"));
  79. nested_doc_options.enable_all();
  80. def("fact_usr_on_sig_on_2", fact, "usr on sig on 2", args("n"));
  81. }
  82. def("fact_usr_off_sig_off_2", fact, "usr off sig off 2", args("n"));
  83. }
  84. {
  85. docstring_options doc_options(true);
  86. doc_options.disable_cpp_signatures();
  87. def("fact_usr_on_psig_on_csig_off_1", fact, "usr on psig on csig off 1", args("n"));
  88. doc_options.enable_cpp_signatures();
  89. doc_options.disable_py_signatures();
  90. def("fact_usr_on_psig_off_csig_on_1", fact, "usr on psig off csig on 1", args("n"));
  91. doc_options.enable_py_signatures();
  92. doc_options.disable_user_defined();
  93. doc_options.disable_cpp_signatures();
  94. def("fact_usr_off_psig_on_csig_off_1", fact, "usr off psig on csig off 1", args("n"));
  95. doc_options.enable_cpp_signatures();
  96. doc_options.disable_py_signatures();
  97. def("fact_usr_off_psig_off_csig_on_1", fact, "usr off psig off csig on 1", args("n"));
  98. }
  99. }
  100. #include "module_tail.cpp"