Skip to content

Commit 2ce37e4

Browse files
Commit
1 parent 75b1afe commit 2ce37e4

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/test/test_range.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +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)):
588+
with self.subTest(test=test):
589+
self.assertIs(test, test[:])
590+
591+
if len(test) > 1:
592+
self.assertIsNot(test, test[1:])
593+
self.assertIsNot(test, test[:-1])
594+
587595
def test_contains(self):
588596
r = range(10)
589597
self.assertIn(0, r)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`range` slicing with ``[:]`` no longer creates a copy, it now returns
2+
the same object, consistent with :func:`copy.copy`.

Objects/rangeobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ 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+
412416
error = _PySlice_GetLongIndices(slice, r->length, &start, &stop, &step);
413417
if (error == -1)
414418
return NULL;

0 commit comments

Comments
 (0)