From 589dc93620656faf80713b50b50d957664cea287 Mon Sep 17 00:00:00 2001 From: "Michael W. Hudson" Date: Tue, 11 Jun 2002 13:38:42 +0000 Subject: Fix for problem reported by Neal Norwitz. Tighten up calculation of slicelength. Include his test case. --- Lib/test/test_types.py | 2 ++ Objects/sliceobject.c | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 8452cec..71e18c4 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -374,6 +374,8 @@ vereq(a[3::-2], [3,1]) vereq(a[-100:100:], a) vereq(a[100:-100:-1], a[::-1]) vereq(a[-100L:100L:2L], [0,2,4]) +vereq(a[1000:2000:2], []) +vereq(a[-1000:-2000:-2], []) # deletion del a[::2] vereq(a, [1,3]) diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 7499d31..9a268b7 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -151,13 +151,15 @@ PySlice_GetIndicesEx(PySliceObject *r, int length, if (*stop < 0) *stop = -1; if (*stop > length) *stop = length; } - - if (*step < 0) { + + if ((*stop - *start)*(*step) <= 0) { + *slicelength = 0; + } + else if (*step < 0) { *slicelength = (*stop-*start+1)/(*step)+1; } else { *slicelength = (*stop-*start-1)/(*step)+1; } - if (*slicelength < 0) *slicelength = 0; return 0; } -- cgit v0.12