Debian Codegraph @

Lib/test/test_ctypes/test_win32.py

raw
1 # Windows specific tests
2
3 import ctypes
4 import errno
5 import sys
6 import unittest
7 from ctypes import (CDLL, Structure, POINTER, pointer, sizeof, byref,
8 _pointer_type_cache,
9 c_void_p, c_char, c_int, c_long)
10 from test import support
11 from test.support import import_helper
12 from ._support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE
13
14
15 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
16 class FunctionCallTestCase(unittest.TestCase):
17 @unittest.skipUnless('MSC' in sys.version, "SEH only supported by MSC")
18 @unittest.skipIf(sys.executable.lower().endswith('_d.exe'),
19 "SEH not enabled in debug builds")
20 def test_SEH(self):
21 # Disable faulthandler to prevent logging the warning:
22 # "Windows fatal exception: access violation"
23 kernel32 = ctypes.windll.kernel32
24 with support.disable_faulthandler():
25 # Call functions with invalid arguments, and make sure
26 # that access violations are trapped and raise an
27 # exception.
28 self.assertRaises(OSError, kernel32.GetModuleHandleA, 32)
29
30 def test_noargs(self):
31 # This is a special case on win32 x64
32 user32 = ctypes.windll.user32
33 user32.GetDesktopWindow()
34
35
36 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
37 class ReturnStructSizesTestCase(unittest.TestCase):
38 def test_sizes(self):
39 _ctypes_test = import_helper.import_module("_ctypes_test")
40 dll = CDLL(_ctypes_test.__file__)
41 for i in range(1, 11):
42 fields = [ (f"f{f}", c_char) for f in range(1, i + 1)]
43 class S(Structure):
44 _fields_ = fields
45 f = getattr(dll, f"TestSize{i}")
46 f.restype = S
47 res = f()
48 for i, f in enumerate(fields):
49 value = getattr(res, f[0])
50 expected = bytes([ord('a') + i])
51 self.assertEqual(value, expected)
52
53
54 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
55 class TestWintypes(unittest.TestCase):
56 def test_HWND(self):
57 from ctypes import wintypes
58 self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
59
60 def test_PARAM(self):
61 from ctypes import wintypes
62 self.assertEqual(sizeof(wintypes.WPARAM),
63 sizeof(c_void_p))
64 self.assertEqual(sizeof(wintypes.LPARAM),
65 sizeof(c_void_p))
66
67 def test_COMError(self):
68 from _ctypes import COMError
69 if support.HAVE_DOCSTRINGS:
70 self.assertEqual(COMError.__doc__,
71 "Raised when a COM method call failed.")
72
73 ex = COMError(-1, "text", ("details",))
74 self.assertEqual(ex.hresult, -1)
75 self.assertEqual(ex.text, "text")
76 self.assertEqual(ex.details, ("details",))
77
78 self.assertEqual(COMError.mro(),
79 [COMError, Exception, BaseException, object])
80 self.assertFalse(COMError.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)
81 self.assertTrue(COMError.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
82
83
84 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
85 class TestWinError(unittest.TestCase):
86 def test_winerror(self):
87 # see Issue 16169
88 ERROR_INVALID_PARAMETER = 87
89 msg = ctypes.FormatError(ERROR_INVALID_PARAMETER).strip()
90 args = (errno.EINVAL, msg, None, ERROR_INVALID_PARAMETER)
91
92 e = ctypes.WinError(ERROR_INVALID_PARAMETER)
93 self.assertEqual(e.args, args)
94 self.assertEqual(e.errno, errno.EINVAL)
95 self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)
96
97 kernel32 = ctypes.windll.kernel32
98 kernel32.SetLastError(ERROR_INVALID_PARAMETER)
99 try:
100 raise ctypes.WinError()
101 except OSError as exc:
102 e = exc
103 self.assertEqual(e.args, args)
104 self.assertEqual(e.errno, errno.EINVAL)
105 self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)
106
107
108 class Structures(unittest.TestCase):
109 def test_struct_by_value(self):
110 class POINT(Structure):
111 _fields_ = [("x", c_long),
112 ("y", c_long)]
113
114 class RECT(Structure):
115 _fields_ = [("left", c_long),
116 ("top", c_long),
117 ("right", c_long),
118 ("bottom", c_long)]
119
120 _ctypes_test = import_helper.import_module("_ctypes_test")
121 dll = CDLL(_ctypes_test.__file__)
122
123 pt = POINT(15, 25)
124 left = c_long.in_dll(dll, 'left')
125 top = c_long.in_dll(dll, 'top')
126 right = c_long.in_dll(dll, 'right')
127 bottom = c_long.in_dll(dll, 'bottom')
128 rect = RECT(left, top, right, bottom)
129 PointInRect = dll.PointInRect
130 PointInRect.argtypes = [POINTER(RECT), POINT]
131 self.assertEqual(1, PointInRect(byref(rect), pt))
132
133 ReturnRect = dll.ReturnRect
134 ReturnRect.argtypes = [c_int, RECT, POINTER(RECT), POINT, RECT,
135 POINTER(RECT), POINT, RECT]
136 ReturnRect.restype = RECT
137 for i in range(4):
138 ret = ReturnRect(i, rect, pointer(rect), pt, rect,
139 byref(rect), pt, rect)
140 # the c function will check and modify ret if something is
141 # passed in improperly
142 self.assertEqual(ret.left, left.value)
143 self.assertEqual(ret.right, right.value)
144 self.assertEqual(ret.top, top.value)
145 self.assertEqual(ret.bottom, bottom.value)
146
147 # to not leak references, we must clean _pointer_type_cache
148 del _pointer_type_cache[RECT]
149
150
151 if __name__ == '__main__':
152 unittest.main()