Skip to content

FIX: Constrain environment dictionary to bytes in Windows #2085

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 1 commit into from
Jun 23, 2017
Merged
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
35 changes: 33 additions & 2 deletions nipype/interfaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,35 @@ def _get_ram_mb(pid, pyfunc=False):
return mem_mb


def _canonicalize_env(env):
"""Windows requires that environment be dicts with bytes as keys and values
This function converts any unicode entries for Windows only, returning the
dictionary untouched in other environments.

Parameters
----------
env : dict
environment dictionary with unicode or bytes keys and values

Returns
-------
env : dict
Windows: environment dictionary with bytes keys and values
Other: untouched input ``env``
"""
if os.name != 'nt':
return env

out_env = {}
for key, val in env:
if not isinstance(key, bytes):
key = key.encode('utf-8')
if not isinstance(val, bytes):
val = key.encode('utf-8')
out_env[key] = val
return out_env


# Get max resources used for process
def get_max_resources_used(pid, mem_mb, num_threads, pyfunc=False):
"""Function to get the RAM and threads usage of a process
Expand Down Expand Up @@ -1435,6 +1464,8 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
raise RuntimeError('Xvfb was not found, X redirection aborted')
cmdline = 'xvfb-run -a ' + cmdline

env = _canonicalize_env(runtime.environ)

default_encoding = locale.getdefaultlocale()[1]
if default_encoding is None:
default_encoding = 'UTF-8'
Expand All @@ -1449,14 +1480,14 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
stderr=stderr,
shell=True,
cwd=runtime.cwd,
env=runtime.environ)
env=env)
else:
proc = subprocess.Popen(cmdline,
stdout=PIPE,
stderr=PIPE,
shell=True,
cwd=runtime.cwd,
env=runtime.environ)
env=env)
result = {}
errfile = os.path.join(runtime.cwd, 'stderr.nipype')
outfile = os.path.join(runtime.cwd, 'stdout.nipype')
Expand Down