Skip to content

Commit 466e293

Browse files
Commit
1 parent 2ce37e4 commit 466e293

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Lib/test/test_range.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,14 @@ def check(start, stop, step=None):
584584
check(0, -1)
585585
check(-1, -3, -1)
586586

587-
for test in (range(5), range(0), range(1, 10, 2), range(10, 0, -1)):
587+
for test in (range(5), range(0), range(1, 10, 2), range(10, 0, -1), range(-1, 200, 1)):
588588
with self.subTest(test=test):
589589
self.assertIs(test, test[:])
590+
self.assertIs(test, test[:len(test):])
591+
self.assertIs(test, test[0:len(test):1])
590592

591593
if len(test) > 1:
594+
self.assertIsNot(test, test[1:len(test):2])
592595
self.assertIsNot(test, test[1:])
593596
self.assertIsNot(test, test[:-1])
594597

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
:func:`range` slicing with ``[:]`` no longer creates a copy, it now returns
2-
the same object, consistent with :func:`copy.copy`.
1+
:func:`range` slicing with ``[:]`` and ``[:len(r):]`` no longer creates a copy,
2+
it now returns the same object, consistent with :func:`copy.copy`.

Objects/rangeobject.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,17 @@ compute_slice(rangeobject *r, PyObject *_slice)
409409
PyObject *substart = NULL, *substop = NULL, *substep = NULL;
410410
int error;
411411

412-
if (slice->start == Py_None && slice->stop == Py_None && slice->step == Py_None) {
413-
return Py_NewRef(r);
414-
}
415-
416412
error = _PySlice_GetLongIndices(slice, r->length, &start, &stop, &step);
417413
if (error == -1)
418414
return NULL;
419415

416+
if (start == _PyLong_GetZero()
417+
&& step == _PyLong_GetOne()
418+
&& (slice->stop == Py_None || PyObject_RichCompareBool(stop, r->length, Py_EQ) == 1))
419+
{
420+
return Py_NewRef(r);
421+
}
422+
420423
substep = PyNumber_Multiply(r->step, step);
421424
if (substep == NULL) goto fail;
422425
Py_CLEAR(step);

0 commit comments

Comments
 (0)