PageRenderTime 382ms CodeModel.GetById 143ms app.highlight 6ms RepoModel.GetById 226ms app.codeStats 1ms

/Lib/ctypes/test/test_simplesubclasses.py

http://unladen-swallow.googlecode.com/
Python | 57 lines | 39 code | 16 blank | 2 comment | 2 complexity | 9f6211068f389b8a95345309d7bfd877 MD5 | raw file
 1import unittest
 2from ctypes import *
 3
 4class MyInt(c_int):
 5    def __cmp__(self, other):
 6        if type(other) != MyInt:
 7            return -1
 8        return cmp(self.value, other.value)
 9    def __hash__(self): # Silence Py3k warning
10        return hash(self.value)
11
12class Test(unittest.TestCase):
13
14    def test_compare(self):
15        self.failUnlessEqual(MyInt(3), MyInt(3))
16        self.failIfEqual(MyInt(42), MyInt(43))
17
18    def test_ignore_retval(self):
19        # Test if the return value of a callback is ignored
20        # if restype is None
21        proto = CFUNCTYPE(None)
22        def func():
23            return (1, "abc", None)
24
25        cb = proto(func)
26        self.failUnlessEqual(None, cb())
27
28
29    def test_int_callback(self):
30        args = []
31        def func(arg):
32            args.append(arg)
33            return arg
34
35        cb = CFUNCTYPE(None, MyInt)(func)
36
37        self.failUnlessEqual(None, cb(42))
38        self.failUnlessEqual(type(args[-1]), MyInt)
39
40        cb = CFUNCTYPE(c_int, c_int)(func)
41
42        self.failUnlessEqual(42, cb(42))
43        self.failUnlessEqual(type(args[-1]), int)
44
45    def test_int_struct(self):
46        class X(Structure):
47            _fields_ = [("x", MyInt)]
48
49        self.failUnlessEqual(X().x, MyInt())
50
51        s = X()
52        s.x = MyInt(42)
53
54        self.failUnlessEqual(s.x, MyInt(42))
55
56if __name__ == "__main__":
57    unittest.main()