convert usage of fail* to assert*
[python.git] / Lib / ctypes / test / test_init.py
blob953a14551a734aa2b0e4d565d6d9a05eefc6bc5c
1 from ctypes import *
2 import unittest
4 class X(Structure):
5 _fields_ = [("a", c_int),
6 ("b", c_int)]
7 new_was_called = False
9 def __new__(cls):
10 result = super(X, cls).__new__(cls)
11 result.new_was_called = True
12 return result
14 def __init__(self):
15 self.a = 9
16 self.b = 12
18 class Y(Structure):
19 _fields_ = [("x", X)]
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)
30 # But explicitely 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)
35 y.x = x
36 self.assertEqual((y.x.a, y.x.b), (9, 12))
37 self.assertEqual(y.x.new_was_called, False)
39 if __name__ == "__main__":
40 unittest.main()