summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-11-05 15:28:51 +0000
committerMichael W. Hudson <mwh@python.net>2002-11-05 15:28:51 +0000
commit41a618cf8f54c7ebc5475d84f7350721aaee05ce (patch)
treea7b408e63b90041ad3e08f57de8002f86b586e39 /Objects/sliceobject.c
parentf555bd64f2ca7e3fcf8c3b18530b8b19a00a3c28 (diff)
downloadcpython-41a618cf8f54c7ebc5475d84f7350721aaee05ce.tar.gz
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!).
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 21426a761a..a035e5faf0 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;
}