Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Lib/ctypes/util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys

from dataclasses import dataclass
from dataclasses import dataclass, _update_func_cell_for__class__

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.

I think it's a really bad idea for us to rely on dataclass internals like this.

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.

It is a bad idea.


lazy import functools
lazy import inspect
Expand Down Expand Up @@ -478,6 +478,17 @@ class _Struct(endian_class):
_fields_ = fields
_anonymous_ = anonymous

for member in vars(_Struct).values():
if not isinstance(member, type) and hasattr(member, '__wrapped__'):
member = inspect.unwrap(member)
if isinstance(member, property):
functions = (member.fget, member.fset, member.fdel)
else:
functions = (member,)
for func in functions:
if inspect.isfunction(func):
_update_func_cell_for__class__(func, decorated_class, _Struct)

return _Struct


Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,37 @@ class Foo:

self.assertEqual(Foo.__name__, "Foo")

@subTests("endian", ["native", "little", "big"])
def test_struct_util_class_cell(self, endian):
@struct_util(endian=endian)
class Foo:
x: c_int

def __init__(self, x=0):
super().__init__(x)

self.assertEqual(Foo(1).x, 1)

@struct_util(endian=endian)
class Foo:
x: c_int

@classmethod
def create(cls):
return super().__new__(cls)

self.assertIsInstance(Foo.create(), Foo)

@struct_util(endian=endian)
class Foo:
x: c_int

@property
def cls(self):
return __class__

self.assertIs(Foo().cls, Foo)

def test_string_annotations(self):
from test.test_ctypes import struct_str_ann
Point = struct_str_ann.Point
Expand Down
Loading