Skip to content

FIX: Canonicalize environment dicts to strings in Windows #3267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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