Debian Codegraph @

extra_tests/ctypes_tests/test_anon.py

raw symbols SCIP
1 import pytest
2 from ctypes import *
3
4 @pytest.mark.pypy_only
5 def test_nested():
6 class ANON_S(Structure):
7 _fields_ = [("a", c_int)]
8
9 class ANON_U(Union):
10 _fields_ = [("_", ANON_S),
11 ("b", c_int)]
12 _anonymous_ = ["_"]
13
14 class Y(Structure):
15 _fields_ = [("x", c_int),
16 ("_", ANON_U),
17 ("y", c_int)]
18 _anonymous_ = ["_"]
19
20 assert Y.x.offset == 0
21 assert Y.a.offset == sizeof(c_int)
22 assert Y.b.offset == sizeof(c_int)
23 assert Y._.offset == sizeof(c_int)
24 assert Y.y.offset == sizeof(c_int) * 2
25
26 assert Y._names_ == ['x', 'a', 'b', 'y']
27
28 def test_anonymous_fields_on_instance():
29 # this is about the *instance-level* access of anonymous fields,
30 # which you'd guess is the most common, but used not to work
31 # (issue #2230)
32
33 class B(Structure):
34 _fields_ = [("x", c_int), ("y", c_int), ("z", c_int)]
35 class A(Structure):
36 _anonymous_ = ["b"]
37 _fields_ = [("b", B)]
38
39 a = A()
40 a.x = 5
41 assert a.x == 5
42 assert a.b.x == 5
43 a.b.x += 1
44 assert a.x == 6
45
46 class C(Structure):
47 _anonymous_ = ["a"]
48 _fields_ = [("v", c_int), ("a", A)]
49
50 c = C()
51 c.v = 3
52 c.y = -8
53 assert c.v == 3
54 assert c.y == c.a.y == c.a.b.y == -8
55 assert not hasattr(c, 'b')