-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-104533: Add a dataclass-like decorator for ctypes structures
#153781
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
Open
ZeroIntensity
wants to merge
8
commits into
python:main
Choose a base branch
from
ZeroIntensity:ctypes/struct-decorator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0db59d
Implement a dataclass-like struct decorator for ctypes.
ZeroIntensity b73c912
Add docs.
ZeroIntensity 94eb25a
Add blurb.
ZeroIntensity bba7682
Fix Sphinx warnings.
ZeroIntensity 91eb76b
Apply suggestions from code review
ZeroIntensity 103a740
Use functools.wraps.
ZeroIntensity b14dd51
Use functools.partial.
ZeroIntensity 864b811
Add tests for CFieldInfo.
ZeroIntensity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,16 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from dataclasses import dataclass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy import functools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy import shutil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy import subprocess | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy import annotationlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy from typing import Annotated, get_args, ClassVar, get_origin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lazy from ctypes import Structure, BigEndianStructure, LittleEndianStructure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # find_library(name) returns the pathname of a library, or None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if os.name == "nt": | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -491,6 +498,83 @@ def dllist(): | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| ctypes.byref(ctypes.py_object(libraries))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return libraries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @dataclass(slots=True, frozen=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class CFieldInfo: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anonymous: bool = False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bit_width: int | None = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _process_struct(klass, /, *, align, layout, endian, pack): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fields = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anonymous = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if issubclass(klass, Structure): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fields.extend(klass._fields_) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anonymous.extend(klass._anonymous_) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for name, hint in annotationlib.get_annotations(klass).items(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if get_origin(hint) is ClassVar: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| field = [name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if get_origin(hint) is Annotated: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| args = get_args(hint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| field.append(args[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| field_info = args[1] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+521
to
+523
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to ignore
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(field_info, CFieldInfo): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise TypeError(f"expected CFieldInfo in Annotated, got {field_info!r}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if field_info.bit_width is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| field.append(field_info.bit_width) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if field_info.anonymous is True: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| anonymous.append(name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| field.append(hint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fields.append(field) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if endian == 'big': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endian_class = BigEndianStructure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif endian == 'little': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endian_class = LittleEndianStructure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| elif endian == 'native': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endian_class = Structure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError(f"expected 'big', 'little', or 'native', but got {endian!r}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @functools.wraps(klass, updated=()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _Struct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| process_the_struct = functools.partial( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _process_struct, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| align=align, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| layout=layout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| endian=endian, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pack=pack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if class_or_none is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def inner(klass): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return process_the_struct(klass) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return inner | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ZeroIntensity marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return process_the_struct(class_or_none) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+561
to
+576
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant it like this:
Suggested change
That's how decorators of this fashion are usually made. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # test code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
Consider adding a collation: old syntax vs. new syntax.