~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_incomplete.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.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
 
 
41
if __name__ == '__main__':
 
42
    unittest.main()