PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/ctypes/test/test_incomplete.py

http://unladen-swallow.googlecode.com/
Python | 42 lines | 25 code | 11 blank | 6 comment | 3 complexity | 4b019df4609cba5a24cc70aeba86357d MD5 | raw file
 1import unittest
 2from ctypes import *
 3
 4################################################################
 5#
 6# The incomplete pointer example from the tutorial
 7#
 8
 9class MyTestCase(unittest.TestCase):
10
11    def test_incomplete_example(self):
12        lpcell = POINTER("cell")
13        class cell(Structure):
14            _fields_ = [("name", c_char_p),
15                        ("next", lpcell)]
16
17        SetPointerType(lpcell, cell)
18
19        c1 = cell()
20        c1.name = "foo"
21        c2 = cell()
22        c2.name = "bar"
23
24        c1.next = pointer(c2)
25        c2.next = pointer(c1)
26
27        p = c1
28
29        result = []
30        for i in range(8):
31            result.append(p.name)
32            p = p.next[0]
33        self.failUnlessEqual(result, ["foo", "bar"] * 4)
34
35        # to not leak references, we must clean _pointer_type_cache
36        from ctypes import _pointer_type_cache
37        del _pointer_type_cache[cell]
38
39################################################################
40
41if __name__ == '__main__':
42    unittest.main()