Uh oh!
There was an error while loading. Please reload this page.
gh-104533: Add a dataclass-like decorator for ctypes structures - #153781
Conversation
Documentation build overview
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Uh oh!
There was an error while loading. Please reload this page.
| 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 | ||
| return process_the_struct(class_or_none) |
There was a problem hiding this comment.
I meant it like this:
| defstruct(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 | |
| ) | |
| ifclass_or_noneisNone: | |
| definner(klass): | |
| returnprocess_the_struct(klass) | |
| returninner | |
| returnprocess_the_struct(class_or_none) | |
| defstruct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None): | |
| ifclass_or_noneisNone: | |
| returnfunctools.partial( | |
| struct, | |
| align=align, | |
| layout=layout, | |
| endian=endian, | |
| pack=pack, | |
| ) | |
| return_process_struct( | |
| class_or_none, | |
| align=align, | |
| layout=layout, | |
| endian=endian, | |
| pack=pack, | |
| ) |
That's how decorators of this fashion are usually made.
Feel free to ignore.
There was a problem hiding this comment.
process_the_struct isn't defined here; what are you trying to show?
| .. decorator:: struct(*, align=None, layout, endian='native', pack=None) | ||
| :module: ctypes.util |
There was a problem hiding this comment.
Consider adding a collation: old syntax vs. new syntax.
There was a problem hiding this comment.
I don't think that's necessary -- the examples for Structure are right above.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Uh oh!
There was an error while loading. Please reload this page.
…es (pythonGH-153781) Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
| For controlling field-specific data, wrap the annotation in :class:`typing.Annotated` | ||
| with :class:`CFieldInfo` as the second argument, like so: |
There was a problem hiding this comment.
Maybe a silly question, but why does this use Annotated instead of dataclasses-style field: SomeType = field(anonymous=True) syntax? Did I miss a consensus on this?
There was a problem hiding this comment.
Annotated is more correct, isn't it? field() from dataclasses predates typing.Annotated, so that's why it's used there.
vstinner
commented
Jul 18, 2026
Oh, this new API looks easier to use than fromctypes.utilimportstructfromctypesimportc_int@structclassPoint:
x: c_inty: c_intpoint=Point(1, 2)
print(point) |
Uh oh!
There was an error while loading. Please reload this page.