# tests for slice objects; in particular the indices method.
from test.test_support import vereq
+import sys
vereq(slice(None ).indices(10), (0, 10, 1))
vereq(slice(None, None, 2).indices(10), (0, 10, 2))
vereq(slice(-100, 100 ).indices(10), slice(None).indices(10))
vereq(slice(100, -100, -1).indices(10), slice(None, None, -1).indices(10))
vereq(slice(-100L, 100L, 2L).indices(10), (0, 10, 2))
+
+vereq(range(10)[::sys.maxint - 1], [0])
int *start, int *stop, int *step, int *slicelength)
{
/* this is harder to get right than you might think */
+
int defstart, defstop;
if (r->step == Py_None) {
*step = 1;
- } else {
+ }
+ else {
*step = PyInt_AsLong(r->step);
if (*step == -1 && PyErr_Occurred()) {
return -1;
if (r->start == Py_None) {
*start = defstart;
- } else {
+ }
+ else {
if (!_PyEval_SliceIndex(r->start, start)) return -1;
if (*start < 0) *start += length;
if (*start < 0) *start = (*step < 0) ? -1 : 0;
if (r->stop == Py_None) {
*stop = defstop;
- } else {
+ }
+ else {
if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
if (*stop < 0) *stop += length;
if (*stop < 0) *stop = -1;
if (*stop > length) *stop = length;
}
-
- if ((*stop - *start)*(*step) <= 0) {
+
+ if ((*step < 0 && *stop >= *start)
+ || (*step > 0 && *start >= *stop)) {
*slicelength = 0;
}
else if (*step < 0) {
*slicelength = (*stop-*start+1)/(*step)+1;
- } else {
+ }
+ else {
*slicelength = (*stop-*start-1)/(*step)+1;
}