summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-11-05 15:28:51 (GMT)
committerMichael W. Hudson <mwh@python.net>2002-11-05 15:28:51 (GMT)
commit173f11da5dae76c504dac3606c27b74673ccfee4 (patch)
tree4941ffa1ae203a4c87e4c3bdf67a2a35a8708e23
parentd7c14c6c9b5b589d33810fb64ad2b883d52ff7ae (diff)
downloadcpython-173f11da5dae76c504dac3606c27b74673ccfee4.zip
cpython-173f11da5dae76c504dac3606c27b74673ccfee4.tar.gz
cpython-173f11da5dae76c504dac3606c27b74673ccfee4.tar.bz2
Some days, I think my comment of
/* this is harder to get right than you might think */ angered some God somewhere. After noticing >>> range(5000000)[slice(96360, None, 439)] [] I found that my cute test for the slice being empty failed due to overflow. Fixed, and added simple test (not the above!).
-rw-r--r--Lib/test/test_slice.py3
-rw-r--r--Objects/sliceobject.c18
2 files changed, 15 insertions, 6 deletions
diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py
index 7b3ee06..49ed20e 100644
--- a/Lib/test/test_slice.py
+++ b/Lib/test/test_slice.py
@@ -1,6 +1,7 @@
# 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))
@@ -11,3 +12,5 @@ vereq(slice(3, None, -2).indices(10), (3, -1, -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])
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 21426a7..a035e5f 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -114,11 +114,13 @@ PySlice_GetIndicesEx(PySliceObject *r, int length,
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;
@@ -135,7 +137,8 @@ PySlice_GetIndicesEx(PySliceObject *r, int length,
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;
@@ -145,19 +148,22 @@ PySlice_GetIndicesEx(PySliceObject *r, int length,
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;
}