Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
test_reloading.py77 linesDownload Raw Back to tests
1import pickle
2import subprocess
3import sys
4import textwrap
5from importlib import reload
6
7import pytest
8
9import numpy.exceptions as ex
10from numpy.testing import IS_WASM, assert_, assert_equal, assert_raises
11
12
13@pytest.mark.thread_unsafe(reason="reloads global module")
14def test_numpy_reloading():
15    # gh-7844. Also check that relevant globals retain their identity.
16    import numpy as np
17    import numpy._globals
18
19    _NoValue = np._NoValue
20    VisibleDeprecationWarning = ex.VisibleDeprecationWarning
21    ModuleDeprecationWarning = ex.ModuleDeprecationWarning
22
23    with pytest.warns(UserWarning):
24        reload(np)
25    assert_(_NoValue is np._NoValue)
26    assert_(ModuleDeprecationWarning is ex.ModuleDeprecationWarning)
27    assert_(VisibleDeprecationWarning is ex.VisibleDeprecationWarning)
28
29    assert_raises(RuntimeError, reload, numpy._globals)
30    with pytest.warns(UserWarning):
31        reload(np)
32    assert_(_NoValue is np._NoValue)
33    assert_(ModuleDeprecationWarning is ex.ModuleDeprecationWarning)
34    assert_(VisibleDeprecationWarning is ex.VisibleDeprecationWarning)
35
36def test_novalue():
37    import numpy as np
38    for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
39        assert_equal(repr(np._NoValue), '<no value>')
40        assert_(pickle.loads(pickle.dumps(np._NoValue,
41                                          protocol=proto)) is np._NoValue)
42
43
44@pytest.mark.skipif(IS_WASM, reason="can't start subprocess")
45def test_full_reimport():
46    # Reimporting numpy like this is not safe due to use of global C state,
47    # and has unexpected side effects. Test that an ImportError is raised.
48    # When all extension modules are isolated, this should test that clearing
49    # sys.modules and reimporting numpy works without error.
50
51    # Test within a new process, to ensure that we do not mess with the
52    # global state during the test run (could lead to cryptic test failures).
53    # This is generally unsafe, especially, since we also reload the C-modules.
54    code = textwrap.dedent(r"""
55        import sys
56        import numpy as np
57
58        for k in [k for k in sys.modules if k.startswith('numpy')]:
59            del sys.modules[k]
60
61        try:
62            import numpy as np
63        except ImportError as err:
64            if str(err) != "cannot load module more than once per process":
65                raise SystemExit(f"Unexpected ImportError: {err}")
66        else:
67            raise SystemExit("DID NOT RAISE ImportError")
68        """)
69    p = subprocess.run(
70        (sys.executable, '-c', code),
71        stdout=subprocess.PIPE,
72        stderr=subprocess.STDOUT,
73        encoding='utf-8',
74        check=False,
75    )
76    assert p.returncode == 0, p.stdout
77 
codekingpro/portable-devtools · Team Ai