-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathtest_lazyloading.py
More file actions
42 lines (34 loc) · 1.23 KB
/
test_lazyloading.py
File metadata and controls
42 lines (34 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
import sys
import textwrap
import pytest
from numpy.testing import IS_WASM
@pytest.mark.skipif(IS_WASM, reason="can't start subprocess")
def test_lazy_load():
# gh-22045. lazyload doesn't import submodule names into the namespace
# Test within a new process, to ensure that we do not mess with the
# global state during the test run (could lead to cryptic test failures).
# This is generally unsafe, especially, since we also reload the C-modules.
code = textwrap.dedent(r"""
import sys
from importlib.util import LazyLoader, find_spec, module_from_spec
# create lazy load of numpy as np
spec = find_spec("numpy")
module = module_from_spec(spec)
sys.modules["numpy"] = module
loader = LazyLoader(spec.loader)
loader.exec_module(module)
np = module
# test a subpackage import
from numpy.lib import recfunctions # noqa: F401
# test triggering the import of the package
np.ndarray
""")
p = subprocess.run(
(sys.executable, '-c', code),
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding='utf-8',
check=False,
)
assert p.returncode == 0, p.stdout