object.cpp 8.9 KB

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