-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Fix deepcopy
for new-style Bit
#10411
Conversation
One or more of the the following people are requested to review this:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks sensible to me, thanks. I think it would be good to insert a test, if there's a sensible one we can do.
Pull Request Test Coverage Report for Build 5513114945
💛 - Coveralls |
I've updated the description since the PR now only changes the behavior of |
Bit
copy return self
.deepcopy
for new-style Bit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for the spotting the problem and solving it!
# Old-style bits need special handling for now, since some code seems | ||
# to rely on their registers getting deep-copied. | ||
bit = type(self).__new__(type(self)) | ||
bit._register = copy.deepcopy(self._register, memo) | ||
bit._index = self._index | ||
bit._hash = self._hash | ||
bit._repr = self._repr | ||
return bit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, in fairness, we were wrong before when we didn't recurse the deepcopy
onto the register. It likely would have caused a weird inconsistency where the reg[0]._register is not reg
, which is an invariant of old-style registers/bits at the moment. I can't immediately see why that has knock-on effects, but I guess it shouldn't be surprising.
Summary
Overrides the default behavior of
copy
anddeepcopy
forBit
to returnself
for new-style bits.Details and comments
We choose this behavior because
Bit
s are immutable, and it allows us to deep-copy new-style bits while maintaining equality between the original and the copy. For old-style bits, we leave the behavior unchanged, since some existing code appears to depend on_register
getting deep-copied.Note that we now return
self
when shallow-copying old-style bits, which we can do safely sinceBit
s themselves are immutable.Resolves #10409.