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