| 1 |
from test.test_importlib import abc, util |
| 2 |
|
| 3 |
machinery = util.import_importlib('importlib.machinery') |
| 4 |
|
| 5 |
import os.path |
| 6 |
import unittest |
| 7 |
|
| 8 |
from test.support import import_helper, REPO_ROOT, STDLIB_DIR |
| 9 |
|
| 10 |
|
| 11 |
def resolve_stdlib_file(name, ispkg=False): |
| 12 |
assert name |
| 13 |
if ispkg: |
| 14 |
return os.path.join(STDLIB_DIR, *name.split('.'), '__init__.py') |
| 15 |
else: |
| 16 |
return os.path.join(STDLIB_DIR, *name.split('.')) + '.py' |
| 17 |
|
| 18 |
|
| 19 |
class FindSpecTests(abc.FinderTests): |
| 20 |
|
| 21 |
"""Test finding frozen modules.""" |
| 22 |
|
| 23 |
def find(self, name, **kwargs): |
| 24 |
finder = self.machinery.FrozenImporter |
| 25 |
with import_helper.frozen_modules(): |
| 26 |
return finder.find_spec(name, **kwargs) |
| 27 |
|
| 28 |
def check_basic(self, spec, name, ispkg=False): |
| 29 |
self.assertEqual(spec.name, name) |
| 30 |
self.assertIs(spec.loader, self.machinery.FrozenImporter) |
| 31 |
self.assertEqual(spec.origin, 'frozen') |
| 32 |
self.assertFalse(spec.has_location) |
| 33 |
if ispkg: |
| 34 |
self.assertIsNotNone(spec.submodule_search_locations) |
| 35 |
else: |
| 36 |
self.assertIsNone(spec.submodule_search_locations) |
| 37 |
self.assertIsNotNone(spec.loader_state) |
| 38 |
|
| 39 |
def check_loader_state(self, spec, origname=None, filename=None): |
| 40 |
if not filename: |
| 41 |
if not origname: |
| 42 |
origname = spec.name |
| 43 |
filename = resolve_stdlib_file(origname) |
| 44 |
|
| 45 |
actual = dict(vars(spec.loader_state)) |
| 46 |
|
| 47 |
# Check the rest of spec.loader_state. |
| 48 |
expected = dict( |
| 49 |
origname=origname, |
| 50 |
filename=filename if origname else None, |
| 51 |
) |
| 52 |
self.assertDictEqual(actual, expected) |
| 53 |
|
| 54 |
def check_search_locations(self, spec): |
| 55 |
"""This is only called when testing packages.""" |
| 56 |
missing = object() |
| 57 |
filename = getattr(spec.loader_state, 'filename', missing) |
| 58 |
origname = getattr(spec.loader_state, 'origname', None) |
| 59 |
if not origname or filename is missing: |
| 60 |
# We deal with this in check_loader_state(). |
| 61 |
return |
| 62 |
if not filename: |
| 63 |
expected = [] |
| 64 |
elif origname != spec.name and not origname.startswith('<'): |
| 65 |
expected = [] |
| 66 |
else: |
| 67 |
expected = [os.path.dirname(filename)] |
| 68 |
self.assertListEqual(spec.submodule_search_locations, expected) |
| 69 |
|
| 70 |
def test_module(self): |
| 71 |
modules = [ |
| 72 |
'__hello__', |
| 73 |
'__phello__.spam', |
| 74 |
'__phello__.ham.eggs', |
| 75 |
] |
| 76 |
for name in modules: |
| 77 |
with self.subTest(f'{name} -> {name}'): |
| 78 |
spec = self.find(name) |
| 79 |
self.check_basic(spec, name) |
| 80 |
self.check_loader_state(spec) |
| 81 |
modules = { |
| 82 |
'__hello_alias__': '__hello__', |
| 83 |
'_frozen_importlib': 'importlib._bootstrap', |
| 84 |
} |
| 85 |
for name, origname in modules.items(): |
| 86 |
with self.subTest(f'{name} -> {origname}'): |
| 87 |
spec = self.find(name) |
| 88 |
self.check_basic(spec, name) |
| 89 |
self.check_loader_state(spec, origname) |
| 90 |
modules = [ |
| 91 |
'__phello__.__init__', |
| 92 |
'__phello__.ham.__init__', |
| 93 |
] |
| 94 |
for name in modules: |
| 95 |
origname = '<' + name.rpartition('.')[0] |
| 96 |
filename = resolve_stdlib_file(name) |
| 97 |
with self.subTest(f'{name} -> {origname}'): |
| 98 |
spec = self.find(name) |
| 99 |
self.check_basic(spec, name) |
| 100 |
self.check_loader_state(spec, origname, filename) |
| 101 |
modules = { |
| 102 |
'__hello_only__': ('Tools', 'freeze', 'flag.py'), |
| 103 |
} |
| 104 |
for name, path in modules.items(): |
| 105 |
origname = None |
| 106 |
filename = os.path.join(REPO_ROOT, *path) |
| 107 |
with self.subTest(f'{name} -> {filename}'): |
| 108 |
spec = self.find(name) |
| 109 |
self.check_basic(spec, name) |
| 110 |
self.check_loader_state(spec, origname, filename) |
| 111 |
|
| 112 |
def test_package(self): |
| 113 |
packages = [ |
| 114 |
'__phello__', |
| 115 |
'__phello__.ham', |
| 116 |
] |
| 117 |
for name in packages: |
| 118 |
filename = resolve_stdlib_file(name, ispkg=True) |
| 119 |
with self.subTest(f'{name} -> {name}'): |
| 120 |
spec = self.find(name) |
| 121 |
self.check_basic(spec, name, ispkg=True) |
| 122 |
self.check_loader_state(spec, name, filename) |
| 123 |
self.check_search_locations(spec) |
| 124 |
packages = { |
| 125 |
'__phello_alias__': '__hello__', |
| 126 |
} |
| 127 |
for name, origname in packages.items(): |
| 128 |
filename = resolve_stdlib_file(origname, ispkg=False) |
| 129 |
with self.subTest(f'{name} -> {origname}'): |
| 130 |
spec = self.find(name) |
| 131 |
self.check_basic(spec, name, ispkg=True) |
| 132 |
self.check_loader_state(spec, origname, filename) |
| 133 |
self.check_search_locations(spec) |
| 134 |
|
| 135 |
# These are covered by test_module() and test_package(). |
| 136 |
test_module_in_package = None |
| 137 |
test_package_in_package = None |
| 138 |
|
| 139 |
# No easy way to test. |
| 140 |
test_package_over_module = None |
| 141 |
|
| 142 |
def test_path_ignored(self): |
| 143 |
for name in ('__hello__', '__phello__', '__phello__.spam'): |
| 144 |
actual = self.find(name) |
| 145 |
for path in (None, object(), '', 'eggs', [], [''], ['eggs']): |
| 146 |
with self.subTest((name, path)): |
| 147 |
spec = self.find(name, path=path) |
| 148 |
self.assertEqual(spec, actual) |
| 149 |
|
| 150 |
def test_target_ignored(self): |
| 151 |
imported = ('__hello__', '__phello__') |
| 152 |
with import_helper.CleanImport(*imported, usefrozen=True): |
| 153 |
import __hello__ as match |
| 154 |
import __phello__ as nonmatch |
| 155 |
name = '__hello__' |
| 156 |
actual = self.find(name) |
| 157 |
for target in (None, match, nonmatch, object(), 'not-a-module-object'): |
| 158 |
with self.subTest(target): |
| 159 |
spec = self.find(name, target=target) |
| 160 |
self.assertEqual(spec, actual) |
| 161 |
|
| 162 |
def test_failure(self): |
| 163 |
spec = self.find('<not real>') |
| 164 |
self.assertIsNone(spec) |
| 165 |
|
| 166 |
def test_not_using_frozen(self): |
| 167 |
finder = self.machinery.FrozenImporter |
| 168 |
with import_helper.frozen_modules(enabled=False): |
| 169 |
# both frozen and not frozen |
| 170 |
spec1 = finder.find_spec('__hello__') |
| 171 |
# only frozen |
| 172 |
spec2 = finder.find_spec('__hello_only__') |
| 173 |
self.assertIsNone(spec1) |
| 174 |
self.assertIsNone(spec2) |
| 175 |
|
| 176 |
|
| 177 |
(Frozen_FindSpecTests, |
| 178 |
Source_FindSpecTests |
| 179 |
) = util.test_both(FindSpecTests, machinery=machinery) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == '__main__': |
| 183 |
unittest.main() |