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
--- /dev/null
+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
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
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]]
+ self.assertEqual(Point._fields_, fields)
+
if __name__ == '__main__':
unittest.main()