File: test_simplesubclasses.py

package info (click to toggle)
python2.6 2.6.8-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 65,740 kB
  • sloc: ansic: 389,342; python: 376,882; asm: 9,734; sh: 4,934; makefile: 4,040; lisp: 2,933; objc: 775; xml: 62
file content (57 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download | duplicates (3)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import unittest
from ctypes import *

class MyInt(c_int):
    def __cmp__(self, other):
        if type(other) != MyInt:
            return -1
        return cmp(self.value, other.value)
    def __hash__(self): # Silence Py3k warning
        return hash(self.value)

class Test(unittest.TestCase):

    def test_compare(self):
        self.failUnlessEqual(MyInt(3), MyInt(3))
        self.failIfEqual(MyInt(42), MyInt(43))

    def test_ignore_retval(self):
        # Test if the return value of a callback is ignored
        # if restype is None
        proto = CFUNCTYPE(None)
        def func():
            return (1, "abc", None)

        cb = proto(func)
        self.failUnlessEqual(None, cb())


    def test_int_callback(self):
        args = []
        def func(arg):
            args.append(arg)
            return arg

        cb = CFUNCTYPE(None, MyInt)(func)

        self.failUnlessEqual(None, cb(42))
        self.failUnlessEqual(type(args[-1]), MyInt)

        cb = CFUNCTYPE(c_int, c_int)(func)

        self.failUnlessEqual(42, cb(42))
        self.failUnlessEqual(type(args[-1]), int)

    def test_int_struct(self):
        class X(Structure):
            _fields_ = [("x", MyInt)]

        self.failUnlessEqual(X().x, MyInt())

        s = X()
        s.x = MyInt(42)

        self.failUnlessEqual(s.x, MyInt(42))

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