|
2 | 2 | A testcase which accesses *values* in a dll. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import imp |
| 6 | +import importlib.util |
5 | 7 | import unittest |
6 | 8 | import sys |
7 | 9 | from ctypes import * |
| 10 | +from test.support import import_helper, captured_stdout |
8 | 11 |
|
9 | 12 | import _ctypes_test |
10 | 13 |
|
@@ -55,41 +58,32 @@ class struct_frozen(Structure): |
55 | 58 |
|
56 | 59 | ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules") |
57 | 60 | # ft is a pointer to the struct_frozen entries: |
58 | | - items = [] |
59 | | - # _frozen_importlib changes size whenever importlib._bootstrap |
60 | | - # changes, so it gets a special case. We should make sure it's |
61 | | - # found, but don't worry about its size too much. The same |
62 | | - # applies to _frozen_importlib_external. |
63 | | - bootstrap_seen = [] |
64 | | - bootstrap_expected = [ |
65 | | - b'_frozen_importlib', |
66 | | - b'_frozen_importlib_external', |
67 | | - b'zipimport', |
68 | | - ] |
| 61 | + modules = [] |
69 | 62 | for entry in ft: |
70 | 63 | # This is dangerous. We *can* iterate over a pointer, but |
71 | 64 | # the loop will not terminate (maybe with an access |
72 | 65 | # violation;-) because the pointer instance has no size. |
73 | 66 | if entry.name is None: |
74 | 67 | break |
75 | | - |
76 | | - if entry.name in bootstrap_expected: |
77 | | - bootstrap_seen.append(entry.name) |
78 | | - self.assertTrue(entry.size, |
79 | | - "{!r} was reported as having no size".format(entry.name)) |
80 | | - continue |
81 | | - items.append((entry.name.decode("ascii"), entry.size)) |
82 | | - |
83 | | - expected = [("__hello__", 164), |
84 | | - ("__phello__", -164), |
85 | | - ("__phello__.spam", 164), |
86 | | - ] |
87 | | - self.assertEqual(items, expected, "PyImport_FrozenModules example " |
| 68 | + modname = entry.name.decode("ascii") |
| 69 | + modules.append(modname) |
| 70 | + with self.subTest(modname): |
| 71 | + # Do a sanity check on entry.size and entry.code. |
| 72 | + self.assertGreater(abs(entry.size), 10) |
| 73 | + self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) |
| 74 | + # Check the module's package-ness. |
| 75 | + spec = importlib.util.find_spec(modname) |
| 76 | + if entry.size < 0: |
| 77 | + # It's a package. |
| 78 | + self.assertIsNotNone(spec.submodule_search_locations) |
| 79 | + else: |
| 80 | + self.assertIsNone(spec.submodule_search_locations) |
| 81 | + |
| 82 | + expected = imp._frozen_module_names() |
| 83 | + self.maxDiff = None |
| 84 | + self.assertEqual(modules, expected, "PyImport_FrozenModules example " |
88 | 85 | "in Doc/library/ctypes.rst may be out of date") |
89 | 86 |
|
90 | | - self.assertEqual(sorted(bootstrap_seen), bootstrap_expected, |
91 | | - "frozen bootstrap modules did not match PyImport_FrozenModules") |
92 | | - |
93 | 87 | from ctypes import _pointer_type_cache |
94 | 88 | del _pointer_type_cache[struct_frozen] |
95 | 89 |
|
|
0 commit comments