Update to 2.7.3
[profile/ivi/python.git] / Lib / ctypes / test / test_init.py
1 from ctypes import *
2 import unittest
3
4 class X(Structure):
5     _fields_ = [("a", c_int),
6                 ("b", c_int)]
7     new_was_called = False
8
9     def __new__(cls):
10         result = super(X, cls).__new__(cls)
11         result.new_was_called = True
12         return result
13
14     def __init__(self):
15         self.a = 9
16         self.b = 12
17
18 class Y(Structure):
19     _fields_ = [("x", X)]
20
21
22 class InitTest(unittest.TestCase):
23     def test_get(self):
24         # make sure the only accessing a nested structure
25         # doesn't call the structure's __new__ and __init__
26         y = Y()
27         self.assertEqual((y.x.a, y.x.b), (0, 0))
28         self.assertEqual(y.x.new_was_called, False)
29
30         # But explicitly creating an X structure calls __new__ and __init__, of course.
31         x = X()
32         self.assertEqual((x.a, x.b), (9, 12))
33         self.assertEqual(x.new_was_called, True)
34
35         y.x = x
36         self.assertEqual((y.x.a, y.x.b), (9, 12))
37         self.assertEqual(y.x.new_was_called, False)
38
39 if __name__ == "__main__":
40     unittest.main()