Debian Codegraph @

cpython27/Lib/ctypes/test/test_win32.py

raw symbols SCIP
1 # Windows specific tests
2
3 from ctypes import *
4 from ctypes.test import requires
5 import unittest, sys
6 from test import test_support as support
7
8 import _ctypes_test
9
10 # Only windows 32-bit has different calling conventions.
11 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
12 @unittest.skipUnless(sizeof(c_void_p) == sizeof(c_int),
13 "sizeof c_void_p and c_int differ")
14 class WindowsTestCase(unittest.TestCase):
15 def test_callconv_1(self):
16 # Testing stdcall function
17
18 IsWindow = windll.user32.IsWindow
19 # ValueError: Procedure probably called with not enough arguments
20 # (4 bytes missing)
21 self.assertRaises(ValueError, IsWindow)
22
23 # This one should succeed...
24 self.assertEqual(0, IsWindow(0))
25
26 # ValueError: Procedure probably called with too many arguments
27 # (8 bytes in excess)
28 self.assertRaises(ValueError, IsWindow, 0, 0, 0)
29
30 def test_callconv_2(self):
31 # Calling stdcall function as cdecl
32
33 IsWindow = cdll.user32.IsWindow
34
35 # ValueError: Procedure called with not enough arguments
36 # (4 bytes missing) or wrong calling convention
37 self.assertRaises(ValueError, IsWindow, None)
38
39 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
40 class FunctionCallTestCase(unittest.TestCase):
41 @unittest.skipUnless('MSC' in sys.version, "SEH only supported by MSC")
42 @unittest.skipIf(sys.executable.endswith('_d.exe'),
43 "SEH not enabled in debug builds")
44 def test_SEH(self):
45 requires("SEH")
46 # Call functions with invalid arguments, and make sure
47 # that access violations are trapped and raise an
48 # exception.
49 self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
50
51 def test_noargs(self):
52 # This is a special case on win32 x64
53 windll.user32.GetDesktopWindow()
54
55 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
56 class ReturnStructSizesTestCase(unittest.TestCase):
57 def test_sizes(self):
58 dll = CDLL(_ctypes_test.__file__)
59 for i in range(1, 11):
60 fields = [ ("f%d" % f, c_char) for f in range(1, i + 1)]
61 class S(Structure):
62 _fields_ = fields
63 f = getattr(dll, "TestSize%d" % i)
64 f.restype = S
65 res = f()
66 for i, f in enumerate(fields):
67 value = getattr(res, f[0])
68 expected = chr(ord('a') + i)
69 self.assertEquals(value, expected)
70
71
72
73 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
74 class TestWintypes(unittest.TestCase):
75 def test_HWND(self):
76 from ctypes import wintypes
77 self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
78
79 def test_PARAM(self):
80 from ctypes import wintypes
81 self.assertEqual(sizeof(wintypes.WPARAM),
82 sizeof(c_void_p))
83 self.assertEqual(sizeof(wintypes.LPARAM),
84 sizeof(c_void_p))
85
86 def test_COMError(self):
87 from _ctypes import COMError
88 if support.HAVE_DOCSTRINGS:
89 self.assertEqual(COMError.__doc__,
90 "Raised when a COM method call failed.")
91
92 ex = COMError(-1, "text", ("details",))
93 self.assertEqual(ex.hresult, -1)
94 self.assertEqual(ex.text, "text")
95 self.assertEqual(ex.details, ("details",))
96
97 class Structures(unittest.TestCase):
98 def test_struct_by_value(self):
99 class POINT(Structure):
100 _fields_ = [("x", c_long),
101 ("y", c_long)]
102
103 class RECT(Structure):
104 _fields_ = [("left", c_long),
105 ("top", c_long),
106 ("right", c_long),
107 ("bottom", c_long)]
108
109 dll = CDLL(_ctypes_test.__file__)
110
111 pt = POINT(15, 25)
112 left = c_long.in_dll(dll, 'left')
113 top = c_long.in_dll(dll, 'top')
114 right = c_long.in_dll(dll, 'right')
115 bottom = c_long.in_dll(dll, 'bottom')
116 rect = RECT(left, top, right, bottom)
117 PointInRect = dll.PointInRect
118 PointInRect.argtypes = [POINTER(RECT), POINT]
119 self.assertEqual(1, PointInRect(byref(rect), pt))
120
121 ReturnRect = dll.ReturnRect
122 ReturnRect.argtypes = [c_int, RECT, POINTER(RECT), POINT, RECT,
123 POINTER(RECT), POINT, RECT]
124 ReturnRect.restype = RECT
125 for i in range(4):
126 ret = ReturnRect(i, rect, pointer(rect), pt, rect,
127 byref(rect), pt, rect)
128 # the c function will check and modify ret if something is
129 # passed in improperly
130 self.assertEqual(ret.left, left.value)
131 self.assertEqual(ret.right, right.value)
132 self.assertEqual(ret.top, top.value)
133 self.assertEqual(ret.bottom, bottom.value)
134
135 # to not leak references, we must clean _pointer_type_cache
136 from ctypes import _pointer_type_cache
137 del _pointer_type_cache[RECT]
138
139 if __name__ == '__main__':
140 unittest.main()