diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2021-09-13 22:18:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 22:18:37 (GMT) |
commit | a2d8c4b81b8e68e2ffe10945f7ca69174c14e52a (patch) | |
tree | c46c7aead37c0a393f13ef0fb8bb97ea9a9836c6 /Lib/ctypes/test | |
parent | 1fc41ae8709e20d741bd86c2345173688a5e84b0 (diff) | |
download | cpython-a2d8c4b81b8e68e2ffe10945f7ca69174c14e52a.zip cpython-a2d8c4b81b8e68e2ffe10945f7ca69174c14e52a.tar.gz cpython-a2d8c4b81b8e68e2ffe10945f7ca69174c14e52a.tar.bz2 |
bpo-45019: Do some cleanup related to frozen modules. (gh-28319)
There are a few things I missed in gh-27980. This is a follow-up that will make subsequent PRs cleaner. It includes fixes to tests and tools that reference the frozen modules.
https://bugs.python.org/issue45019
Diffstat (limited to 'Lib/ctypes/test')
-rw-r--r-- | Lib/ctypes/test/test_values.py | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/Lib/ctypes/test/test_values.py b/Lib/ctypes/test/test_values.py index 96a5f7cc..aa31d44 100644 --- a/Lib/ctypes/test/test_values.py +++ b/Lib/ctypes/test/test_values.py @@ -2,9 +2,12 @@ A testcase which accesses *values* in a dll. """ +import imp +import importlib.util import unittest import sys from ctypes import * +from test.support import import_helper, captured_stdout import _ctypes_test @@ -55,41 +58,32 @@ class PythonValuesTestCase(unittest.TestCase): ft = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules") # ft is a pointer to the struct_frozen entries: - items = [] - # _frozen_importlib changes size whenever importlib._bootstrap - # changes, so it gets a special case. We should make sure it's - # found, but don't worry about its size too much. The same - # applies to _frozen_importlib_external. - bootstrap_seen = [] - bootstrap_expected = [ - b'_frozen_importlib', - b'_frozen_importlib_external', - b'zipimport', - ] + modules = [] for entry in ft: # This is dangerous. We *can* iterate over a pointer, but # the loop will not terminate (maybe with an access # violation;-) because the pointer instance has no size. if entry.name is None: break - - if entry.name in bootstrap_expected: - bootstrap_seen.append(entry.name) - self.assertTrue(entry.size, - "{!r} was reported as having no size".format(entry.name)) - continue - items.append((entry.name.decode("ascii"), entry.size)) - - expected = [("__hello__", 164), - ("__phello__", -164), - ("__phello__.spam", 164), - ] - self.assertEqual(items, expected, "PyImport_FrozenModules example " + modname = entry.name.decode("ascii") + modules.append(modname) + with self.subTest(modname): + # Do a sanity check on entry.size and entry.code. + self.assertGreater(abs(entry.size), 10) + self.assertTrue([entry.code[i] for i in range(abs(entry.size))]) + # Check the module's package-ness. + spec = importlib.util.find_spec(modname) + if entry.size < 0: + # It's a package. + self.assertIsNotNone(spec.submodule_search_locations) + else: + self.assertIsNone(spec.submodule_search_locations) + + expected = imp._frozen_module_names() + self.maxDiff = None + self.assertEqual(modules, expected, "PyImport_FrozenModules example " "in Doc/library/ctypes.rst may be out of date") - self.assertEqual(sorted(bootstrap_seen), bootstrap_expected, - "frozen bootstrap modules did not match PyImport_FrozenModules") - from ctypes import _pointer_type_cache del _pointer_type_cache[struct_frozen] |