Repository: numpy/numpy
Pull request: BUG: Convert fortran flags in environment variable
This PR fixes #13074.

<!-- Please be sure you are following the instructions in the dev guidelines
http://www.numpy.org/devdocs/dev/gitwash/development_workflow.html
-->

<!-- We'd appreciate it if your commit message is properly formatted
http://www.numpy.org/devdocs/dev/gitwash/development_workflow.html#writing-the-commit-message
-->


Problem statement:
Failed to compile scipy with Anaconda gfortran.
<!-- Please describe the issue in detail here, and fill in the fields below -->

The compilation fails with an error and prints a command it is trying to execute. 

If I type the command at the end of the error log manually, it works.

I think this points to a quotation error. The additional fortran flags from the environment shall be added to the command line list after shell-style string split. It will produce this exact problem if the full list of fortran flags are added as a single string.

### Reproducing code example:

clone scipy from github;

activate Anaconda build enviroment.
python runtests.py

### Error message:

```
f951: Error: unrecognized command line option '-fopenmp -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe'
Running from scipy source directory.
/home/yfeng1/anaconda3/install/lib/python3.6/site-packages/numpy/distutils/system_info.py:730: UserWarning: Specified path /usr/local/include/python3.6m is invalid.
  return self.get_paths(self.section, key)
/home/yfeng1/anaconda3/install/lib/python3.6/site-packages/numpy/distutils/system_info.py:730: UserWarning: Specified path /usr/include/suitesparse/python3.6m is invalid.
  return self.get_paths(self.section, key)
error: Command "/home/yfeng1/anaconda3/install/bin/x86_64-conda_cos6-linux-gnu-gfortran -Wall -g -ffixed-form -fno-second-underscore -fPIC -fopenmp -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -O3 -funroll-loops -I/home/yfeng1/anaconda3/install/lib/python3.6/site-packages/numpy/core/include -c -c scipy/fftpack/src/dfftpack/dcosqb.f -o build/temp.linux-x86_64-3.6/scipy/fftpack/src/dfftpack/dcosqb.o" failed with exit status 1
```

### Numpy/Python version information:

1.16.2.

The environment variables regarding fortran are
```
FORTRANFLAGS=-fopenmp -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe
GFORTRAN=/home/yfeng1/anaconda3/install/bin/x86_64-conda_cos6-linux-gnu-gfortran
DEBUG_FORTRANFLAGS=-fopenmp -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-strong -fno-plt -O2 -ffunction-sections -pipe -fopenmp -march=nocona -mtune=haswell -ftree-vectorize -fPIC -fstack-protector-all -fno-plt -Og -g -Wall -Wextra -fcheck=all -fbacktrace -fimplicit-none -fvar-tracking-assignments -ffunction-sections -pipe
```

Hints from issue discussion:
I found a fix. Will file a PR.
Great - I couldn't even work out where we are using `FORTRANFLAGS`
Looks like `distutils.util.split_quoted` needs to be replaced with `distutils._shell_utils.NativeParser.split`

Commit message: Changes suggested by eric-wieser

Patch to review:
diff --git a/numpy/distutils/fcompiler/environment.py b/numpy/distutils/fcompiler/environment.py
index 4238f35cba6a..4d02bbdbba2d 100644
--- a/numpy/distutils/fcompiler/environment.py
+++ b/numpy/distutils/fcompiler/environment.py
@@ -51,13 +51,15 @@ def get(self, name, default=None):
 
     def _get_var(self, name, conf_desc):
         hook, envvar, confvar, convert, append = conf_desc
+        if convert is None: convert = lambda x: x
         var = self._hook_handler(name, hook)
         if envvar is not None:
             envvar_contents = os.environ.get(envvar)
             if envvar_contents is not None:
+                envvar_contents = convert(envvar_contents)
                 if var and append:
                     if os.environ.get('NPY_DISTUTILS_APPEND_FLAGS', '0') == '1':
-                        var = var + [envvar_contents]
+                        var.extend(envvar_contents)
                     else:
                         var = envvar_contents
                         if 'NPY_DISTUTILS_APPEND_FLAGS' not in os.environ.keys():
@@ -70,11 +72,12 @@ def _get_var(self, name, conf_desc):
                 else:
                     var = envvar_contents
         if confvar is not None and self._conf:
-            var = self._conf.get(confvar, (None, var))[1]
-        if convert is not None:
-            var = convert(var)
+            if confvar in self._conf:
+                source, confvar_contents = self._conf[convvar]
+                var = convert(confvar_contents)
         return var
 
+
     def clone(self, hook_handler):
         ec = self.__class__(distutils_section=self._distutils_section,
                             **self._conf_keys)


Code review comments:
diff_hunk: @@ -70,11 +72,12 @@ def _get_var(self, name, conf_desc):
                 else:
                     var = envvar_contents
         if confvar is not None and self._conf:
-            var = self._conf.get(confvar, (None, var))[1]
-        if convert is not None:
-            var = convert(var)
+            if confvar in self._conf:
+                source, confvar_contents = self._conf[convvar]
path: numpy/distutils/fcompiler/environment.py
text: @user1:
```suggestion
                source, confvar_contents = self._conf[confvar]
```

@author:
too many convolutions...