Home | History | Annotate | Download | only in test
      1 import unittest
      2 from ctypes import *
      3 
      4 ################################################################
      5 #
      6 # The incomplete pointer example from the tutorial
      7 #
      8 
      9 class 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.assertEqual(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 
     41 if __name__ == '__main__':
     42     unittest.main()
     43