@@ -1381,6 +1381,35 @@ def _get_ram_mb(pid, pyfunc=False):
1381
1381
return mem_mb
1382
1382
1383
1383
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
+
1384
1413
# Get max resources used for process
1385
1414
def get_max_resources_used (pid , mem_mb , num_threads , pyfunc = False ):
1386
1415
"""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):
1435
1464
raise RuntimeError ('Xvfb was not found, X redirection aborted' )
1436
1465
cmdline = 'xvfb-run -a ' + cmdline
1437
1466
1467
+ env = _canonicalize_env (runtime .environ )
1468
+
1438
1469
default_encoding = locale .getdefaultlocale ()[1 ]
1439
1470
if default_encoding is None :
1440
1471
default_encoding = 'UTF-8'
@@ -1449,14 +1480,14 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
1449
1480
stderr = stderr ,
1450
1481
shell = True ,
1451
1482
cwd = runtime .cwd ,
1452
- env = runtime . environ )
1483
+ env = env )
1453
1484
else :
1454
1485
proc = subprocess .Popen (cmdline ,
1455
1486
stdout = PIPE ,
1456
1487
stderr = PIPE ,
1457
1488
shell = True ,
1458
1489
cwd = runtime .cwd ,
1459
- env = runtime . environ )
1490
+ env = env )
1460
1491
result = {}
1461
1492
errfile = os .path .join (runtime .cwd , 'stderr.nipype' )
1462
1493
outfile = os .path .join (runtime .cwd , 'stdout.nipype' )
0 commit comments