2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

Throw an IndexError when given an extended slice;

Clip bounds of slices in a mannar consistant with builtin containers;
Prevent undefined behavior within the STL when given certain kinds of
empty slices.


[SVN r22507]
This commit is contained in:
Jonathan Brandmeyer
2004-03-17 03:15:35 +00:00
parent b3f0d7c756
commit 7187c6132a
2 changed files with 57 additions and 13 deletions

View File

@@ -13,6 +13,7 @@
# include <boost/detail/binary_search.hpp>
# include <vector>
# include <map>
#include <iostream>
namespace boost { namespace python { namespace detail {
@@ -507,9 +508,9 @@ namespace boost { namespace python { namespace detail {
static object
base_get_item_(back_reference<Container&> const& container, PyObject* i)
{
// Proxy
// Proxy
Index idx = DerivedPolicies::convert_index(container.get(), i);
if (PyObject* shared =
ContainerElement::get_links().find(container.get(), idx))
{
@@ -570,15 +571,42 @@ namespace boost { namespace python { namespace detail {
base_get_slice_data(
Container& container, PySliceObject* slice, Index& from, Index& to)
{
if (Py_None == slice->start)
from = DerivedPolicies::get_min_index(container);
else
from = DerivedPolicies::convert_index(container, slice->start);
if (Py_None != slice->step) {
PyErr_SetString( PyExc_IndexError, "slice step size not supported.");
throw_error_already_set();
}
if (Py_None == slice->stop)
Index min_index = DerivedPolicies::get_min_index(container);
Index max_index = DerivedPolicies::get_max_index(container);
if (Py_None == slice->start) {
from = min_index;
}
else {
from = extract<long>( 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<long>( slice->stop);
if (to < 0)
to += max_index;
if (to < 0)
to = 0;
if (to > max_index)
to = max_index;
}
}
static void

View File

@@ -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 <class Iter>
@@ -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);
}