Skip to content

Commit 9713e30

Browse files
committed
gh-105751: test_ctypes gets Windows attrs from ctypes
test_ctypes now gets attributes specific to Windows from the ctypes module, rather than relying on "from ctypes import *". Attributes: * ctypes.FormatError * ctypes.WINFUNCTYPE * ctypes.WinError * ctypes.WinDLL * ctypes.windll * ctypes.oledll * ctypes.get_last_error() * ctypes.set_last_error()
1 parent f3266c0 commit 9713e30

11 files changed

+75
-64
lines changed

Lib/test/test_ctypes/test_as_parameter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import unittest
2+
import ctypes
23
from ctypes import *
34
from test.test_ctypes import need_symbol
45
import _ctypes_test
56

67
dll = CDLL(_ctypes_test.__file__)
78

89
try:
9-
CALLBACK_FUNCTYPE = WINFUNCTYPE
10-
except NameError:
10+
CALLBACK_FUNCTYPE = ctypes.WINFUNCTYPE
11+
except AttributeError:
1112
# fake to enable this test on Linux
1213
CALLBACK_FUNCTYPE = CFUNCTYPE
1314

Lib/test/test_ctypes/test_callbacks.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
from test import support
44

5+
import ctypes
56
from ctypes import *
67
from test.test_ctypes import need_symbol
78
from _ctypes import CTYPES_MAX_ARGCOUNT
@@ -152,7 +153,7 @@ def __del__(self):
152153

153154
@need_symbol('WINFUNCTYPE')
154155
def test_i38748_stackCorruption(self):
155-
callback_funcType = WINFUNCTYPE(c_long, c_long, c_longlong)
156+
callback_funcType = ctypes.WINFUNCTYPE(c_long, c_long, c_longlong)
156157
@callback_funcType
157158
def callback(a, b):
158159
c = a + b
@@ -163,12 +164,10 @@ def callback(a, b):
163164
self.assertEqual(dll._test_i38748_runCallback(callback, 5, 10), 15)
164165

165166

166-
@need_symbol('WINFUNCTYPE')
167-
class StdcallCallbacks(Callbacks):
168-
try:
169-
functype = WINFUNCTYPE
170-
except NameError:
171-
pass
167+
if hasattr(ctypes, 'WINFUNCTYPE'):
168+
class StdcallCallbacks(Callbacks):
169+
functype = ctypes.WINFUNCTYPE
170+
172171

173172
################################################################
174173

@@ -216,13 +215,14 @@ def test_issue_8959_b(self):
216215
global windowCount
217216
windowCount = 0
218217

219-
@WINFUNCTYPE(BOOL, HWND, LPARAM)
218+
@ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM)
220219
def EnumWindowsCallbackFunc(hwnd, lParam):
221220
global windowCount
222221
windowCount += 1
223222
return True #Allow windows to keep enumerating
224223

225-
windll.user32.EnumWindows(EnumWindowsCallbackFunc, 0)
224+
user32 = ctypes.windll.user32
225+
user32.EnumWindows(EnumWindowsCallbackFunc, 0)
226226

227227
def test_callback_register_int(self):
228228
# Issue #8275: buggy handling of callback args under Win64

Lib/test/test_ctypes/test_cfuncs.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Byte order related?
33

44
import unittest
5+
import ctypes
56
from ctypes import *
67
from test.test_ctypes import need_symbol
78

@@ -197,22 +198,17 @@ def test_void(self):
197198

198199
# The following repeats the above tests with stdcall functions (where
199200
# they are available)
200-
try:
201-
WinDLL
202-
except NameError:
203-
def stdcall_dll(*_): pass
204-
else:
205-
class stdcall_dll(WinDLL):
201+
if hasattr(ctypes, 'WinDLL'):
202+
class stdcall_dll(ctypes.WinDLL):
206203
def __getattr__(self, name):
207204
if name[:2] == '__' and name[-2:] == '__':
208205
raise AttributeError(name)
209206
func = self._FuncPtr(("s_" + name, self))
210207
setattr(self, name, func)
211208
return func
212209

213-
@need_symbol('WinDLL')
214-
class stdcallCFunctions(CFunctions):
215-
_dll = stdcall_dll(_ctypes_test.__file__)
210+
class stdcallCFunctions(CFunctions):
211+
_dll = stdcall_dll(_ctypes_test.__file__)
216212

217213
if __name__ == '__main__':
218214
unittest.main()

Lib/test/test_ctypes/test_checkretval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22

3+
import ctypes
34
from ctypes import *
45
from test.test_ctypes import need_symbol
56

@@ -28,9 +29,8 @@ def test_checkretval(self):
2829

2930
@need_symbol('oledll')
3031
def test_oledll(self):
31-
self.assertRaises(OSError,
32-
oledll.oleaut32.CreateTypeLib2,
33-
0, None, None)
32+
oleaut32 = ctypes.oledll.oleaut32
33+
self.assertRaises(OSError, oleaut32.CreateTypeLib2, 0, None, None)
3434

3535
if __name__ == "__main__":
3636
unittest.main()

Lib/test/test_ctypes/test_errno.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest, os, errno
22
import threading
33

4+
import ctypes
45
from ctypes import *
56
from ctypes.util import find_library
67

@@ -44,33 +45,33 @@ def _worker():
4445

4546
@unittest.skipUnless(os.name == "nt", 'Test specific to Windows')
4647
def test_GetLastError(self):
47-
dll = WinDLL("kernel32", use_last_error=True)
48+
dll = ctypes.WinDLL("kernel32", use_last_error=True)
4849
GetModuleHandle = dll.GetModuleHandleA
4950
GetModuleHandle.argtypes = [c_wchar_p]
5051

5152
self.assertEqual(0, GetModuleHandle("foo"))
52-
self.assertEqual(get_last_error(), 126)
53+
self.assertEqual(ctypes.get_last_error(), 126)
5354

54-
self.assertEqual(set_last_error(32), 126)
55-
self.assertEqual(get_last_error(), 32)
55+
self.assertEqual(ctypes.set_last_error(32), 126)
56+
self.assertEqual(ctypes.get_last_error(), 32)
5657

5758
def _worker():
58-
set_last_error(0)
59+
ctypes.set_last_error(0)
5960

60-
dll = WinDLL("kernel32", use_last_error=False)
61+
dll = ctypes.WinDLL("kernel32", use_last_error=False)
6162
GetModuleHandle = dll.GetModuleHandleW
6263
GetModuleHandle.argtypes = [c_wchar_p]
6364
GetModuleHandle("bar")
6465

65-
self.assertEqual(get_last_error(), 0)
66+
self.assertEqual(ctypes.get_last_error(), 0)
6667

6768
t = threading.Thread(target=_worker)
6869
t.start()
6970
t.join()
7071

71-
self.assertEqual(get_last_error(), 32)
72+
self.assertEqual(ctypes.get_last_error(), 32)
7273

73-
set_last_error(0)
74+
ctypes.set_last_error(0)
7475

7576
if __name__ == "__main__":
7677
unittest.main()

Lib/test/test_ctypes/test_funcptr.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import unittest
2+
import ctypes
23
from ctypes import *
34

45
try:
5-
WINFUNCTYPE
6-
except NameError:
6+
WINFUNCTYPE = ctypes.WINFUNCTYPE
7+
except AttributeError:
78
# fake to enable this test on Linux
89
WINFUNCTYPE = CFUNCTYPE
910

@@ -39,7 +40,7 @@ def func(a, b):
3940
# possible, as in C, to call cdecl functions with more parameters.
4041
#self.assertRaises(TypeError, c, 1, 2, 3)
4142
self.assertEqual(c(1, 2, 3, 4, 5, 6), 3)
42-
if not WINFUNCTYPE is CFUNCTYPE:
43+
if WINFUNCTYPE is not CFUNCTYPE:
4344
self.assertRaises(TypeError, s, 1, 2, 3)
4445

4546
def test_structures(self):
@@ -91,7 +92,7 @@ def test_dllfunctions(self):
9192

9293
def NoNullHandle(value):
9394
if not value:
94-
raise WinError()
95+
raise ctypes.WinError()
9596
return value
9697

9798
strchr = lib.my_strchr

Lib/test/test_ctypes/test_functions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
Later...
66
"""
77

8+
import ctypes
89
from ctypes import *
910
from test.test_ctypes import need_symbol
1011
import sys, unittest
1112

1213
try:
13-
WINFUNCTYPE
14-
except NameError:
14+
WINFUNCTYPE = ctypes.WINFUNCTYPE
15+
except AttributeError:
1516
# fake to enable this test on Linux
1617
WINFUNCTYPE = CFUNCTYPE
1718

1819
import _ctypes_test
1920
dll = CDLL(_ctypes_test.__file__)
2021
if sys.platform == "win32":
21-
windll = WinDLL(_ctypes_test.__file__)
22+
windll = ctypes.WinDLL(_ctypes_test.__file__)
2223

2324
class POINT(Structure):
2425
_fields_ = [("x", c_int), ("y", c_int)]

Lib/test/test_ctypes/test_loading.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ctypes import *
2+
import ctypes
23
import os
34
import shutil
45
import subprocess
@@ -72,18 +73,18 @@ def test_load_library(self):
7273
print(find_library("user32"))
7374

7475
if os.name == "nt":
75-
windll.kernel32.GetModuleHandleW
76-
windll["kernel32"].GetModuleHandleW
77-
windll.LoadLibrary("kernel32").GetModuleHandleW
78-
WinDLL("kernel32").GetModuleHandleW
76+
ctypes.windll.kernel32.GetModuleHandleW
77+
ctypes.windll["kernel32"].GetModuleHandleW
78+
ctypes.windll.LoadLibrary("kernel32").GetModuleHandleW
79+
ctypes.WinDLL("kernel32").GetModuleHandleW
7980
# embedded null character
80-
self.assertRaises(ValueError, windll.LoadLibrary, "kernel32\0")
81+
self.assertRaises(ValueError, ctypes.windll.LoadLibrary, "kernel32\0")
8182

8283
@unittest.skipUnless(os.name == "nt",
8384
'test specific to Windows')
8485
def test_load_ordinal_functions(self):
8586
import _ctypes_test
86-
dll = WinDLL(_ctypes_test.__file__)
87+
dll = ctypes.WinDLL(_ctypes_test.__file__)
8788
# We load the same function both via ordinal and name
8889
func_ord = dll[2]
8990
func_name = dll.GetString
@@ -114,14 +115,16 @@ def test_1703286_B(self):
114115
# also has a high address. 'call_function' should accept
115116
# addresses so large.
116117
from _ctypes import call_function
117-
advapi32 = windll.advapi32
118+
119+
advapi32 = ctypes.windll.advapi32
118120
# Calling CloseEventLog with a NULL argument should fail,
119121
# but the call should not segfault or so.
120122
self.assertEqual(0, advapi32.CloseEventLog(None))
121-
windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
122-
windll.kernel32.GetProcAddress.restype = c_void_p
123-
proc = windll.kernel32.GetProcAddress(advapi32._handle,
124-
b"CloseEventLog")
123+
124+
kernel32 = ctypes.windll.kernel32
125+
kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
126+
kernel32.GetProcAddress.restype = c_void_p
127+
proc = kernel32.GetProcAddress(advapi32._handle, b"CloseEventLog")
125128
self.assertTrue(proc)
126129
# This is the real test: call the function via 'call_function'
127130
self.assertEqual(0, call_function(proc, (None,)))
@@ -130,7 +133,7 @@ def test_1703286_B(self):
130133
'test specific to Windows')
131134
def test_load_hasattr(self):
132135
# bpo-34816: shouldn't raise OSError
133-
self.assertFalse(hasattr(windll, 'test'))
136+
self.assertFalse(hasattr(ctypes.windll, 'test'))
134137

135138
@unittest.skipUnless(os.name == "nt",
136139
'test specific to Windows')

Lib/test/test_ctypes/test_pointers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest, sys
22

3+
import ctypes
34
from ctypes import *
45
import _ctypes_test
56

@@ -193,7 +194,7 @@ def test_pointers_bool(self):
193194

194195
# COM methods are boolean True:
195196
if sys.platform == "win32":
196-
mth = WINFUNCTYPE(None)(42, "name", (), None)
197+
mth = ctypes.WINFUNCTYPE(None)(42, "name", (), None)
197198
self.assertEqual(bool(mth), True)
198199

199200
def test_pointer_type_name(self):

Lib/test/test_ctypes/test_random_things.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ctypes import *
2+
import ctypes
23
import contextlib
34
from test import support
45
import unittest
@@ -16,15 +17,17 @@ class call_function_TestCase(unittest.TestCase):
1617

1718
def test(self):
1819
from _ctypes import call_function
19-
windll.kernel32.LoadLibraryA.restype = c_void_p
20-
windll.kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
21-
windll.kernel32.GetProcAddress.restype = c_void_p
2220

23-
hdll = windll.kernel32.LoadLibraryA(b"kernel32")
24-
funcaddr = windll.kernel32.GetProcAddress(hdll, b"GetModuleHandleA")
21+
kernel32 = ctypes.windll.kernel32
22+
kernel32.LoadLibraryA.restype = c_void_p
23+
kernel32.GetProcAddress.argtypes = c_void_p, c_char_p
24+
kernel32.GetProcAddress.restype = c_void_p
25+
26+
hdll = kernel32.LoadLibraryA(b"kernel32")
27+
funcaddr = kernel32.GetProcAddress(hdll, b"GetModuleHandleA")
2528

2629
self.assertEqual(call_function(funcaddr, (None,)),
27-
windll.kernel32.GetModuleHandleA(None))
30+
kernel32.GetModuleHandleA(None))
2831

2932
class CallbackTracbackTestCase(unittest.TestCase):
3033
# When an exception is raised in a ctypes callback function, the C

Lib/test/test_ctypes/test_win32.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Windows specific tests
22

3+
import ctypes
34
from ctypes import *
45
import unittest, sys
56
from test import support
@@ -14,15 +15,17 @@ class FunctionCallTestCase(unittest.TestCase):
1415
def test_SEH(self):
1516
# Disable faulthandler to prevent logging the warning:
1617
# "Windows fatal exception: access violation"
18+
kernel32 = ctypes.windll.kernel32
1719
with support.disable_faulthandler():
1820
# Call functions with invalid arguments, and make sure
1921
# that access violations are trapped and raise an
2022
# exception.
21-
self.assertRaises(OSError, windll.kernel32.GetModuleHandleA, 32)
23+
self.assertRaises(OSError, kernel32.GetModuleHandleA, 32)
2224

2325
def test_noargs(self):
2426
# This is a special case on win32 x64
25-
windll.user32.GetDesktopWindow()
27+
user32 = ctypes.windll.user32
28+
user32.GetDesktopWindow()
2629

2730

2831
@unittest.skipUnless(sys.platform == "win32", 'Windows-specific test')
@@ -73,17 +76,18 @@ def test_winerror(self):
7376
# see Issue 16169
7477
import errno
7578
ERROR_INVALID_PARAMETER = 87
76-
msg = FormatError(ERROR_INVALID_PARAMETER).strip()
79+
msg = ctypes.FormatError(ERROR_INVALID_PARAMETER).strip()
7780
args = (errno.EINVAL, msg, None, ERROR_INVALID_PARAMETER)
7881

79-
e = WinError(ERROR_INVALID_PARAMETER)
82+
e = ctypes.WinError(ERROR_INVALID_PARAMETER)
8083
self.assertEqual(e.args, args)
8184
self.assertEqual(e.errno, errno.EINVAL)
8285
self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)
8386

84-
windll.kernel32.SetLastError(ERROR_INVALID_PARAMETER)
87+
kernel32 = ctypes.windll.kernel32
88+
kernel32.SetLastError(ERROR_INVALID_PARAMETER)
8589
try:
86-
raise WinError()
90+
raise ctypes.WinError()
8791
except OSError as exc:
8892
e = exc
8993
self.assertEqual(e.args, args)

0 commit comments

Comments
 (0)