classification
Title: The range() object is deepcopied as atomic
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: alexandre.vassalotti, ethan.furman, fdrake, python-dev, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2016-01-25 20:53 by serhiy.storchaka, last changed 2020-05-28 08:15 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
deepcopy_range.patch serhiy.storchaka, 2016-01-25 20:53 review
Messages (10)
msg258921 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-25 20:53
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.
msg258925 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-25 21:23
Hum ok, it's a bug :-) It should be fixed. Do you want to work on a patch?
msg258926 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-01-25 21:24
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?
msg258927 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-01-25 21:25
Victor, patch was already attached.  ;)
msg258929 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-25 21:44
deepcopy_range.patch looks good to me.
msg258930 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-25 21:47
Returning the same range() object from copy.copy() is correct. This is shallow copying.
msg259154 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-28 19:44
New changeset 5772eae17a82 by Serhiy Storchaka in branch '3.5':
Issue #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 #26202: copy.deepcopy() now correctly copies range() objects with
https://hg.python.org/cpython/rev/d5d0b62c2830
msg259155 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-01-28 19:46
Thanks Victor.
msg370167 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-28 07:34
New changeset 5f4b229df7812f1788287095eb6b138bb21876a4 by Serhiy Storchaka in branch 'master':
bpo-40792: Make the result of PyNumber_Index() always having exact type int. (GH-20443)
https://github.com/python/cpython/commit/5f4b229df7812f1788287095eb6b138bb21876a4
msg370174 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-28 08:15
This change has been reverted in issue40792. The range object attributes has now exact type int, so the original issue with deep copying is gone.
History
Date User Action Args
2020-05-28 08:15:50serhiy.storchakasetmessages: + msg370174
2020-05-28 07:34:43serhiy.storchakasetmessages: + msg370167
2016-01-28 19:46:46serhiy.storchakasetstatus: open -> closed
messages: + msg259155

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2016-01-28 19:44:43python-devsetnosy: + python-dev
messages: + msg259154
2016-01-25 21:47:24serhiy.storchakasetmessages: + msg258930
2016-01-25 21:44:51vstinnersetmessages: + msg258929
2016-01-25 21:25:18ethan.furmansetmessages: + msg258927
2016-01-25 21:24:46ethan.furmansetnosy: + ethan.furman
messages: + msg258926
2016-01-25 21:23:00vstinnersetmessages: + msg258925
2016-01-25 20:53:06serhiy.storchakacreate