Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The range() object is deepcopied as atomic #70390

Closed
serhiy-storchaka opened this issue Jan 25, 2016 · 10 comments
Closed

The range() object is deepcopied as atomic #70390

serhiy-storchaka opened this issue Jan 25, 2016 · 10 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 26202
Nosy @freddrake, @vstinner, @avassalotti, @ethanfurman, @serhiy-storchaka
Files
  • deepcopy_range.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2016-01-28.19:46:46.805>
    created_at = <Date 2016-01-25.20:53:06.176>
    labels = ['type-bug', 'library']
    title = 'The range() object is deepcopied as atomic'
    updated_at = <Date 2020-05-28.08:15:50.321>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2020-05-28.08:15:50.321>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2016-01-28.19:46:46.805>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2016-01-25.20:53:06.176>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = ['41714']
    hgrepos = []
    issue_num = 26202
    keywords = ['patch']
    message_count = 10.0
    messages = ['258921', '258925', '258926', '258927', '258929', '258930', '259154', '259155', '370167', '370174']
    nosy_count = 6.0
    nosy_names = ['fdrake', 'vstinner', 'alexandre.vassalotti', 'ethan.furman', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26202'
    versions = ['Python 3.5', 'Python 3.6']

    @serhiy-storchaka
    Copy link
    Member Author

    The range() object is immutable, but is not atomic, and copy.deepcopy() shouldn't return it unchanged.

    >>> class I(int): pass  # mutable index
    ... 
    >>> import copy
    >>> r = range(I(10))
    >>> r2 = copy.deepcopy(r)
    >>> r.stop.attr = 'spam'
    >>> r2.stop.attr
    'spam'

    This is Python 3 only issue because in 2.7 the xrange() object doesn't exposes start/stop/step attributes.

    Proposed patch fixes the copy module.

    @serhiy-storchaka serhiy-storchaka added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 25, 2016
    @vstinner
    Copy link
    Member

    Hum ok, it's a bug :-) It should be fixed. Do you want to work on a patch?

    @ethanfurman
    Copy link
    Member

    I don't have much experience with the copy module, but I don't see any problems with the code.

    Does copy.copy suffer from the same problem? If yes, is it fixed with this same patch, or is more work needed?

    @ethanfurman
    Copy link
    Member

    Victor, patch was already attached. ;)

    @vstinner
    Copy link
    Member

    deepcopy_range.patch looks good to me.

    @serhiy-storchaka
    Copy link
    Member Author

    Returning the same range() object from copy.copy() is correct. This is shallow copying.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 28, 2016

    New changeset 5772eae17a82 by Serhiy Storchaka in branch '3.5':
    Issue bpo-26202: copy.deepcopy() now correctly copies range() objects with
    https://hg.python.org/cpython/rev/5772eae17a82

    New changeset d5d0b62c2830 by Serhiy Storchaka in branch 'default':
    Issue bpo-26202: copy.deepcopy() now correctly copies range() objects with
    https://hg.python.org/cpython/rev/d5d0b62c2830

    @serhiy-storchaka
    Copy link
    Member Author

    Thanks Victor.

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 5f4b229 by Serhiy Storchaka in branch 'master':
    bpo-40792: Make the result of PyNumber_Index() always having exact type int. (GH-20443)
    5f4b229

    @serhiy-storchaka
    Copy link
    Member Author

    This change has been reverted in bpo-40792. The range object attributes has now exact type int, so the original issue with deep copying is gone.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants