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
6 #include <boost/python/module.hpp>
7 #include <boost/python/def.hpp>
8 #include <boost/python/class.hpp>
9 #include <boost/python/list.hpp>
10 #include <boost/python/tuple.hpp>
11 #include <boost/python/dict.hpp>
12 #include <boost/python/make_function.hpp>
13 #include <boost/lexical_cast.hpp>
14 #define BOOST_ENABLE_ASSERT_HANDLER
15 #include <boost/assert.hpp>
16 #include "test_class.hpp"
17
18 using namespace boost::python;
19
new_list()20 object new_list()
21 {
22 return list();
23 }
24
listify(object x)25 list listify(object x)
26 {
27 return list(x);
28 }
29
listify_string(char const * s)30 object listify_string(char const* s)
31 {
32 return list(s);
33 }
34
x_rep(test_class<> const & x)35 std::string x_rep(test_class<> const& x)
36 {
37 return "X(" + boost::lexical_cast<std::string>(x.value()) + ")";
38 }
39
apply_object_list(object f,list x)40 object apply_object_list(object f, list x)
41 {
42 return f(x);
43 }
44
apply_list_list(object f,list x)45 list apply_list_list(object f, list x)
46 {
47 return call<list>(f.ptr(), x);
48 }
49
append_object(list & x,object y)50 void append_object(list& x, object y)
51 {
52 x.append(y);
53 }
54
append_list(list & x,list const & y)55 void append_list(list& x, list const& y)
56 {
57 x.append(y);
58 }
59
60 typedef test_class<> X;
61
notcmp(object const & x,object const & y)62 int notcmp(object const& x, object const& y)
63 {
64 return y < x ? -1 : y > x ? 1 : 0;
65 }
66
exercise(list x,object y,object print)67 void exercise(list x, object y, object print)
68 {
69 x.append(y);
70 x.append(5);
71 x.append(X(3));
72
73 print("after append:");
74 print(x);
75
76 print("number of", y, "instances:", x.count(y));
77
78 print("number of 5s:", x.count(5));
79
80 x.extend("xyz");
81 print("after extend:");
82 print(x);
83 print("index of", y, "is:", x.index(y));
84 print("index of 'l' is:", x.index("l"));
85
86 x.insert(4, 666);
87 print("after inserting 666:");
88 print(x);
89 print("inserting with object as index:");
90 x.insert(x[x.index(5)], "---");
91 print(x);
92
93 print("popping...");
94 x.pop();
95 print(x);
96 x.pop(x[x.index(5)]);
97 print(x);
98 x.pop(x.index(5));
99 print(x);
100
101 print("removing", y);
102 x.remove(y);
103 print(x);
104 print("removing", 666);
105 x.remove(666);
106 print(x);
107
108 print("reversing...");
109 x.reverse();
110 print(x);
111
112 print("sorted:");
113 x.pop(2); // make sorting predictable
114 x.pop(2); // remove [1,2] so the list is sortable in py3k
115 x.sort();
116 print(x);
117
118 print("reverse sorted:");
119 #if PY_VERSION_HEX >= 0x03000000
120 x.sort(*tuple(), **dict(make_tuple(make_tuple("reverse", true))));
121 #else
122 x.sort(¬cmp);
123 #endif
124 print(x);
125
126 list w;
127 w.append(5);
128 w.append(6);
129 w += "hi";
130 BOOST_ASSERT(w[0] == 5);
131 BOOST_ASSERT(w[1] == 6);
132 BOOST_ASSERT(w[2] == 'h');
133 BOOST_ASSERT(w[3] == 'i');
134 }
135
BOOST_PYTHON_MODULE(list_ext)136 BOOST_PYTHON_MODULE(list_ext)
137 {
138 def("new_list", new_list);
139 def("listify", listify);
140 def("listify_string", listify_string);
141 def("apply_object_list", apply_object_list);
142 def("apply_list_list", apply_list_list);
143
144 def("append_object", append_object);
145 def("append_list", append_list);
146
147 def("exercise", exercise);
148
149 class_<X>("X", init<int>())
150 .def( "__repr__", x_rep)
151 ;
152 }
153
154 #include "module_tail.cpp"
155