Repository: nipy/nipype

Issue:
ValueError when running NiftyReg node
### Summary
I get this error when running a NiftyReg registration or resampling: `ValueError: close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr`.

### Actual behavior
```python-traceback
C:\Users\fernando\tmp
(vesseg) λ python r.py
180612-16:47:31,795 interface WARNING:
         version_from_command member of CommandLine was Deprecated in nipype-1.0.0 and deleted in 1.1.0
180612-16:47:31,866 interface WARNING:
         version_from_command member of CommandLine was Deprecated in nipype-1.0.0 and deleted in 1.1.0
Traceback (most recent call last):
  File "r.py", line 11, in <module>
    aladin.run()
  File "C:\Users\fernando\Miniconda3\envs\vesseg\lib\site-packages\nipype\interfaces\base\core.py", line 521, in run
    runtime = self._run_interface(runtime)
  File "C:\Users\fernando\Miniconda3\envs\vesseg\lib\site-packages\nipype\interfaces\base\core.py", line 1041, in _run_interface
    runtime = run_command(runtime, output=self.terminal_output)
  File "C:\Users\fernando\Miniconda3\envs\vesseg\lib\site-packages\nipype\interfaces\base\core.py", line 778, in run_command
    close_fds=True,
  File "C:\Users\fernando\Miniconda3\envs\vesseg\lib\subprocess.py", line 624, in __init__
    "close_fds is not supported on Windows platforms"
ValueError: close_fds is not supported on Windows platforms if you redirect stdin/stdout/stderr
```

### How to replicate the behavior
Run:
```python
import pathlib
from nipype.interfaces import niftyreg

data_dir = pathlib.Path('~/tmp/oeslle').expanduser()
ref_path = flo_path = str(data_dir / 't1.nii.gz')

aladin = niftyreg.RegAladin()
aladin.inputs.ref_file = ref_path
aladin.inputs.flo_file = flo_path
aladin.inputs.lp_val = 1
aladin.run()
```

This 
```python
subprocess.call(aladin.cmdline.split())
```
works on that little script, but in a larger script Windows shows an error and the script stops working. I haven't been able to reproduce that, though.

### Platform details:
```python
In [1]: import nipype

In [2]: nipype.get_info()
Out[2]:
{'pkg_path': 'C:\\Users\\fernando\\Miniconda3\\envs\\vesseg\\lib\\site-packages\\nipype',
 'commit_source': 'archive substitution',
 'commit_hash': '%h',
 'nipype_version': '1.0.4',
 'sys_version': '3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]',
 'sys_executable': 'C:\\Users\\fernando\\Miniconda3\\envs\\vesseg\\python.exe',
 'sys_platform': 'win32',
 'numpy_version': '1.14.3',
 'scipy_version': '1.1.0',
 'networkx_version': '2.1',
 'nibabel_version': '2.2.1',
 'traits_version': '4.6.0'}

In [3]: nipype.__version__
Out[3]: '1.0.4'
```

### Execution environment
I'm not sure what this means. I'm running Python from a `conda` environment.




Discussion:
Pinging @byvernault here. I would guess that means you'll need to set a different `terminal_output` when running on Windows.
I'm not sure what you mean. I haven't set that variable.

Resolving patch:
diff --git a/nipype/interfaces/base/core.py b/nipype/interfaces/base/core.py
--- a/nipype/interfaces/base/core.py
+++ b/nipype/interfaces/base/core.py
@@ -775,7 +775,7 @@ def run_command(runtime, output=None, timeout=0.01):
         shell=True,
         cwd=runtime.cwd,
         env=env,
-        close_fds=True,
+        close_fds=(not sys.platform.startswith('win')),
     )
 
     result = {
diff --git a/nipype/utils/filemanip.py b/nipype/utils/filemanip.py
--- a/nipype/utils/filemanip.py
+++ b/nipype/utils/filemanip.py
@@ -38,6 +38,7 @@
     ('.BRIK', '.HEAD'),
 ]
 
+PY3 = sys.version_info[0] >= 3
 
 class FileNotFoundError(Exception):
     pass
@@ -877,12 +878,18 @@ def canonicalize_env(env):
     if os.name != 'nt':
         return env
 
+    # convert unicode to string for python 2
+    if not PY3:
+        from future.utils import bytes_to_native_str
     out_env = {}
     for key, val in env.items():
         if not isinstance(key, bytes):
             key = key.encode('utf-8')
         if not isinstance(val, bytes):
             val = val.encode('utf-8')
+        if not PY3:
+            key = bytes_to_native_str(key)
+            val = bytes_to_native_str(val)
         out_env[key] = val
     return out_env
 


Test patch: