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