Skip to content
Merged
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
3 changes: 2 additions & 1 deletion Lib/ctypes/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ def _process_struct(decorated_class, /, *, align, layout, endian, pack):
fields.extend(decorated_class._fields_)
anonymous.extend(decorated_class._anonymous_)

for name, hint in annotationlib.get_annotations(decorated_class).items():
annotations = annotationlib.get_annotations(decorated_class, eval_str=True)
for name, hint in annotations.items():
if get_origin(hint) is ClassVar:
continue

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_ctypes/struct_str_ann.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import annotations
from ctypes.util import struct
from ctypes import c_int

class TestAnn:
x: c_int

# Check that "from __future__ import annotations" works as expected
if not isinstance(TestAnn.__annotations__['x'], str):
raise Exception("annotations must be strings")

@struct
class Point:
x: c_int
y: c_int
13 changes: 10 additions & 3 deletions Lib/test/test_ctypes/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ class X(Structure):
self.assertEqual(sizeof(X), min(8, longlong_align) + longlong_size)
self.assertEqual(X.b.offset, min(8, longlong_align))

with self.assertRaises(ValueError):
if use_struct_util:
if use_struct_util:
with self.assertRaises(NameError):
@struct_util(pack=-1, layout='ms')
class X:
a: "b"
b: "q"
else:
else:
with self.assertRaises(ValueError):
class X(Structure):
_fields_ = [("a", "b"), ("b", "q")]
_pack_ = -1
Expand Down Expand Up @@ -954,6 +955,12 @@ class Foo:

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

def test_string_annotations(self):
from test.test_ctypes import struct_str_ann
Point = struct_str_ann.Point
fields = [['x', c_int], ['y', c_int]]

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.

This line should be updated if PR gh-154038 is merged first.

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.

Crap. I missed my own comment when I merged this PR, and now the pre-commit CI fails on the main branch :-( I wrote #154091 to fix the CI.

self.assertEqual(Point._fields_, fields)


if __name__ == '__main__':
unittest.main()
Loading