gh-104533: Add a dataclass-like decorator for ctypes structures#153781
Draft
ZeroIntensity wants to merge 4 commits into
Draft
gh-104533: Add a dataclass-like decorator for ctypes structures#153781ZeroIntensity wants to merge 4 commits into
ctypes structures#153781ZeroIntensity wants to merge 4 commits into
Conversation
Documentation build overview
|
johnslavik
reviewed
Jul 15, 2026
Comment on lines
+543
to
+564
| # These fields don't apply correctly when set later. | ||
| # As a workaround, we have this weird _StructData thing to set the attributes | ||
| # in advance. | ||
| class _StructData: | ||
| pass | ||
|
|
||
| if align is not None: | ||
| _StructData._align_ = align | ||
|
|
||
| if layout is not None: | ||
| _StructData._layout_ = layout | ||
|
|
||
| if pack is not None: | ||
| _StructData._pack_ = pack | ||
|
|
||
| for attr, value in klass.__dict__.items(): | ||
| if attr != "__dict__": | ||
| setattr(_StructData, attr, value) | ||
|
|
||
| class _Struct(_StructData, endian_class): | ||
| _fields_ = fields | ||
| _anonymous_ = anonymous |
Member
There was a problem hiding this comment.
This works for me just fine:
Suggested change
| # These fields don't apply correctly when set later. | |
| # As a workaround, we have this weird _StructData thing to set the attributes | |
| # in advance. | |
| class _StructData: | |
| pass | |
| if align is not None: | |
| _StructData._align_ = align | |
| if layout is not None: | |
| _StructData._layout_ = layout | |
| if pack is not None: | |
| _StructData._pack_ = pack | |
| for attr, value in klass.__dict__.items(): | |
| if attr != "__dict__": | |
| setattr(_StructData, attr, value) | |
| class _Struct(_StructData, endian_class): | |
| _fields_ = fields | |
| _anonymous_ = anonymous | |
| class _Struct(endian_class): | |
| vars().update(vars(klass)) | |
| if align is not None: | |
| _align_ = align | |
| if layout is not None: | |
| _layout_ = layout | |
| if pack is not None: | |
| _pack_ = pack | |
| _fields_ = fields | |
| _anonymous_ = anonymous |
but maybe I'm missing sth
|
|
||
| _Struct.__name__ = klass.__name__ | ||
| _Struct.__qualname__ = klass.__qualname__ | ||
| _Struct.__module__ = klass.__module__ |
Member
There was a problem hiding this comment.
Consider using lazy-imported functools.wraps for that.
You'd decorate the class with @wraps(klass, updated=()) (updated=() because __dict__ is unwritable).
Comment on lines
+574
to
+577
| def inner(klass): | ||
| return _process_struct(klass, align=align, layout=layout, endian=endian, pack=pack) | ||
|
|
||
| return inner |
Member
There was a problem hiding this comment.
If you decide to apply functools above, you could return a partial() here idiomatically.
| def _process_struct(klass, /, *, align, layout, endian, pack): | ||
| fields = [] | ||
| anonymous = [] | ||
| if issubclass(klass, (Structure, LittleEndianStructure, BigEndianStructure)): |
Member
There was a problem hiding this comment.
Curious why not
Suggested change
| if issubclass(klass, (Structure, LittleEndianStructure, BigEndianStructure)): | |
| if issubclass(klass, Structure): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.