2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-19 16:32:16 +00:00

Check indices passed to __getitem__, __setitem__, __delitem__

[SVN r10009]
This commit is contained in:
Ralf W. Grosse-Kunstleve
2001-05-05 01:06:33 +00:00
parent fb8d9edfdf
commit 00b4f09e8a
3 changed files with 14 additions and 1 deletions

View File

@@ -24,7 +24,7 @@ PYINC=-I/usr/local/Python-1.5.2/include/python1.5
STLPORTINC=-I$(BOOST)/boost/compatibility/cpp_c_headers
STDOPTS=
WARNOPTS=-woff 1001,1234,1682
WARNOPTS=-woff 1001,1183,1234,1682
OPTOPTS=-g
CPP=CC -LANG:std -n32 -mips4

View File

@@ -30,16 +30,24 @@ namespace { // Avoid cluttering the global namespace.
}
};
void raise_vector_IndexError() {
PyErr_SetString(PyExc_IndexError, "IndexError: vector index out of range");
throw python::error_already_set();
}
double getitem(const std::vector<double>& vd, std::size_t key) {
if (key < 0 || key >= vd.size()) raise_vector_IndexError();
return vd[key];
}
void setitem(std::vector<double>& vd, std::size_t key, double d) {
if (key < 0 || key >= vd.size()) raise_vector_IndexError();
std::vector<double>::iterator vditer = vd.begin();
vditer[key] = d;
}
void delitem(std::vector<double>& vd, std::size_t key) {
if (key < 0 || key >= vd.size()) raise_vector_IndexError();
std::vector<double>::iterator vditer = vd.begin();
vd.erase(&vditer[key]);
}

View File

@@ -15,6 +15,11 @@ r'''>>> import simple_vector
>>> v[1] = 40
>>> print v.as_tuple()
(3.0, 40.0, 5.0)
>>> for e in v:
... print e
3.0
40.0
5.0
>>> del v[1]
>>> print v.as_tuple()
(3.0, 5.0)