Skip to content

Commit cb22479

Browse files
committed
test updates per review
1 parent e1dae92 commit cb22479

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

Lib/test/test_ctypes/test_dllist.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,58 @@
11
import os
22
import sys
3-
import test.support
43
import unittest
54
from ctypes import CDLL
6-
from ctypes.util import dllist
5+
import ctypes.util
76
from test.support import import_helper
87

98

10-
WINDOWS = os.name == 'nt'
9+
WINDOWS = os.name == "nt"
1110
APPLE = sys.platform in {"darwin", "ios", "tvos", "watchos"}
1211

1312
if WINDOWS:
14-
KNOWN_LIBRARIES = ['KERNEL32.DLL']
13+
KNOWN_LIBRARIES = ["KERNEL32.DLL"]
1514
elif APPLE:
16-
KNOWN_LIBRARIES = ['libSystem.B.dylib']
15+
KNOWN_LIBRARIES = ["libSystem.B.dylib"]
1716
else:
1817
# trickier than it seems, because libc may not be present
1918
# on musl systems, and sometimes goes by different names.
2019
# However, ctypes itself loads libffi
21-
KNOWN_LIBRARIES = ['libc.so', 'libffi.so']
20+
KNOWN_LIBRARIES = ["libc.so", "libffi.so"]
2221

22+
23+
@unittest.skipUnless(
24+
hasattr(ctypes.util, "dllist"),
25+
"ctypes.util.dllist is not available on this platform",
26+
)
2327
class ListSharedLibraries(unittest.TestCase):
2428

2529
def test_lists_system(self):
26-
dlls = dllist()
27-
28-
self.assertIsNotNone(dlls)
29-
self.assertGreater(len(dlls), 0, f'loaded={dlls}')
30-
self.assertTrue(any(lib in dll for dll in dlls for lib in KNOWN_LIBRARIES), f'loaded={dlls}')
30+
dlls = ctypes.util.dllist()
3131

32+
self.assertGreater(len(dlls), 0, f"loaded={dlls}")
33+
self.assertTrue(
34+
any(lib in dll for dll in dlls for lib in KNOWN_LIBRARIES), f"loaded={dlls}"
35+
)
3236

3337
def test_lists_updates(self):
34-
dlls = dllist()
35-
36-
if dlls is not None:
37-
if any("_ctypes_test" in dll for dll in dlls):
38-
self.skipTest("Test library is already loaded")
38+
dlls = ctypes.util.dllist()
3939

40-
_ctypes_test = import_helper.import_module("_ctypes_test")
41-
test_module = CDLL(_ctypes_test.__file__)
42-
dlls2 = dllist()
43-
self.assertIsNotNone(dlls2)
40+
# this test relies on being able to import a library which is
41+
# not already loaded.
42+
# If it is (e.g. by a previous test in the same process), we skip
43+
if any("_ctypes_test" in dll for dll in dlls):
44+
self.skipTest("Test library is already loaded")
4445

45-
dlls1 = set(dlls)
46-
dlls2 = set(dlls2)
47-
if test.support.verbose:
48-
print("Newly loaded shared libraries:")
49-
for dll in (dlls2 - dlls1):
50-
print("\t", dll)
46+
_ctypes_test = import_helper.import_module("_ctypes_test")
47+
test_module = CDLL(_ctypes_test.__file__)
48+
dlls2 = ctypes.util.dllist()
49+
self.assertIsNotNone(dlls2)
5150

52-
self.assertGreater(dlls2, dlls1)
53-
self.assertTrue(any("_ctypes_test" in dll for dll in dlls2))
51+
dlls1 = set(dlls)
52+
dlls2 = set(dlls2)
5453

54+
self.assertGreater(dlls2, dlls1, f"newly loaded libraries: {dlls2 - dlls1}")
55+
self.assertTrue(any("_ctypes_test" in dll for dll in dlls2), f"loaded={dlls2}")
5556

5657

5758
if __name__ == "__main__":

0 commit comments

Comments
 (0)