summaryrefslogtreecommitdiff
path: root/lib/python2.7/ctypes/test/test_incomplete.py
blob: 1e03e9fcd50fe9f2f487c7ba8414552ba12d46c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unittest
from ctypes import *

################################################################
#
# The incomplete pointer example from the tutorial
#

class MyTestCase(unittest.TestCase):

    def test_incomplete_example(self):
        lpcell = POINTER("cell")
        class cell(Structure):
            _fields_ = [("name", c_char_p),
                        ("next", lpcell)]

        SetPointerType(lpcell, cell)

        c1 = cell()
        c1.name = "foo"
        c2 = cell()
        c2.name = "bar"

        c1.next = pointer(c2)
        c2.next = pointer(c1)

        p = c1

        result = []
        for i in range(8):
            result.append(p.name)
            p = p.next[0]
        self.assertEqual(result, ["foo", "bar"] * 4)

        # to not leak references, we must clean _pointer_type_cache
        from ctypes import _pointer_type_cache
        del _pointer_type_cache[cell]

################################################################

if __name__ == '__main__':
    unittest.main()