codekingpro/portable-devtools
115k
1import subprocess
2import sys
3import textwrap
4
5import pytest
6
7from numpy.testing import IS_WASM
8
9
10@pytest.mark.skipif(IS_WASM, reason="can't start subprocess")
11def test_lazy_load():
12 # gh-22045. lazyload doesn't import submodule names into the namespace
13
14 # Test within a new process, to ensure that we do not mess with the
15 # global state during the test run (could lead to cryptic test failures).
16 # This is generally unsafe, especially, since we also reload the C-modules.
17 code = textwrap.dedent(r"""
18 import sys
19 from importlib.util import LazyLoader, find_spec, module_from_spec
20
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 # noqa: F401
31
32 # test triggering the import of the package
33 np.ndarray
34 """)
35 p = subprocess.run(
36 (sys.executable, '-c', code),
37 stdout=subprocess.PIPE,
38 stderr=subprocess.STDOUT,
39 encoding='utf-8',
40 check=False,
41 )
42 assert p.returncode == 0, p.stdout
43 