| 1 |
import contextlib |
| 2 |
import importlib |
| 3 |
import importlib.abc |
| 4 |
import importlib.machinery |
| 5 |
import os |
| 6 |
import sys |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
|
| 10 |
from test.test_importlib import util |
| 11 |
|
| 12 |
# needed tests: |
| 13 |
# |
| 14 |
# need to test when nested, so that the top-level path isn't sys.path |
| 15 |
# need to test dynamic path detection, both at top-level and nested |
| 16 |
# with dynamic path, check when a loader is returned on path reload (that is, |
| 17 |
# trying to switch from a namespace package to a regular package) |
| 18 |
|
| 19 |
|
| 20 |
@contextlib.contextmanager |
| 21 |
def sys_modules_context(): |
| 22 |
""" |
| 23 |
Make sure sys.modules is the same object and has the same content |
| 24 |
when exiting the context as when entering. |
| 25 |
|
| 26 |
Similar to importlib.test.util.uncache, but doesn't require explicit |
| 27 |
names. |
| 28 |
""" |
| 29 |
sys_modules_saved = sys.modules |
| 30 |
sys_modules_copy = sys.modules.copy() |
| 31 |
try: |
| 32 |
yield |
| 33 |
finally: |
| 34 |
sys.modules = sys_modules_saved |
| 35 |
sys.modules.clear() |
| 36 |
sys.modules.update(sys_modules_copy) |
| 37 |
|
| 38 |
|
| 39 |
@contextlib.contextmanager |
| 40 |
def namespace_tree_context(**kwargs): |
| 41 |
""" |
| 42 |
Save import state and sys.modules cache and restore it on exit. |
| 43 |
Typical usage: |
| 44 |
|
| 45 |
>>> with namespace_tree_context(path=['/tmp/xxyy/portion1', |
| 46 |
... '/tmp/xxyy/portion2']): |
| 47 |
... pass |
| 48 |
""" |
| 49 |
# use default meta_path and path_hooks unless specified otherwise |
| 50 |
kwargs.setdefault('meta_path', sys.meta_path) |
| 51 |
kwargs.setdefault('path_hooks', sys.path_hooks) |
| 52 |
import_context = util.import_state(**kwargs) |
| 53 |
with import_context, sys_modules_context(): |
| 54 |
yield |
| 55 |
|
| 56 |
class NamespacePackageTest(unittest.TestCase): |
| 57 |
""" |
| 58 |
Subclasses should define self.root and self.paths (under that root) |
| 59 |
to be added to sys.path. |
| 60 |
""" |
| 61 |
root = os.path.join(os.path.dirname(__file__), 'namespace_pkgs') |
| 62 |
|
| 63 |
def setUp(self): |
| 64 |
self.resolved_paths = [ |
| 65 |
os.path.join(self.root, path) for path in self.paths |
| 66 |
] |
| 67 |
self.enterContext(namespace_tree_context(path=self.resolved_paths)) |
| 68 |
|
| 69 |
|
| 70 |
class SingleNamespacePackage(NamespacePackageTest): |
| 71 |
paths = ['portion1'] |
| 72 |
|
| 73 |
def test_simple_package(self): |
| 74 |
import foo.one |
| 75 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 76 |
|
| 77 |
def test_cant_import_other(self): |
| 78 |
with self.assertRaises(ImportError): |
| 79 |
import foo.two |
| 80 |
|
| 81 |
def test_simple_repr(self): |
| 82 |
import foo.one |
| 83 |
self.assertStartsWith(repr(foo), "<module 'foo' (namespace) from [") |
| 84 |
|
| 85 |
|
| 86 |
class DynamicPathNamespacePackage(NamespacePackageTest): |
| 87 |
paths = ['portion1'] |
| 88 |
|
| 89 |
def test_dynamic_path(self): |
| 90 |
# Make sure only 'foo.one' can be imported |
| 91 |
import foo.one |
| 92 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 93 |
|
| 94 |
with self.assertRaises(ImportError): |
| 95 |
import foo.two |
| 96 |
|
| 97 |
# Now modify sys.path |
| 98 |
sys.path.append(os.path.join(self.root, 'portion2')) |
| 99 |
|
| 100 |
# And make sure foo.two is now importable |
| 101 |
import foo.two |
| 102 |
self.assertEqual(foo.two.attr, 'portion2 foo two') |
| 103 |
|
| 104 |
|
| 105 |
class CombinedNamespacePackages(NamespacePackageTest): |
| 106 |
paths = ['both_portions'] |
| 107 |
|
| 108 |
def test_imports(self): |
| 109 |
import foo.one |
| 110 |
import foo.two |
| 111 |
self.assertEqual(foo.one.attr, 'both_portions foo one') |
| 112 |
self.assertEqual(foo.two.attr, 'both_portions foo two') |
| 113 |
|
| 114 |
|
| 115 |
class SeparatedNamespacePackages(NamespacePackageTest): |
| 116 |
paths = ['portion1', 'portion2'] |
| 117 |
|
| 118 |
def test_imports(self): |
| 119 |
import foo.one |
| 120 |
import foo.two |
| 121 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 122 |
self.assertEqual(foo.two.attr, 'portion2 foo two') |
| 123 |
|
| 124 |
|
| 125 |
class SeparatedNamespacePackagesCreatedWhileRunning(NamespacePackageTest): |
| 126 |
paths = ['portion1'] |
| 127 |
|
| 128 |
def test_invalidate_caches(self): |
| 129 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 130 |
# we manipulate sys.path before anything is imported to avoid |
| 131 |
# accidental cache invalidation when changing it |
| 132 |
sys.path.append(temp_dir) |
| 133 |
|
| 134 |
import foo.one |
| 135 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 136 |
|
| 137 |
# the module does not exist, so it cannot be imported |
| 138 |
with self.assertRaises(ImportError): |
| 139 |
import foo.just_created |
| 140 |
|
| 141 |
# util.create_modules() manipulates sys.path |
| 142 |
# so we must create the modules manually instead |
| 143 |
namespace_path = os.path.join(temp_dir, 'foo') |
| 144 |
os.mkdir(namespace_path) |
| 145 |
module_path = os.path.join(namespace_path, 'just_created.py') |
| 146 |
with open(module_path, 'w', encoding='utf-8') as file: |
| 147 |
file.write('attr = "just_created foo"') |
| 148 |
|
| 149 |
# the module is not known, so it cannot be imported yet |
| 150 |
with self.assertRaises(ImportError): |
| 151 |
import foo.just_created |
| 152 |
|
| 153 |
# but after explicit cache invalidation, it is importable |
| 154 |
importlib.invalidate_caches() |
| 155 |
import foo.just_created |
| 156 |
self.assertEqual(foo.just_created.attr, 'just_created foo') |
| 157 |
|
| 158 |
|
| 159 |
class SeparatedOverlappingNamespacePackages(NamespacePackageTest): |
| 160 |
paths = ['portion1', 'both_portions'] |
| 161 |
|
| 162 |
def test_first_path_wins(self): |
| 163 |
import foo.one |
| 164 |
import foo.two |
| 165 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 166 |
self.assertEqual(foo.two.attr, 'both_portions foo two') |
| 167 |
|
| 168 |
def test_first_path_wins_again(self): |
| 169 |
sys.path.reverse() |
| 170 |
import foo.one |
| 171 |
import foo.two |
| 172 |
self.assertEqual(foo.one.attr, 'both_portions foo one') |
| 173 |
self.assertEqual(foo.two.attr, 'both_portions foo two') |
| 174 |
|
| 175 |
def test_first_path_wins_importing_second_first(self): |
| 176 |
import foo.two |
| 177 |
import foo.one |
| 178 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 179 |
self.assertEqual(foo.two.attr, 'both_portions foo two') |
| 180 |
|
| 181 |
|
| 182 |
class SingleZipNamespacePackage(NamespacePackageTest): |
| 183 |
paths = ['top_level_portion1.zip'] |
| 184 |
|
| 185 |
def test_simple_package(self): |
| 186 |
import foo.one |
| 187 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 188 |
|
| 189 |
def test_cant_import_other(self): |
| 190 |
with self.assertRaises(ImportError): |
| 191 |
import foo.two |
| 192 |
|
| 193 |
|
| 194 |
class SeparatedZipNamespacePackages(NamespacePackageTest): |
| 195 |
paths = ['top_level_portion1.zip', 'portion2'] |
| 196 |
|
| 197 |
def test_imports(self): |
| 198 |
import foo.one |
| 199 |
import foo.two |
| 200 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 201 |
self.assertEqual(foo.two.attr, 'portion2 foo two') |
| 202 |
self.assertIn('top_level_portion1.zip', foo.one.__file__) |
| 203 |
self.assertNotIn('.zip', foo.two.__file__) |
| 204 |
|
| 205 |
|
| 206 |
class SingleNestedZipNamespacePackage(NamespacePackageTest): |
| 207 |
paths = ['nested_portion1.zip/nested_portion1'] |
| 208 |
|
| 209 |
def test_simple_package(self): |
| 210 |
import foo.one |
| 211 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 212 |
|
| 213 |
def test_cant_import_other(self): |
| 214 |
with self.assertRaises(ImportError): |
| 215 |
import foo.two |
| 216 |
|
| 217 |
|
| 218 |
class SeparatedNestedZipNamespacePackages(NamespacePackageTest): |
| 219 |
paths = ['nested_portion1.zip/nested_portion1', 'portion2'] |
| 220 |
|
| 221 |
def test_imports(self): |
| 222 |
import foo.one |
| 223 |
import foo.two |
| 224 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 225 |
self.assertEqual(foo.two.attr, 'portion2 foo two') |
| 226 |
fn = os.path.join('nested_portion1.zip', 'nested_portion1') |
| 227 |
self.assertIn(fn, foo.one.__file__) |
| 228 |
self.assertNotIn('.zip', foo.two.__file__) |
| 229 |
|
| 230 |
|
| 231 |
class LegacySupport(NamespacePackageTest): |
| 232 |
paths = ['not_a_namespace_pkg', 'portion1', 'portion2', 'both_portions'] |
| 233 |
|
| 234 |
def test_non_namespace_package_takes_precedence(self): |
| 235 |
import foo.one |
| 236 |
with self.assertRaises(ImportError): |
| 237 |
import foo.two |
| 238 |
self.assertIn('__init__', foo.__file__) |
| 239 |
self.assertNotIn('namespace', str(foo.__loader__).lower()) |
| 240 |
|
| 241 |
|
| 242 |
class DynamicPathCalculation(NamespacePackageTest): |
| 243 |
paths = ['project1', 'project2'] |
| 244 |
|
| 245 |
def test_project3_fails(self): |
| 246 |
import parent.child.one |
| 247 |
self.assertEqual(len(parent.__path__), 2) |
| 248 |
self.assertEqual(len(parent.child.__path__), 2) |
| 249 |
import parent.child.two |
| 250 |
self.assertEqual(len(parent.__path__), 2) |
| 251 |
self.assertEqual(len(parent.child.__path__), 2) |
| 252 |
|
| 253 |
self.assertEqual(parent.child.one.attr, 'parent child one') |
| 254 |
self.assertEqual(parent.child.two.attr, 'parent child two') |
| 255 |
|
| 256 |
with self.assertRaises(ImportError): |
| 257 |
import parent.child.three |
| 258 |
|
| 259 |
self.assertEqual(len(parent.__path__), 2) |
| 260 |
self.assertEqual(len(parent.child.__path__), 2) |
| 261 |
|
| 262 |
def test_project3_succeeds(self): |
| 263 |
import parent.child.one |
| 264 |
self.assertEqual(len(parent.__path__), 2) |
| 265 |
self.assertEqual(len(parent.child.__path__), 2) |
| 266 |
import parent.child.two |
| 267 |
self.assertEqual(len(parent.__path__), 2) |
| 268 |
self.assertEqual(len(parent.child.__path__), 2) |
| 269 |
|
| 270 |
self.assertEqual(parent.child.one.attr, 'parent child one') |
| 271 |
self.assertEqual(parent.child.two.attr, 'parent child two') |
| 272 |
|
| 273 |
with self.assertRaises(ImportError): |
| 274 |
import parent.child.three |
| 275 |
|
| 276 |
# now add project3 |
| 277 |
sys.path.append(os.path.join(self.root, 'project3')) |
| 278 |
import parent.child.three |
| 279 |
|
| 280 |
# the paths dynamically get longer, to include the new directories |
| 281 |
self.assertEqual(len(parent.__path__), 3) |
| 282 |
self.assertEqual(len(parent.child.__path__), 3) |
| 283 |
|
| 284 |
self.assertEqual(parent.child.three.attr, 'parent child three') |
| 285 |
|
| 286 |
|
| 287 |
class ZipWithMissingDirectory(NamespacePackageTest): |
| 288 |
paths = ['missing_directory.zip'] |
| 289 |
# missing_directory.zip contains: |
| 290 |
# Length Date Time Name |
| 291 |
# --------- ---------- ----- ---- |
| 292 |
# 29 2012-05-03 18:13 foo/one.py |
| 293 |
# 0 2012-05-03 20:57 bar/ |
| 294 |
# 38 2012-05-03 20:57 bar/two.py |
| 295 |
# --------- ------- |
| 296 |
# 67 3 files |
| 297 |
|
| 298 |
def test_missing_directory(self): |
| 299 |
import foo.one |
| 300 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 301 |
|
| 302 |
def test_missing_directory2(self): |
| 303 |
import foo |
| 304 |
self.assertNotHasAttr(foo, 'one') |
| 305 |
|
| 306 |
def test_present_directory(self): |
| 307 |
import bar.two |
| 308 |
self.assertEqual(bar.two.attr, 'missing_directory foo two') |
| 309 |
|
| 310 |
|
| 311 |
class ModuleAndNamespacePackageInSameDir(NamespacePackageTest): |
| 312 |
paths = ['module_and_namespace_package'] |
| 313 |
|
| 314 |
def test_module_before_namespace_package(self): |
| 315 |
# Make sure we find the module in preference to the |
| 316 |
# namespace package. |
| 317 |
import a_test |
| 318 |
self.assertEqual(a_test.attr, 'in module') |
| 319 |
|
| 320 |
|
| 321 |
class ReloadTests(NamespacePackageTest): |
| 322 |
paths = ['portion1'] |
| 323 |
|
| 324 |
def test_simple_package(self): |
| 325 |
import foo.one |
| 326 |
foo = importlib.reload(foo) |
| 327 |
self.assertEqual(foo.one.attr, 'portion1 foo one') |
| 328 |
|
| 329 |
def test_cant_import_other(self): |
| 330 |
import foo |
| 331 |
with self.assertRaises(ImportError): |
| 332 |
import foo.two |
| 333 |
foo = importlib.reload(foo) |
| 334 |
with self.assertRaises(ImportError): |
| 335 |
import foo.two |
| 336 |
|
| 337 |
def test_dynamic_path(self): |
| 338 |
import foo.one |
| 339 |
with self.assertRaises(ImportError): |
| 340 |
import foo.two |
| 341 |
|
| 342 |
# Now modify sys.path and reload. |
| 343 |
sys.path.append(os.path.join(self.root, 'portion2')) |
| 344 |
foo = importlib.reload(foo) |
| 345 |
|
| 346 |
# And make sure foo.two is now importable |
| 347 |
import foo.two |
| 348 |
self.assertEqual(foo.two.attr, 'portion2 foo two') |
| 349 |
|
| 350 |
|
| 351 |
class LoaderTests(NamespacePackageTest): |
| 352 |
paths = ['portion1'] |
| 353 |
|
| 354 |
def test_namespace_loader_consistency(self): |
| 355 |
# bpo-32303 |
| 356 |
import foo |
| 357 |
self.assertEqual(foo.__loader__, foo.__spec__.loader) |
| 358 |
self.assertIsNotNone(foo.__loader__) |
| 359 |
|
| 360 |
def test_namespace_origin_consistency(self): |
| 361 |
# bpo-32305 |
| 362 |
import foo |
| 363 |
self.assertIsNone(foo.__spec__.origin) |
| 364 |
self.assertIsNone(foo.__file__) |
| 365 |
|
| 366 |
def test_path_indexable(self): |
| 367 |
# bpo-35843 |
| 368 |
import foo |
| 369 |
expected_path = os.path.join(self.root, 'portion1', 'foo') |
| 370 |
self.assertEqual(foo.__path__[0], expected_path) |
| 371 |
|
| 372 |
def test_loader_abc(self): |
| 373 |
import foo |
| 374 |
self.assertTrue(isinstance(foo.__loader__, importlib.abc.Loader)) |
| 375 |
self.assertTrue(isinstance(foo.__loader__, importlib.machinery.NamespaceLoader)) |
| 376 |
|
| 377 |
|
| 378 |
if __name__ == "__main__": |
| 379 |
unittest.main() |