Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from ctypes import * |
| 3 | |
| 4 | class StructFieldsTestCase(unittest.TestCase): |
| 5 | # Structure/Union classes must get 'finalized' sooner or |
| 6 | # later, when one of these things happen: |
| 7 | # |
| 8 | # 1. _fields_ is set. |
| 9 | # 2. An instance is created. |
| 10 | # 3. The type is used as field of another Structure/Union. |
| 11 | # 4. The type is subclassed |
| 12 | # |
| 13 | # When they are finalized, assigning _fields_ is no longer allowed. |
| 14 | |
| 15 | def test_1_A(self): |
| 16 | class X(Structure): |
| 17 | pass |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 18 | self.assertEqual(sizeof(X), 0) # not finalized |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 19 | X._fields_ = [] # finalized |
| 20 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 21 | |
| 22 | def test_1_B(self): |
| 23 | class X(Structure): |
| 24 | _fields_ = [] # finalized |
| 25 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 26 | |
| 27 | def test_2(self): |
| 28 | class X(Structure): |
| 29 | pass |
| 30 | X() |
| 31 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 32 | |
| 33 | def test_3(self): |
| 34 | class X(Structure): |
| 35 | pass |
| 36 | class Y(Structure): |
| 37 | _fields_ = [("x", X)] # finalizes X |
| 38 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 39 | |
| 40 | def test_4(self): |
| 41 | class X(Structure): |
| 42 | pass |
| 43 | class Y(X): |
| 44 | pass |
| 45 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 46 | Y._fields_ = [] |
| 47 | self.assertRaises(AttributeError, setattr, X, "_fields_", []) |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | unittest.main() |