summaryrefslogtreecommitdiff
path: root/numpy/f2py/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/f2py/__init__.py')
-rw-r--r--numpy/f2py/__init__.py71
1 files changed, 53 insertions, 18 deletions
diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py
index 949bac0ff..07ab6cd7d 100644
--- a/numpy/f2py/__init__.py
+++ b/numpy/f2py/__init__.py
@@ -9,7 +9,6 @@ import subprocess
import os
from . import f2py2e
-from . import f2py_testing
from . import diagnose
run_main = f2py2e.run_main
@@ -21,7 +20,8 @@ def compile(source,
extra_args='',
verbose=True,
source_fn=None,
- extension='.f'
+ extension='.f',
+ full_output=False
):
"""
Build extension module from a Fortran 77 source string with f2py.
@@ -55,10 +55,19 @@ def compile(source,
.. versionadded:: 1.11.0
+ full_output : bool, optional
+ If True, return a `subprocess.CompletedProcess` containing
+ the stdout and stderr of the compile process, instead of just
+ the status code.
+
+ .. versionadded:: 1.20.0
+
+
Returns
-------
- result : int
- 0 on success
+ result : int or `subprocess.CompletedProcess`
+ 0 on success, or a `subprocess.CompletedProcess` if
+ ``full_output=True``
Examples
--------
@@ -95,24 +104,50 @@ def compile(source,
'-c',
'import numpy.f2py as f2py2e;f2py2e.main()'] + args
try:
- output = subprocess.check_output(c)
- except subprocess.CalledProcessError as exc:
- status = exc.returncode
- output = ''
+ cp = subprocess.run(c, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
except OSError:
# preserve historic status code used by exec_command()
- status = 127
- output = ''
+ cp = subprocess.CompletedProcess(c, 127, stdout=b'', stderr=b'')
else:
- status = 0
- output = output.decode()
- if verbose:
- print(output)
+ if verbose:
+ print(cp.stdout.decode())
finally:
if source_fn is None:
os.remove(fname)
- return status
-from numpy._pytesttester import PytestTester
-test = PytestTester(__name__)
-del PytestTester
+ if full_output:
+ return cp
+ else:
+ return cp.returncode
+
+
+if sys.version_info[:2] >= (3, 7):
+ # module level getattr is only supported in 3.7 onwards
+ # https://www.python.org/dev/peps/pep-0562/
+ def __getattr__(attr):
+
+ # Avoid importing things that aren't needed for building
+ # which might import the main numpy module
+ if attr == "f2py_testing":
+ import numpy.f2py.f2py_testing as f2py_testing
+ return f2py_testing
+
+ elif attr == "test":
+ from numpy._pytesttester import PytestTester
+ test = PytestTester(__name__)
+ return test
+
+ else:
+ raise AttributeError("module {!r} has no attribute "
+ "{!r}".format(__name__, attr))
+
+ def __dir__():
+ return list(globals().keys() | {"f2py_testing", "test"})
+
+else:
+ from . import f2py_testing
+
+ from numpy._pytesttester import PytestTester
+ test = PytestTester(__name__)
+ del PytestTester