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 c_void_p, c_char, c_int, c_long)
9 from test import support
10 from test.support import import_helper
11 from ._support import Py_TPFLAGS_DISALLOW_INSTANTIATION, Py_TPFLAGS_IMMUTABLETYPE
12
13
14 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
15 class FunctionCallTestCase(unittest.TestCase):
16 @unittest.skipUnless('MSC' in sys.version, "SEH only supported by MSC")
17 @unittest.skipIf(sys.executable.lower().endswith('_d.exe'),
18 "SEH not enabled in debug builds")
19 def test_SEH(self):
20 # Disable faulthandler to prevent logging the warning:
21 # "Windows fatal exception: access violation"
22 kernel32 = ctypes.windll.kernel32
23 with support.disable_faulthandler():
24 # Call functions with invalid arguments, and make sure
25 # that access violations are trapped and raise an
26 # exception.
27 self.assertRaises(OSError, kernel32.GetModuleHandleA, 32)
28
29 def test_noargs(self):
30 # This is a special case on win32 x64
31 user32 = ctypes.windll.user32
32 user32.GetDesktopWindow()
33
34
35 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
36 class ReturnStructSizesTestCase(unittest.TestCase):
37 def test_sizes(self):
38 _ctypes_test = import_helper.import_module("_ctypes_test")
39 dll = CDLL(_ctypes_test.__file__)
40 for i in range(1, 11):
41 fields = [ (f"f{f}", c_char) for f in range(1, i + 1)]
42 class S(Structure):
43 _fields_ = fields
44 f = getattr(dll, f"TestSize{i}")
45 f.restype = S
46 res = f()
47 for i, f in enumerate(fields):
48 value = getattr(res, f[0])
49 expected = bytes([ord('a') + i])
50 self.assertEqual(value, expected)
51
52
53 @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
54 class TestWintypes(unittest.TestCase):
55 def test_HWND(self):
56 from ctypes import wintypes
57 self.assertEqual(sizeof(wintypes.HWND), sizeof(c_void_p))
58
59 def test_PARAM(self):
60 from ctypes import wintypes
61 self.assertEqual(sizeof(wintypes.WPARAM),
62 sizeof(c_void_p))
63 self.assertEqual(sizeof(wintypes.LPARAM),
64 sizeof(c_void_p))
65
66 def test_COMError(self):
67 from ctypes import COMError
68 if support.HAVE_DOCSTRINGS:
69 self.assertEqual(COMError.__doc__,
70 "Raised when a COM method call failed.")
71
72 ex = COMError(-1, "text", ("descr", "source", "helpfile", 0, "progid"))
73 self.assertEqual(ex.hresult, -1)
74 self.assertEqual(ex.text, "text")
75 self.assertEqual(ex.details,
76 ("descr", "source", "helpfile", 0, "progid"))
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 self.assertIs(PointInRect.argtypes[0], ReturnRect.argtypes[2])
148 self.assertIs(PointInRect.argtypes[0], ReturnRect.argtypes[5])
149
150
151 if __name__ == '__main__':
152 unittest.main()