FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-104533: Add a dataclass-like decorator for `ctypes` structures by ZeroIntensity · Pull Request #153781 · python/cpython · GitHub

Repository navigation

gh-104533: Add a dataclass-like decorator for ctypes structures - #153781

Merged
ZeroIntensity merged 10 commits into
python:mainfrom
ZeroIntensity:ctypes/struct-decorator
Jul 18, 2026
Merged

ZeroIntensity merged 10 commits into
python:mainfrom
ZeroIntensity:ctypes/struct-decorator

Conversation

ZeroIntensity commented Jul 15, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

read-the-docs-community Bot commented Jul 15, 2026 •
edited
Loading

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33620062 | 📁 Comparing 2e469b6 against main (b4662e8)

  🔍 Preview build  

3 files changed
± library/ctypes.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py Outdated
ZeroIntensity marked this pull request as ready for review July 16, 2026 09:39
ZeroIntensity requested a review from AA-Turner as a code owner July 16, 2026 09:39
Comment thread Lib/ctypes/util.py Outdated
Comment thread Lib/ctypes/util.py
Comment on lines +561 to +576
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)

johnslavik Jul 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I meant it like this:

Suggested change
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)
def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None):
if class_or_none is None:
return functools.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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

process_the_struct isn't defined here; what are you trying to show?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Edited the suggestion

Comment thread Doc/library/ctypes.rst
Comment on lines +3164 to +3165
.. decorator:: struct(*, align=None, layout, endian='native', pack=None)
:module: ctypes.util

johnslavik Jul 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Consider adding a collation: old syntax vs. new syntax.

ZeroIntensity Jul 16, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I don't think that's necessary -- the examples for Structure are right above.

Comment thread Lib/ctypes/util.py Outdated
ZeroIntensity and others added 2 commits July 16, 2026 12:01
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
ZeroIntensity merged commit 5b85756 into python:main Jul 18, 2026
51 checks passed
ZeroIntensity deleted the ctypes/struct-decorator branch July 18, 2026 08:48
fedonman pushed a commit to fedonman/cpython that referenced this pull request Jul 18, 2026
…es (pythonGH-153781)

Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Comment thread Doc/library/ctypes.rst
Comment on lines +3196 to +3197
For controlling field-specific data, wrap the annotation in :class:`typing.Annotated`
with :class:`CFieldInfo` as the second argument, like so:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?

ZeroIntensity Jul 18, 2026 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Annotated is more correct, isn't it? field() from dataclasses predates typing.Annotated, so that's why it's used there.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yes, it is more correct.

Copy link
Copy Markdown
Member

Oh, this new API looks easier to use than ctypes.Structure, I'm excited!

from ctypes.util import struct
from ctypes import c_int

@struct
class Point:
    x: c_int
    y: c_int

point = Point(1, 2)
print(point)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants


Back | FazBrowse Home | New Git URL