list.cpp 3.0 KB

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