Skip to content

Commit 055056a

Browse files
authored
Merge pull request numpy#7791 from bertrand-l/feature/f2py-issues-7683
f2py.compile issues (numpy#7683)
2 parents 3a94389 + a05b653 commit 055056a

File tree

5 files changed

+85
-29
lines changed

5 files changed

+85
-29
lines changed

numpy/_import_tools.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _get_info_files(self, package_dir, parent_path, parent_package=None):
5252
def _init_info_modules(self, packages=None):
5353
"""Initialize info_modules = {<package_name>: <package info.py module>}.
5454
"""
55-
import imp
55+
from numpy.compat import npy_load_module
5656
info_files = []
5757
info_modules = self.info_modules
5858

@@ -86,8 +86,7 @@ def _init_info_modules(self, packages=None):
8686
filedescriptor = ('.py', 'U', 1)
8787

8888
try:
89-
info_module = imp.load_module(fullname+'.info',
90-
open(info_file, filedescriptor[1]),
89+
info_module = npy_load_module(fullname + '.info',
9190
info_file,
9291
filedescriptor)
9392
except Exception as msg:

numpy/compat/py3k.py

+59-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar',
88
'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested',
99
'asstr', 'open_latin1', 'long', 'basestring', 'sixu',
10-
'integer_types', 'is_pathlib_path', 'Path']
10+
'integer_types', 'is_pathlib_path', 'npy_load_module', 'Path']
1111

1212
import sys
1313
try:
@@ -91,9 +91,66 @@ def asunicode_nested(x):
9191
else:
9292
return asunicode(x)
9393

94-
9594
def is_pathlib_path(obj):
9695
"""
9796
Check whether obj is a pathlib.Path object.
9897
"""
9998
return Path is not None and isinstance(obj, Path)
99+
100+
if sys.version_info[0] >= 3 and sys.version_info[1] >= 4:
101+
def npy_load_module(name, fn, info=None):
102+
"""
103+
Load a module.
104+
105+
.. versionadded:: 1.11.2
106+
107+
Parameters
108+
----------
109+
name : str
110+
Full module name.
111+
fn : str
112+
Path to module file.
113+
info : tuple, optional
114+
Only here for backward compatibility with Python 2.*.
115+
116+
Returns
117+
-------
118+
mod : module
119+
120+
"""
121+
import importlib
122+
return importlib.machinery.SourceFileLoader(name, fn).load_module()
123+
else:
124+
def npy_load_module(name, fn, info=None):
125+
"""
126+
Load a module.
127+
128+
.. versionadded:: 1.11.2
129+
130+
Parameters
131+
----------
132+
name : str
133+
Full module name.
134+
fn : str
135+
Path to module file.
136+
info : tuple, optional
137+
Information as returned by `imp.find_module`
138+
(suffix, mode, type).
139+
140+
Returns
141+
-------
142+
mod : module
143+
144+
"""
145+
import imp
146+
import os
147+
if info is None:
148+
path = os.path.dirname(fn)
149+
fo, fn, info = imp.find_module(name, [path])
150+
else:
151+
fo = open(fn, info[1])
152+
try:
153+
mod = imp.load_module(name, fo, fn, info)
154+
finally:
155+
fo.close()
156+
return mod

numpy/distutils/misc_util.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import re
55
import sys
6-
import imp
76
import copy
87
import glob
98
import atexit
@@ -40,6 +39,7 @@ def clean_up_temporary_directory():
4039

4140
from numpy.distutils.compat import get_exception
4241
from numpy.compat import basestring
42+
from numpy.compat import npy_load_module
4343

4444
__all__ = ['Configuration', 'get_numpy_include_dirs', 'default_config_dict',
4545
'dict_append', 'appendpath', 'generate_config_py',
@@ -876,14 +876,11 @@ def _get_configuration_from_setup_py(self, setup_py,
876876
# In case setup_py imports local modules:
877877
sys.path.insert(0, os.path.dirname(setup_py))
878878
try:
879-
fo_setup_py = open(setup_py, 'U')
880879
setup_name = os.path.splitext(os.path.basename(setup_py))[0]
881880
n = dot_join(self.name, subpackage_name, setup_name)
882-
setup_module = imp.load_module('_'.join(n.split('.')),
883-
fo_setup_py,
881+
setup_module = npy_load_module('_'.join(n.split('.')),
884882
setup_py,
885883
('.py', 'U', 1))
886-
fo_setup_py.close()
887884
if not hasattr(setup_module, 'configuration'):
888885
if not self.options['assume_default_configuration']:
889886
self.warn('Assuming default configuration '\
@@ -1913,11 +1910,12 @@ def get_version(self, version_file=None, version_variable=None):
19131910
for f in files:
19141911
fn = njoin(self.local_path, f)
19151912
if os.path.isfile(fn):
1916-
info = (open(fn), fn, ('.py', 'U', 1))
1913+
info = ('.py', 'U', 1)
19171914
name = os.path.splitext(os.path.basename(fn))[0]
19181915
n = dot_join(self.name, name)
19191916
try:
1920-
version_module = imp.load_module('_'.join(n.split('.')),*info)
1917+
version_module = npy_load_module('_'.join(n.split('.')),
1918+
fn, info)
19211919
except ImportError:
19221920
msg = get_exception()
19231921
self.warn(str(msg))

numpy/f2py/__init__.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,32 @@ def compile(source,
2222
verbose=True,
2323
source_fn=None,
2424
extension='.f'
25-
):
26-
''' Build extension module from processing source with f2py.
25+
):
26+
"""
27+
Build extension module from processing source with f2py.
2728
2829
Parameters
2930
----------
3031
source : str
3132
Fortran source of module / subroutine to compile
3233
modulename : str, optional
33-
the name of compiled python module
34-
extra_args: str, optional
35-
additional parameters passed to f2py
36-
verbose: bool, optional
37-
print f2py output to screen
38-
extension: {'.f', '.f90'}, optional
39-
filename extension influences the fortran compiler behavior
34+
The name of the compiled python module
35+
extra_args : str, optional
36+
Additional parameters passed to f2py
37+
verbose : bool, optional
38+
Print f2py output to screen
39+
source_fn : str, optional
40+
Name of the file where the fortran source is written.
41+
The default is to use a temporary file with the extension
42+
provided by the `extension` parameter
43+
extension : {'.f', '.f90'}, optional
44+
Filename extension if `source_fn` is not provided.
45+
The extension tells which fortran standard is used.
46+
The default is `.f`, which implies F77 standard.
4047
4148
.. versionadded:: 1.11.0
4249
43-
'''
50+
"""
4451
from numpy.distutils.exec_command import exec_command
4552
import tempfile
4653
if source_fn is None:

numpy/testing/utils.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1088,18 +1088,13 @@ def rundocs(filename=None, raise_on_error=True):
10881088
10891089
>>> np.lib.test(doctests=True) #doctest: +SKIP
10901090
"""
1091+
from numpy.compat import npy_load_module
10911092
import doctest
1092-
import imp
10931093
if filename is None:
10941094
f = sys._getframe(1)
10951095
filename = f.f_globals['__file__']
10961096
name = os.path.splitext(os.path.basename(filename))[0]
1097-
path = [os.path.dirname(filename)]
1098-
file, pathname, description = imp.find_module(name, path)
1099-
try:
1100-
m = imp.load_module(name, file, pathname, description)
1101-
finally:
1102-
file.close()
1097+
m = npy_load_module(name, filename)
11031098

11041099
tests = doctest.DocTestFinder().find(m)
11051100
runner = doctest.DocTestRunner(verbose=False)

0 commit comments

Comments
 (0)