Feature or enhancement
Proposal:
In the deepcopy implementation an exception is generated when the memo dict does not contain a key id(memo). We can avoid generating an exception by adding this key when the memo is generated. As a consequence the _keep_alive method is just a single line of code and we can inline this.
There is a behavior change: a call deepcopy(x, {}) works with current main, but will raise an exception (since the key id(memo)) has not been set. This might be acceptable as the documentation states "The memo dictionary should be treated as an opaque object." (see https://docs.python.org/3/library/copy.html). And in the code itself the memo argument is not documented at all
|
Interface summary: |
|
|
|
import copy |
|
|
|
x = copy.copy(y) # make a shallow copy of y |
|
x = copy.deepcopy(y) # make a deep copy of y |
|
x = copy.replace(y, a=1, b=2) # new object with fields replaced, as defined by `__replace__` |
There are some unit tests involving the memo, but those seem to test the behavior of the deepcopy and not of the memo argument.
If the behavior change is not acceptable because memo is part of the public interface, should we then instead document the structure of the argument memo` as being an implementation detail?
The argument _nil as a sentinel to the deepcopy is redundant, as the memo can only contain integers so we can use None instead.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Feature or enhancement
Proposal:
In the
deepcopyimplementation an exception is generated when thememodict does not contain a keyid(memo). We can avoid generating an exception by adding this key when the memo is generated. As a consequence the_keep_alivemethod is just a single line of code and we can inline this.There is a behavior change: a call
deepcopy(x, {})works with current main, but will raise an exception (since the keyid(memo)) has not been set. This might be acceptable as the documentation states "The memo dictionary should be treated as an opaque object." (see https://docs.python.org/3/library/copy.html). And in the code itself thememoargument is not documented at allcpython/Lib/copy.py
Lines 3 to 9 in 9aea9c1
There are some unit tests involving the
memo, but those seem to test the behavior of thedeepcopyand not of thememoargument.If the behavior change is not acceptable because
memois part of the public interface, should we then instead document the structure of the argument memo` as being an implementation detail?The argument
_nilas a sentinel to thedeepcopyis redundant, as thememocan only contain integers so we can useNoneinstead.Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs