-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-110222: Add support of PyStructSequence in copy.replace() #110223
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
gh-110222: Add support of PyStructSequence in copy.replace() #110223
Conversation
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.
I am surprised to see a correct complex C code from a new contributor. It is not easy to write all these error checks and decrefs on the first version.
The implementation creates arguments (a tuple and a dict) for __new__()
and calls it. It is not the most efficient way, because it creates intermediate tuple and dict and can create string keys for name-only fields multiple times, but it works, and performance is not critical. I'm just wondering, whether it was your first idea or you considered other approaches and they turned out more complex? Did you try to create a new instance with PyStructSequence_New()
, then copy ob_items
from old structure to the new, then iterate the kwargs dict in one pass and patch ob_items
? It looks simpler to me, but I may have missed some details.
@serhiy-storchaka My first thought is to create a copy of the current structseq and then replace the new values from I will refactor it with the former implementation since we have disabled the support for types with unnamed fields. |
Done. |
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.
Very well.
I have few more comments, mostly test suggestions.
LGTM! |
Thank you for your contribution @XuehaiPan. |
Resolves #110222
The implementation in this PR is roughly equivalent to:Refactored: