Skip to content

Commit

Permalink
Merge pull request #3267 from effigies/fix/canonicalize_env
Browse files Browse the repository at this point in the history
FIX: Canonicalize environment dicts to strings in Windows
  • Loading branch information
effigies authored Nov 28, 2020
2 parents 35b7927 + 0dbcedd commit f054c29
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from ... import config, logging, LooseVersion
from ...utils.provenance import write_provenance
from ...utils.misc import str2bool, rgetcwd
from ...utils.filemanip import split_filename, which, get_dependencies
from ...utils.filemanip import split_filename, which, get_dependencies, canonicalize_env
from ...utils.subprocess import run_command

from ...external.due import due
Expand Down Expand Up @@ -778,7 +778,7 @@ def version_from_command(self, flag="-v", cmd=None):
proc = sp.Popen(
" ".join((cmd, flag)),
shell=True,
env=env,
env=canonicalize_env(env),
stdout=sp.PIPE,
stderr=sp.PIPE,
)
Expand Down
18 changes: 7 additions & 11 deletions nipype/utils/filemanip.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@

fmlogger = logging.getLogger("nipype.utils")

related_filetype_sets = [
(".hdr", ".img", ".mat"),
(".nii", ".mat"),
(".BRIK", ".HEAD"),
]
related_filetype_sets = [(".hdr", ".img", ".mat"), (".nii", ".mat"), (".BRIK", ".HEAD")]


def _resolve_with_filenotfound(path, **kwargs):
Expand Down Expand Up @@ -876,7 +872,7 @@ def get_dependencies(name, environ):


def canonicalize_env(env):
"""Windows requires that environment be dicts with bytes as keys and values
"""Windows requires that environment be dicts with str as keys and values
This function converts any unicode entries for Windows only, returning the
dictionary untouched in other environments.
Expand All @@ -888,18 +884,18 @@ def canonicalize_env(env):
Returns
-------
env : dict
Windows: environment dictionary with bytes keys and values
Windows: environment dictionary with str keys and values
Other: untouched input ``env``
"""
if os.name != "nt":
return env

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 isinstance(key, str):
key = key.decode("utf-8")
if not isinstance(val, str):
val = val.decode("utf-8")
out_env[key] = val
return out_env

Expand Down

0 comments on commit f054c29

Please sign in to comment.