| 7 | # Warning raised by _reload_guard() in numpy/__init__.py |
| 8 | @pytest.mark.filterwarnings("ignore:The NumPy module was reloaded") |
| 9 | def test_lazy_load(): |
| 10 | # gh-22045. lazyload doesn't import submodule names into the namespace |
| 11 | # muck with sys.modules to test the importing system |
| 12 | old_numpy = sys.modules.pop("numpy") |
| 13 | |
| 14 | numpy_modules = {} |
| 15 | for mod_name, mod in list(sys.modules.items()): |
| 16 | if mod_name[:6] == "numpy.": |
| 17 | numpy_modules[mod_name] = mod |
| 18 | sys.modules.pop(mod_name) |
| 19 | |
| 20 | try: |
| 21 | # create lazy load of numpy as np |
| 22 | spec = find_spec("numpy") |
| 23 | module = module_from_spec(spec) |
| 24 | sys.modules["numpy"] = module |
| 25 | loader = LazyLoader(spec.loader) |
| 26 | loader.exec_module(module) |
| 27 | np = module |
| 28 | |
| 29 | # test a subpackage import |
| 30 | from numpy.lib import recfunctions |
| 31 | |
| 32 | # test triggering the import of the package |
| 33 | np.ndarray |
| 34 | |
| 35 | finally: |
| 36 | if old_numpy: |
| 37 | sys.modules["numpy"] = old_numpy |
| 38 | sys.modules.update(numpy_modules) |