|
1 | 1 | import os |
2 | 2 | import sys |
3 | | -import test.support |
4 | 3 | import unittest |
5 | 4 | from ctypes import CDLL |
6 | | -from ctypes.util import dllist |
| 5 | +import ctypes.util |
7 | 6 | from test.support import import_helper |
8 | 7 |
|
9 | 8 |
|
10 | | -WINDOWS = os.name == 'nt' |
| 9 | +WINDOWS = os.name == "nt" |
11 | 10 | APPLE = sys.platform in {"darwin", "ios", "tvos", "watchos"} |
12 | 11 |
|
13 | 12 | if WINDOWS: |
14 | | - KNOWN_LIBRARIES = ['KERNEL32.DLL'] |
| 13 | + KNOWN_LIBRARIES = ["KERNEL32.DLL"] |
15 | 14 | elif APPLE: |
16 | | - KNOWN_LIBRARIES = ['libSystem.B.dylib'] |
| 15 | + KNOWN_LIBRARIES = ["libSystem.B.dylib"] |
17 | 16 | else: |
18 | 17 | # trickier than it seems, because libc may not be present |
19 | 18 | # on musl systems, and sometimes goes by different names. |
20 | 19 | # However, ctypes itself loads libffi |
21 | | - KNOWN_LIBRARIES = ['libc.so', 'libffi.so'] |
| 20 | + KNOWN_LIBRARIES = ["libc.so", "libffi.so"] |
22 | 21 |
|
| 22 | + |
| 23 | +@unittest.skipUnless( |
| 24 | + hasattr(ctypes.util, "dllist"), |
| 25 | + "ctypes.util.dllist is not available on this platform", |
| 26 | +) |
23 | 27 | class ListSharedLibraries(unittest.TestCase): |
24 | 28 |
|
25 | 29 | 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() |
31 | 31 |
|
| 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 | + ) |
32 | 36 |
|
33 | 37 | 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() |
39 | 39 |
|
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") |
44 | 45 |
|
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) |
51 | 50 |
|
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) |
54 | 53 |
|
| 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}") |
55 | 56 |
|
56 | 57 |
|
57 | 58 | if __name__ == "__main__": |
|
0 commit comments