Skip to content

Commit a60b477

Browse files
rgommerscharris
authored andcommitted
MAINT: add warning to numpy.distutils for LDFLAGS append behavior.
See numpygh-7427
1 parent 8192711 commit a60b477

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎numpy/distutils/fcompiler/environment.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import division, absolute_import, print_function
22

33
import os
4+
import warnings
45
from distutils.dist import Distribution
56

67
__metaclass__ = type
@@ -54,8 +55,18 @@ def _get_var(self, name, conf_desc):
5455
if envvar is not None:
5556
envvar_contents = os.environ.get(envvar)
5657
if envvar_contents is not None:
57-
if var and append and os.environ.get('NPY_DISTUTILS_APPEND_FLAGS', '0') == '1':
58-
var = var + [envvar_contents]
58+
if var and append:
59+
if os.environ.get('NPY_DISTUTILS_APPEND_FLAGS', '0') == '1':
60+
var = var + [envvar_contents]
61+
else:
62+
var = envvar_contents
63+
if 'NPY_DISTUTILS_APPEND_FLAGS' not in os.environ.keys():
64+
msg = "{} is used as is, not appended ".format(envvar) + \
65+
"to flags already defined " + \
66+
"by numpy.distutils! Use NPY_DISTUTILS_APPEND_FLAGS=1 " + \
67+
"to obtain appending behavior instead (this " + \
68+
"behavior will become default in a future release)."
69+
warnings.warn(msg, UserWarning, stacklevel=3)
5970
else:
6071
var = envvar_contents
6172
if confvar is not None and self._conf:

‎numpy/distutils/tests/test_fcompiler.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import division, absolute_import, print_function
22

3-
from numpy.testing import assert_
3+
import pytest
4+
5+
from numpy.testing import assert_, suppress_warnings
46
import numpy.distutils.fcompiler
57

68
customizable_flags = [
@@ -25,6 +27,7 @@ def test_fcompiler_flags(monkeypatch):
2527

2628
monkeypatch.setenv(envvar, new_flag)
2729
new_flags = getattr(flag_vars, opt)
30+
2831
monkeypatch.delenv(envvar)
2932
assert_(new_flags == [new_flag])
3033

@@ -33,12 +36,41 @@ def test_fcompiler_flags(monkeypatch):
3336
for opt, envvar in customizable_flags:
3437
new_flag = '-dummy-{}-flag'.format(opt)
3538
prev_flags = getattr(flag_vars, opt)
36-
3739
monkeypatch.setenv(envvar, new_flag)
3840
new_flags = getattr(flag_vars, opt)
41+
3942
monkeypatch.delenv(envvar)
4043
if prev_flags is None:
4144
assert_(new_flags == [new_flag])
4245
else:
4346
assert_(new_flags == prev_flags + [new_flag])
4447

48+
49+
def test_fcompiler_flags_append_warning(monkeypatch):
50+
# Test to check that the warning for append behavior changing in future
51+
# is triggered. Need to use a real compiler instance so that we have
52+
# non-empty flags to start with (otherwise the "if var and append" check
53+
# will always be false).
54+
try:
55+
fc = numpy.distutils.fcompiler.new_fcompiler(compiler='gnu95')
56+
fc.customize()
57+
except numpy.distutils.fcompiler.CompilerNotFound:
58+
pytest.skip("gfortran not found, so can't execute this test")
59+
60+
# Ensure NPY_DISTUTILS_APPEND_FLAGS not defined
61+
monkeypatch.delenv('NPY_DISTUTILS_APPEND_FLAGS', raising=False)
62+
63+
for opt, envvar in customizable_flags:
64+
new_flag = '-dummy-{}-flag'.format(opt)
65+
prev_flags = getattr(fc.flag_vars, opt)
66+
monkeypatch.setenv(envvar, new_flag)
67+
with suppress_warnings() as sup:
68+
sup.record()
69+
new_flags = getattr(fc.flag_vars, opt)
70+
if prev_flags:
71+
# Check that warning was issued
72+
assert len(sup.log) == 1
73+
74+
monkeypatch.delenv(envvar)
75+
assert_(new_flags == [new_flag])
76+

0 commit comments

Comments
 (0)