Skip to content

Commit 94a11f0

Browse files
authored
Merge pull request #2085 from effigies/fix/windows_env
FIX: Constrain environment dictionary to bytes in Windows
2 parents 62e4958 + da57c93 commit 94a11f0

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

nipype/interfaces/base.py

+33-2
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,35 @@ def _get_ram_mb(pid, pyfunc=False):
13811381
return mem_mb
13821382

13831383

1384+
def _canonicalize_env(env):
1385+
"""Windows requires that environment be dicts with bytes as keys and values
1386+
This function converts any unicode entries for Windows only, returning the
1387+
dictionary untouched in other environments.
1388+
1389+
Parameters
1390+
----------
1391+
env : dict
1392+
environment dictionary with unicode or bytes keys and values
1393+
1394+
Returns
1395+
-------
1396+
env : dict
1397+
Windows: environment dictionary with bytes keys and values
1398+
Other: untouched input ``env``
1399+
"""
1400+
if os.name != 'nt':
1401+
return env
1402+
1403+
out_env = {}
1404+
for key, val in env:
1405+
if not isinstance(key, bytes):
1406+
key = key.encode('utf-8')
1407+
if not isinstance(val, bytes):
1408+
val = key.encode('utf-8')
1409+
out_env[key] = val
1410+
return out_env
1411+
1412+
13841413
# Get max resources used for process
13851414
def get_max_resources_used(pid, mem_mb, num_threads, pyfunc=False):
13861415
"""Function to get the RAM and threads usage of a process
@@ -1435,6 +1464,8 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
14351464
raise RuntimeError('Xvfb was not found, X redirection aborted')
14361465
cmdline = 'xvfb-run -a ' + cmdline
14371466

1467+
env = _canonicalize_env(runtime.environ)
1468+
14381469
default_encoding = locale.getdefaultlocale()[1]
14391470
if default_encoding is None:
14401471
default_encoding = 'UTF-8'
@@ -1449,14 +1480,14 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
14491480
stderr=stderr,
14501481
shell=True,
14511482
cwd=runtime.cwd,
1452-
env=runtime.environ)
1483+
env=env)
14531484
else:
14541485
proc = subprocess.Popen(cmdline,
14551486
stdout=PIPE,
14561487
stderr=PIPE,
14571488
shell=True,
14581489
cwd=runtime.cwd,
1459-
env=runtime.environ)
1490+
env=env)
14601491
result = {}
14611492
errfile = os.path.join(runtime.cwd, 'stderr.nipype')
14621493
outfile = os.path.join(runtime.cwd, 'stdout.nipype')

0 commit comments

Comments
 (0)