8000 ENH: provide a convenience function to replace npy_load_module (#20395) · numpy/numpy@46ec71f · GitHub
[go: up one dir, main page]

Skip to content

Commit 46ec71f

Browse files
authored
ENH: provide a convenience function to replace npy_load_module (#20395)
`load_module` is deprecated since python 3.4 and will be removed in python 3.12. Use `exec_module` instead. Provide a convenience function in `distutils.misc_utils` instead of `npy_load_module` from `compat.py3k`.
1 parent 3774a62 commit 46ec71f

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

numpy/compat/py3k.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ def __exit__(self, *excinfo):
107107

108108
def npy_load_module(name, fn, info=None):
109109
"""
110-
Load a module.
110+
Load a module. Uses ``load_module`` which will be deprecated in python
111+
3.12. An alternative that uses ``exec_module`` is in
112+
numpy.distutils.misc_util.exec_mod_from_location
111113
112114
.. versionadded:: 1.11.2
113115

numpy/distutils/ccompiler_opt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,9 +654,9 @@ def dist_log(*args, stderr=False):
654654
@staticmethod
655655
def dist_load_module(name, path):
656656
"""Load a module from file, required by the abstract class '_Cache'."""
657-
from numpy.compat import npy_load_module
657+
from .misc_util import exec_mod_from_location
658658
try:
659-
return npy_load_module(name, path)
659+
return exec_mod_from_location(name, path)
660660
except Exception as e:
661661
_Distutils.dist_log(e, stderr=True)
662662
return None

numpy/distutils/misc_util.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def clean_up_temporary_directory():
3131

3232
atexit.register(clean_up_temporary_directory)
3333

34-
from numpy.compat import npy_load_module
35-
3634
__all__ = ['Configuration', 'get_numpy_include_dirs', 'default_config_dict',
3735
'dict_append', 'appendpath', 'generate_config_py',
3836
'get_cmd', 'allpath', 'get_mathlibs',
@@ -44,7 +42,8 @@ def clean_up_temporary_directory():
4442
'dot_join', 'get_frame', 'minrelpath', 'njoin',
4543
'is_sequence', 'is_string', 'as_list', 'gpaths', 'get_language',
4644
'get_build_architecture', 'get_info', 'get_pkg_info',
47-
'get_num_build_jobs', 'sanitize_cxx_flags']
45+
'get_num_build_jobs', 'sanitize_cxx_flags',
46+
'exec_mod_from_location']
4847

4948
class InstallableLib:
5049
"""
@@ -945,9 +944,8 @@ def _get_configuration_from_setup_py(self, setup_py,
945944
try:
946945
setup_name = os.path.splitext(os.path.basename(setup_py))[0]
947946
n = dot_join(self.name, subpackage_name, setup_name)
948-
setup_module = npy_load_module('_'.join(n.split('.')),
949-
setup_py,
950-
('.py', 'U', 1))
947+
setup_module = exec_mod_from_location(
948+
'_'.join(n.split('.')), setup_py)
951949
if not hasattr(setup_module, 'configuration'):
952950
if not self.options['assume_default_configuration']:
953951
self.warn('Assuming default configuration '\
@@ -1993,8 +1991,8 @@ def get_version(self, version_file=None, version_variable=None):
19931991
name = os.path.splitext(os.path.basename(fn))[0]
19941992
n = dot_join(self.name, name)
19951993
try:
1996-
version_module = npy_load_module('_'.join(n.split('.')),
1997-
fn, info)
1994+
version_module = exec_mod_from_location(
1995+
'_'.join(n.split('.')), fn)
19981996
except ImportError as e:
19991997
self.warn(str(e))
20001998
version_module = None
@@ -2491,3 +2489,14 @@ def sanitize_cxx_flags(cxxflags):
24912489
return [flag for flag in cxxflags if flag not in _cxx_ignore_flags]
24922490

24932491

2492+
def exec_mod_from_location(modname, modfile):
2493+
'''
2494+
Use importlib machinery to import a module `modname` from the file
2495+
`modfile`. Depending on the `spec.loader`, the module may not be
2496+
registered in sys.modules.
2497+
'''
2498+
spec = importlib.util.spec_from_file_location(modname, modfile)
2499+
foo = importlib.util.module_from_spec(spec)
2500+
spec.loader.exec_module(foo)
2501+
return foo
2502+

numpy/random/tests/test_extending.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import warnings
77
import numpy as np
8+
from numpy.distutils.misc_util import exec_mod_from_location
89

910
try:
1011
import cffi
@@ -75,10 +76,9 @@ def test_cython(tmp_path):
7576
assert so1 is not None
7677
assert so2 is not None
7778
# import the so's without adding the directory to sys.path
78-
from importlib.machinery import ExtensionFileLoader
79-
extending = ExtensionFileLoader('extending', so1).load_module()
80-
extending_distributions = ExtensionFileLoader('extending_distributions', so2).load_module()
81-
79+
exec_mod_from_location('extending', so1)
80+
extending_distributions = exec_mod_from_location(
81+
'extending_distributions', so2)
8282
# actually test the cython c-extension
8383
from numpy.random import PCG64
8484
values = extending_distributions.uniforms_ex(PCG64(0), 10, 'd')

numpy/testing/_private/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,13 +1228,13 @@ def rundocs(filename=None, raise_on_error=True):
12281228
12291229
>>> np.lib.test(doctests=True) # doctest: +SKIP
12301230
"""
1231-
from numpy.compat import npy_load_module
1231+
from numpy.distutils.misc_util import exec_mod_from_location
12321232
import doctest
12331233
if filename is None:
12341234
f = sys._getframe(1)
12351235
filename = f.f_globals['__file__']
12361236
name = os.path.splitext(os.path.basename(filename))[0]
1237-
m = npy_load_module(name, filename)
1237+
m = exec_mod_from_location(name, filename)
12381238

12391239
tests = doctest.DocTestFinder().find(m)
12401240
runner = doctest.DocTestRunner(verbose=False)

0 commit comments

Comments
 (0)
0