diff --git a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp b/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp index e3ecf7d7..d43e74de 100644 --- a/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp +++ b/include/boost/python/suite/indexing/detail/indexing_suite_detail.hpp @@ -570,15 +570,40 @@ namespace boost { namespace python { namespace detail { base_get_slice_data( Container& container, PySliceObject* slice, Index& from, Index& to) { + if (Py_None != slice->step) { + PyErr_SetString( PyExc_IndexError, "slice step size not supported."); + throw_error_already_set(); + } + + Index min_index = DerivedPolicies::get_min_index(container); + Index max_index = DerivedPolicies::get_max_index(container); + + if (Py_None == slice->start) - from = DerivedPolicies::get_min_index(container); - else - from = DerivedPolicies::convert_index(container, slice->start); + from = min_index; + else { + from = extract( slice->start); + if (from < 0) // Negative slice index + from += max_index; + if (from < 0) // Clip lower bounds to zero + from = 0; + if (from > max_index) // Clip upper bounds to max_index. + from = max_index; + + } if (Py_None == slice->stop) to = DerivedPolicies::get_max_index(container); - else - to = DerivedPolicies::convert_index(container, slice->stop); + else { + to = extract( slice->stop); + if (to < 0) + to += max_index; + if (to < 0) + to = 0; + if (to > max_index) + to = max_index; + } + } static void diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index 20c58af6..19abe2ff 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -81,6 +81,8 @@ namespace boost { namespace python { static object get_slice(Container& container, index_type from, index_type to) { + if (from > to) + return object(Container()); return object(Container(container.begin()+from, container.begin()+to)); } @@ -94,8 +96,13 @@ namespace boost { namespace python { set_slice(Container& container, index_type from, index_type to, data_type const& v) { - container.erase(container.begin()+from, container.begin()+to); - container.insert(container.begin()+from, v); + if (from > to) { + return; + } + else { + container.erase(container.begin()+from, container.begin()+to); + container.insert(container.begin()+from, v); + } } template @@ -103,8 +110,13 @@ namespace boost { namespace python { set_slice(Container& container, index_type from, index_type to, Iter first, Iter last) { - container.erase(container.begin()+from, container.begin()+to); - container.insert(container.begin()+from, first, last); + if (from > to) { + container.insert(container.begin()+from, first, last); + } + else { + container.erase(container.begin()+from, container.begin()+to); + container.insert(container.begin()+from, first, last); + } } static void @@ -116,6 +128,10 @@ namespace boost { namespace python { static void delete_slice(Container& container, index_type from, index_type to) { + if (from > to) { + // A null-op. + return; + } container.erase(container.begin()+from, container.begin()+to); }