| [29] | 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/module.hpp> |
|---|
| 6 | #include <boost/python/def.hpp> |
|---|
| 7 | #include <boost/python/object.hpp> |
|---|
| 8 | #include <boost/python/class.hpp> |
|---|
| 9 | |
|---|
| 10 | using namespace boost::python; |
|---|
| 11 | |
|---|
| 12 | class NotCopyable |
|---|
| 13 | { |
|---|
| 14 | } not_copyable; |
|---|
| 15 | |
|---|
| 16 | object ref_to_noncopyable() |
|---|
| 17 | { |
|---|
| 18 | return object(boost::ref(not_copyable)); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | object call_object_3(object f) |
|---|
| 22 | { |
|---|
| 23 | return f(3); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | object message() |
|---|
| 27 | { |
|---|
| 28 | return object("hello, world!"); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | object number() |
|---|
| 32 | { |
|---|
| 33 | return object(42); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | object obj_getattr(object x, char const* name) |
|---|
| 37 | { |
|---|
| 38 | return x.attr(name); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | object obj_const_getattr(object const& x, char const* name) |
|---|
| 42 | { |
|---|
| 43 | return x.attr(name); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void obj_setattr(object x, char const* name, object value) |
|---|
| 47 | { |
|---|
| 48 | x.attr(name) = value; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void obj_setattr42(object x, char const* name) |
|---|
| 52 | { |
|---|
| 53 | x.attr(name) = 42; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void obj_moveattr(object& x, char const* src, char const* dst) |
|---|
| 57 | { |
|---|
| 58 | x.attr(dst) = x.attr(src); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | object obj_getitem(object x, object key) |
|---|
| 62 | { |
|---|
| 63 | return x[key]; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | object obj_getitem3(object x) |
|---|
| 67 | { |
|---|
| 68 | return x[3]; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | object obj_const_getitem(object const& x, object key) |
|---|
| 72 | { |
|---|
| 73 | return x[key]; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void obj_setitem(object x, object key, object value) |
|---|
| 77 | { |
|---|
| 78 | x[key] = value; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void obj_setitem42(object x, object key) |
|---|
| 82 | { |
|---|
| 83 | x[key] = 42; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void obj_moveitem(object& x, object src, object dst) |
|---|
| 87 | { |
|---|
| 88 | x[dst] = x[src]; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void obj_moveitem2(object const& x_src, object k_src, object& x_dst, object k_dst) |
|---|
| 92 | { |
|---|
| 93 | x_dst[k_dst] = x_src[k_src]; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | bool test(object y) |
|---|
| 97 | { |
|---|
| 98 | return y; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | bool test_not(object y) |
|---|
| 102 | { |
|---|
| 103 | return !y; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | bool test_attr(object y, char* name) |
|---|
| 107 | { |
|---|
| 108 | return y.attr(name); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | bool test_not_attr(object y, char* name) |
|---|
| 112 | { |
|---|
| 113 | return !y.attr(name); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | bool test_item(object y, object key) |
|---|
| 117 | { |
|---|
| 118 | return y[key]; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | bool test_not_item(object y, object key) |
|---|
| 122 | { |
|---|
| 123 | return !y[key]; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | bool check_string_slice() |
|---|
| 127 | { |
|---|
| 128 | object s("hello, world"); |
|---|
| 129 | |
|---|
| 130 | if (s.slice(_,-3) != "hello, wo") |
|---|
| 131 | return false; |
|---|
| 132 | |
|---|
| 133 | if (s.slice(-3,_) != "rld") |
|---|
| 134 | return false; |
|---|
| 135 | |
|---|
| 136 | if (s.slice(_,_) != s) |
|---|
| 137 | return false; |
|---|
| 138 | |
|---|
| 139 | if (", " != s.slice(5,7)) |
|---|
| 140 | return false; |
|---|
| 141 | |
|---|
| 142 | return s.slice(2,-1).slice(1,-1) == "lo, wor"; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | bool check_binary_operators() |
|---|
| 146 | { |
|---|
| 147 | int y; |
|---|
| 148 | |
|---|
| 149 | object x(3); |
|---|
| 150 | |
|---|
| 151 | #define TEST_BINARY(op) \ |
|---|
| 152 | for (y = 1; y < 6; ++y) \ |
|---|
| 153 | { \ |
|---|
| 154 | if ((x op y) != (3 op y)) \ |
|---|
| 155 | return false; \ |
|---|
| 156 | } \ |
|---|
| 157 | for (y = 1; y < 6; ++y) \ |
|---|
| 158 | { \ |
|---|
| 159 | if ((y op x) != (y op 3)) \ |
|---|
| 160 | return false; \ |
|---|
| 161 | } \ |
|---|
| 162 | for (y = 1; y < 6; ++y) \ |
|---|
| 163 | { \ |
|---|
| 164 | object oy(y); \ |
|---|
| 165 | if ((oy op x) != (oy op 3)) \ |
|---|
| 166 | return false; \ |
|---|
| 167 | } |
|---|
| 168 | TEST_BINARY(>) |
|---|
| 169 | TEST_BINARY(>=) |
|---|
| 170 | TEST_BINARY(<) |
|---|
| 171 | TEST_BINARY(<=) |
|---|
| 172 | TEST_BINARY(==) |
|---|
| 173 | TEST_BINARY(!=) |
|---|
| 174 | |
|---|
| 175 | TEST_BINARY(+) |
|---|
| 176 | TEST_BINARY(-) |
|---|
| 177 | TEST_BINARY(*) |
|---|
| 178 | TEST_BINARY(/) |
|---|
| 179 | TEST_BINARY(%) |
|---|
| 180 | TEST_BINARY(<<) |
|---|
| 181 | TEST_BINARY(>>) |
|---|
| 182 | TEST_BINARY(&) |
|---|
| 183 | TEST_BINARY(^) |
|---|
| 184 | TEST_BINARY(|) |
|---|
| 185 | return true; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | bool check_inplace(object l, object o) |
|---|
| 189 | { |
|---|
| 190 | int y; |
|---|
| 191 | #define TEST_INPLACE(op) \ |
|---|
| 192 | for (y = 1; y < 6; ++y) \ |
|---|
| 193 | { \ |
|---|
| 194 | object x(666); \ |
|---|
| 195 | x op##= y; \ |
|---|
| 196 | if (x != (666 op y)) \ |
|---|
| 197 | return false; \ |
|---|
| 198 | } \ |
|---|
| 199 | for (y = 1; y < 6; ++y) \ |
|---|
| 200 | { \ |
|---|
| 201 | object x(666); \ |
|---|
| 202 | x op##= object(y); \ |
|---|
| 203 | if (!(x == (666 op y))) \ |
|---|
| 204 | return false; \ |
|---|
| 205 | } |
|---|
| 206 | TEST_INPLACE(+) |
|---|
| 207 | TEST_INPLACE(-) |
|---|
| 208 | TEST_INPLACE(*) |
|---|
| 209 | TEST_INPLACE(/) |
|---|
| 210 | TEST_INPLACE(%) |
|---|
| 211 | TEST_INPLACE(<<) |
|---|
| 212 | TEST_INPLACE(>>) |
|---|
| 213 | TEST_INPLACE(&) |
|---|
| 214 | TEST_INPLACE(^) |
|---|
| 215 | TEST_INPLACE(|) |
|---|
| 216 | |
|---|
| 217 | l += l; |
|---|
| 218 | for (y = 0; y < 6; ++y) |
|---|
| 219 | { |
|---|
| 220 | if (l[y] != y % 3) |
|---|
| 221 | return false; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | #define TEST_ITEM_INPLACE(index, op, n, r1, r2) \ |
|---|
| 225 | l[index] op##= n; \ |
|---|
| 226 | if (l[index] != r1) \ |
|---|
| 227 | return false; \ |
|---|
| 228 | l[index] op##= object(n); \ |
|---|
| 229 | if (!(l[index] == r2)) \ |
|---|
| 230 | return false; |
|---|
| 231 | |
|---|
| 232 | TEST_ITEM_INPLACE(0,+,7,7,14) |
|---|
| 233 | TEST_ITEM_INPLACE(1,-,2,-1,-3) |
|---|
| 234 | TEST_ITEM_INPLACE(2,*,3,6,18) |
|---|
| 235 | TEST_ITEM_INPLACE(2,/,2,9,4) |
|---|
| 236 | TEST_ITEM_INPLACE(0,%,4,2,2) |
|---|
| 237 | l[0] += 1; |
|---|
| 238 | TEST_ITEM_INPLACE(0,<<,2,12,48) |
|---|
| 239 | TEST_ITEM_INPLACE(0,>>,1,24,12) |
|---|
| 240 | l[4] = 15; |
|---|
| 241 | TEST_ITEM_INPLACE(4,&,(16+4+1),5,5) |
|---|
| 242 | TEST_ITEM_INPLACE(0,^,1,13,12) |
|---|
| 243 | TEST_ITEM_INPLACE(0,|,1,13,13) |
|---|
| 244 | |
|---|
| 245 | o.attr("x0") = 0; |
|---|
| 246 | o.attr("x1") = 1; |
|---|
| 247 | o.attr("x2") = 2; |
|---|
| 248 | o.attr("x3") = 0; |
|---|
| 249 | o.attr("x4") = 1; |
|---|
| 250 | |
|---|
| 251 | #define TEST_ATTR_INPLACE(index, op, n, r1, r2) \ |
|---|
| 252 | o.attr("x" #index) op##= n; \ |
|---|
| 253 | if (o.attr("x" #index) != r1) \ |
|---|
| 254 | return false; \ |
|---|
| 255 | o.attr("x" #index) op##= object(n); \ |
|---|
| 256 | if (o.attr("x" #index) != r2) \ |
|---|
| 257 | return false; |
|---|
| 258 | |
|---|
| 259 | TEST_ATTR_INPLACE(0,+,7,7,14) |
|---|
| 260 | TEST_ATTR_INPLACE(1,-,2,-1,-3) |
|---|
| 261 | TEST_ATTR_INPLACE(2,*,3,6,18) |
|---|
| 262 | TEST_ATTR_INPLACE(2,/,2,9,4) |
|---|
| 263 | TEST_ATTR_INPLACE(0,%,4,2,2) |
|---|
| 264 | o.attr("x0") += 1; |
|---|
| 265 | TEST_ATTR_INPLACE(0,<<,2,12,48) |
|---|
| 266 | TEST_ATTR_INPLACE(0,>>,1,24,12) |
|---|
| 267 | o.attr("x4") = 15; |
|---|
| 268 | TEST_ATTR_INPLACE(4,&,(16+4+1),5,5) |
|---|
| 269 | TEST_ATTR_INPLACE(0,^,1,13,12) |
|---|
| 270 | TEST_ATTR_INPLACE(0,|,1,13,13) |
|---|
| 271 | |
|---|
| 272 | if (l[0] != o.attr("x0")) |
|---|
| 273 | return false; |
|---|
| 274 | if (l[1] != o.attr("x1")) |
|---|
| 275 | return false; |
|---|
| 276 | if (l[2] != o.attr("x2")) |
|---|
| 277 | return false; |
|---|
| 278 | if (l[3] != o.attr("x3")) |
|---|
| 279 | return false; |
|---|
| 280 | if (l[4] != o.attr("x4")) |
|---|
| 281 | return false; |
|---|
| 282 | |
|---|
| 283 | // set item 5 to be a list, by calling l.__class__ |
|---|
| 284 | l[5] = l.attr("__class__")(); |
|---|
| 285 | // append an element |
|---|
| 286 | l[5].attr("append")(2); |
|---|
| 287 | // Check its value |
|---|
| 288 | if (l[5][0] != 2) |
|---|
| 289 | return false; |
|---|
| 290 | |
|---|
| 291 | return true; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | BOOST_PYTHON_MODULE(object_ext) |
|---|
| 295 | { |
|---|
| 296 | class_<NotCopyable, boost::noncopyable>("NotCopyable", no_init); |
|---|
| 297 | |
|---|
| 298 | def("ref_to_noncopyable", ref_to_noncopyable); |
|---|
| 299 | def("call_object_3", call_object_3); |
|---|
| 300 | def("message", message); |
|---|
| 301 | def("number", number); |
|---|
| 302 | |
|---|
| 303 | def("obj_getattr", obj_getattr); |
|---|
| 304 | def("obj_const_getattr", obj_const_getattr); |
|---|
| 305 | def("obj_setattr", obj_setattr); |
|---|
| 306 | def("obj_setattr42", obj_setattr42); |
|---|
| 307 | def("obj_moveattr", obj_moveattr); |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | def("obj_getitem", obj_getitem); |
|---|
| 311 | def("obj_getitem3", obj_getitem); |
|---|
| 312 | def("obj_const_getitem", obj_const_getitem); |
|---|
| 313 | def("obj_setitem", obj_setitem); |
|---|
| 314 | def("obj_setitem42", obj_setitem42); |
|---|
| 315 | def("obj_moveitem", obj_moveitem); |
|---|
| 316 | def("obj_moveitem2", obj_moveitem2); |
|---|
| 317 | |
|---|
| 318 | def("test", test); |
|---|
| 319 | def("test_not", test_not); |
|---|
| 320 | |
|---|
| 321 | def("test_attr", test_attr); |
|---|
| 322 | def("test_not_attr", test_not_attr); |
|---|
| 323 | |
|---|
| 324 | def("test_item", test_item); |
|---|
| 325 | def("test_not_item", test_not_item); |
|---|
| 326 | |
|---|
| 327 | def("check_binary_operators", check_binary_operators); |
|---|
| 328 | def("check_inplace", check_inplace); |
|---|
| 329 | def("check_string_slice", check_string_slice); |
|---|
| 330 | ; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | #include "module_tail.cpp" |
|---|