ยปCore Development>Code coverage>Lib/ctypes/test/test_win32.py

Python code coverage for Lib/ctypes/test/test_win32.py

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