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

« back to all changes in this revision

Viewing changes to Lib/ctypes/test/test_win32.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
# Windows specific tests
 
2
 
 
3
from ctypes import *
 
4
from ctypes.test import is_resource_enabled
 
5
import unittest, sys
 
6
 
 
7
import _ctypes_test
 
8
 
 
9
if sys.platform == "win32" and sizeof(c_void_p) == sizeof(c_int):
 
10
    # Only windows 32-bit has different calling conventions.
 
11
 
 
12
    class WindowsTestCase(unittest.TestCase):
 
13
        def test_callconv_1(self):
 
14
            # Testing stdcall function
 
15
 
 
16
            IsWindow = windll.user32.IsWindow
 
17
            # ValueError: Procedure probably called with not enough arguments (4 bytes missing)
 
18
            self.assertRaises(ValueError, IsWindow)
 
19
 
 
20
            # This one should succeeed...
 
21
            self.failUnlessEqual(0, IsWindow(0))
 
22
 
 
23
            # ValueError: Procedure probably called with too many arguments (8 bytes in excess)
 
24
            self.assertRaises(ValueError, IsWindow, 0, 0, 0)
 
25
 
 
26
        def test_callconv_2(self):
 
27
            # Calling stdcall function as cdecl
 
28
 
 
29
            IsWindow = cdll.user32.IsWindow
 
30
 
 
31
            # ValueError: Procedure called with not enough arguments (4 bytes missing)
 
32
            # or wrong calling convention
 
33
            self.assertRaises(ValueError, IsWindow, None)
 
34
 
 
35
if sys.platform == "win32":
 
36
    class FunctionCallTestCase(unittest.TestCase):
 
37
 
 
38
        if is_resource_enabled("SEH"):
 
39
            def test_SEH(self):
 
40
                # Call functions with invalid arguments, and make sure
 
41
                # that access violations are trapped and raise an
 
42
                # exception.
 
43
                self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
 
44
 
 
45
        def test_noargs(self):
 
46
            # This is a special case on win32 x64
 
47
            windll.user32.GetDesktopWindow()
 
48
 
 
49
    class TestWintypes(unittest.TestCase):
 
50
        def test_HWND(self):
 
51
            from ctypes import wintypes
 
52
            self.failUnlessEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
 
53
 
 
54
        def test_PARAM(self):
 
55
            from ctypes import wintypes
 
56
            self.failUnlessEqual(sizeof(wintypes.WPARAM),
 
57
                                 sizeof(c_void_p))
 
58
            self.failUnlessEqual(sizeof(wintypes.LPARAM),
 
59
                                 sizeof(c_void_p))
 
60
 
 
61
        def test_COMError(self):
 
62
            from _ctypes import COMError
 
63
            self.assertEqual(COMError.__doc__, "Raised when a COM method call failed.")
 
64
 
 
65
            ex = COMError(-1, "text", ("details",))
 
66
            self.assertEqual(ex.hresult, -1)
 
67
            self.assertEqual(ex.text, "text")
 
68
            self.assertEqual(ex.details, ("details",))
 
69
 
 
70
class Structures(unittest.TestCase):
 
71
 
 
72
    def test_struct_by_value(self):
 
73
        class POINT(Structure):
 
74
            _fields_ = [("x", c_long),
 
75
                        ("y", c_long)]
 
76
 
 
77
        class RECT(Structure):
 
78
            _fields_ = [("left", c_long),
 
79
                        ("top", c_long),
 
80
                        ("right", c_long),
 
81
                        ("bottom", c_long)]
 
82
 
 
83
        dll = CDLL(_ctypes_test.__file__)
 
84
 
 
85
        pt = POINT(10, 10)
 
86
        rect = RECT(0, 0, 20, 20)
 
87
        self.failUnlessEqual(1, dll.PointInRect(byref(rect), pt))
 
88
 
 
89
if __name__ == '__main__':
 
90
    unittest.main()