~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This tests the internal _objects attribute
 
2
import unittest
 
3
from ctypes import *
 
4
from sys import getrefcount as grc
 
5
 
 
6
# XXX This test must be reviewed for correctness!!!
 
7
 
 
8
"""
 
9
ctypes' types are container types.
 
10
 
 
11
They have an internal memory block, which only consists of some bytes,
 
12
but it has to keep references to other objects as well. This is not
 
13
really needed for trivial C types like int or char, but it is important
 
14
for aggregate types like strings or pointers in particular.
 
15
 
 
16
What about pointers?
 
17
 
 
18
"""
 
19
 
 
20
class ObjectsTestCase(unittest.TestCase):
 
21
    def failUnlessSame(self, a, b):
 
22
        self.failUnlessEqual(id(a), id(b))
 
23
 
 
24
    def test_ints(self):
 
25
        i = 42000123
 
26
        rc = grc(i)
 
27
        ci = c_int(i)
 
28
        self.failUnlessEqual(rc, grc(i))
 
29
        self.failUnlessEqual(ci._objects, None)
 
30
 
 
31
    def test_c_char_p(self):
 
32
        s = b"Hello, World"
 
33
        rc = grc(s)
 
34
        cs = c_char_p(s)
 
35
        self.failUnlessEqual(rc + 1, grc(s))
 
36
        self.failUnlessSame(cs._objects, s)
 
37
 
 
38
    def test_simple_struct(self):
 
39
        class X(Structure):
 
40
            _fields_ = [("a", c_int), ("b", c_int)]
 
41
 
 
42
        a = 421234
 
43
        b = 421235
 
44
        x = X()
 
45
        self.failUnlessEqual(x._objects, None)
 
46
        x.a = a
 
47
        x.b = b
 
48
        self.failUnlessEqual(x._objects, None)
 
49
 
 
50
    def test_embedded_structs(self):
 
51
        class X(Structure):
 
52
            _fields_ = [("a", c_int), ("b", c_int)]
 
53
 
 
54
        class Y(Structure):
 
55
            _fields_ = [("x", X), ("y", X)]
 
56
 
 
57
        y = Y()
 
58
        self.failUnlessEqual(y._objects, None)
 
59
 
 
60
        x1, x2 = X(), X()
 
61
        y.x, y.y = x1, x2
 
62
        self.failUnlessEqual(y._objects, {"0": {}, "1": {}})
 
63
        x1.a, x2.b = 42, 93
 
64
        self.failUnlessEqual(y._objects, {"0": {}, "1": {}})
 
65
 
 
66
    def test_xxx(self):
 
67
        class X(Structure):
 
68
            _fields_ = [("a", c_char_p), ("b", c_char_p)]
 
69
 
 
70
        class Y(Structure):
 
71
            _fields_ = [("x", X), ("y", X)]
 
72
 
 
73
        s1 = "Hello, World"
 
74
        s2 = "Hallo, Welt"
 
75
 
 
76
        x = X()
 
77
        x.a = s1
 
78
        x.b = s2
 
79
        self.failUnlessEqual(x._objects, {"0": bytes(s1, "ascii"),
 
80
                                          "1": bytes(s2, "ascii")})
 
81
 
 
82
        y = Y()
 
83
        y.x = x
 
84
        self.failUnlessEqual(y._objects, {"0": {"0": bytes(s1, "ascii"),
 
85
                                                "1": bytes(s2, "ascii")}})
 
86
##        x = y.x
 
87
##        del y
 
88
##        print x._b_base_._objects
 
89
 
 
90
    def test_ptr_struct(self):
 
91
        class X(Structure):
 
92
            _fields_ = [("data", POINTER(c_int))]
 
93
 
 
94
        A = c_int*4
 
95
        a = A(11, 22, 33, 44)
 
96
        self.failUnlessEqual(a._objects, None)
 
97
 
 
98
        x = X()
 
99
        x.data = a
 
100
##XXX        print x._objects
 
101
##XXX        print x.data[0]
 
102
##XXX        print x.data._objects
 
103
 
 
104
if __name__ == '__main__':
 
105
    unittest.main()