Skip to content

Apply similar slicing optimizations as tuple and str to range #139476

@hyperkai

Description

@hyperkai

Bug report

Bug description:

copy.copy() and copy.deepcopy() don't copy a range as shown below:

import copy

v1 = range(5)

v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)

print(v1 is v2)
# True

But slicing copies a range as shown below:

v1 = range(5)

v2 = v1[:]

print(v1 is v2)
# False

Basically, a shallow or deep copy operation with copy.copy(), slicing or copy.deepcopy() doesn't create a copy for an immutable object like a tuple, string or bytes to avoid wasting memory but only slicing copies a range even though copy.copy() and copy.deepcopy() don't copy a range.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions