1 #include <boost/python.hpp>
2 #include <boost/python/slice.hpp>
3 #include <boost/python/str.hpp>
4 #include <vector>
5 
6 // Copyright (c) 2004 Jonathan Brandmeyer
7 //  Use, modification and distribution are subject to the
8 //  Boost Software License, Version 1.0. (See accompanying file
9 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 
11 using namespace boost::python;
12 
13 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
14 # define make_tuple boost::python::make_tuple
15 #endif
16 
17 // These checks are only valid under Python 2.3
18 // (rich slicing wasn't supported for builtins under Python 2.2)
check_string_rich_slice()19 bool check_string_rich_slice()
20 {
21     object s("hello, world");
22 
23     // default slice
24     if (s[slice()] != "hello, world")
25         return false;
26 
27     // simple reverse
28     if (s[slice(_,_,-1)] != "dlrow ,olleh")
29         return false;
30 
31     // reverse with mixed-sign offsets
32     if (s[slice(-6,1,-1)] != " ,oll")
33         return false;
34 
35     // all of the object.cpp check_string_slice() checks should work
36     // with the form that omits the step argument.
37     if (s[slice(_,-3)] != "hello, wo")
38         return false;
39     if (s[slice(-3,_)] != "rld")
40         return false;
41     if (", " != s[slice(5,7)])
42         return false;
43 
44     return s[slice(2,-1)][slice(1,-1)]  == "lo, wor";
45 }
46 
47 // Tried to get more info into the error message (actual array
48 // contents) but Numeric complains that treating an array as a boolean
49 // value doesn't make any sense.
50 #define ASSERT_EQUAL( e1, e2 ) \
51     if (!all((e1) == (e2)))                                                             \
52         return "assertion failed: " #e1 " == " #e2 "\nLHS:\n%s\nRHS:\n%s" % make_tuple(e1,e2);    \
53 else
54 
55 // Verify functions accepting a slice argument can be called
accept_slice(slice)56 bool accept_slice( slice) { return true; }
57 
58 #if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \
59     || BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710)
60 int check_slice_get_indices(slice index);
61 #endif
check_slice_get_indices(const slice index)62 int check_slice_get_indices(
63 #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
64     const
65 #endif
66     slice index)
67 {
68     // A vector of integers from [-5, 5].
69     std::vector<int> coll(11);
70     typedef std::vector<int>::iterator coll_iterator;
71 
72     for (coll_iterator i = coll.begin(); i != coll.end(); ++i) {
73         *i = i - coll.begin() - 5;
74     }
75 
76     slice::range<std::vector<int>::iterator> bounds;
77     try {
78         bounds = index.get_indices(coll.begin(), coll.end());
79     }
80     catch (std::invalid_argument) {
81         return 0;
82     }
83     int sum = 0;
84     while (bounds.start != bounds.stop) {
85         sum += *bounds.start;
86         std::advance( bounds.start, bounds.step);
87     }
88     sum += *bounds.start;
89     return sum;
90 }
91 
92 
BOOST_PYTHON_MODULE(slice_ext)93 BOOST_PYTHON_MODULE(slice_ext)
94 {
95     def( "accept_slice", accept_slice);
96     def( "check_string_rich_slice", check_string_rich_slice);
97     def( "check_slice_get_indices", check_slice_get_indices);
98 }
99