Skip to content

Commit cb207f8

Browse files
committed
Merge pull request #1394 from satra/fix/proc_communicate_py2
fix: unicode characters in stderr on certain systems
2 parents d8c23b1 + cb614b3 commit cb207f8

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Next release
22
============
33

4+
* FIX: Clean up byte/unicode issues using subprocess (https://github.com/nipy/nipype/pull/1394)
45
* FIX: Prevent crash when tvtk is loaded - ETS_TOOLKIT=null (https://github.com/nipy/nipype/pull/973)
56
* ENH: New interfaces in dipy: RESTORE, EstimateResponseSH, CSD and StreamlineTractography
67
(https://github.com/nipy/nipype/pull/1090)

nipype/interfaces/base.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from copy import deepcopy
2020
import datetime
2121
import errno
22+
import locale
2223
import os
2324
import re
2425
import platform
@@ -47,7 +48,7 @@
4748
from ..utils.provenance import write_provenance
4849
from .. import config, logging, LooseVersion
4950
from .. import __version__
50-
from ..external.six import string_types
51+
from ..external.six import string_types, text_type
5152

5253
nipype_version = LooseVersion(__version__)
5354

@@ -1205,8 +1206,8 @@ def run_command(runtime, output=None, timeout=0.01, redirect_x=False):
12051206
if output == 'file':
12061207
errfile = os.path.join(runtime.cwd, 'stderr.nipype')
12071208
outfile = os.path.join(runtime.cwd, 'stdout.nipype')
1208-
stderr = open(errfile, 'wt') # t=='text'===default
1209-
stdout = open(outfile, 'wt')
1209+
stderr = open(errfile, 'wb') # t=='text'===default
1210+
stdout = open(outfile, 'wb')
12101211

12111212
proc = subprocess.Popen(cmdline,
12121213
stdout=stdout,
@@ -1256,26 +1257,17 @@ def _process(drain=0):
12561257
result['merged'] = [r[1] for r in temp]
12571258
if output == 'allatonce':
12581259
stdout, stderr = proc.communicate()
1259-
if stdout and isinstance(stdout, bytes):
1260-
try:
1261-
stdout = stdout.decode()
1262-
except UnicodeDecodeError:
1263-
stdout = stdout.decode("ISO-8859-1")
1264-
if stderr and isinstance(stderr, bytes):
1265-
try:
1266-
stderr = stderr.decode()
1267-
except UnicodeDecodeError:
1268-
stderr = stderr.decode("ISO-8859-1")
1269-
1270-
result['stdout'] = str(stdout).split('\n')
1271-
result['stderr'] = str(stderr).split('\n')
1260+
stdout = stdout.decode(locale.getdefaultlocale()[1])
1261+
stderr = stderr.decode(locale.getdefaultlocale()[1])
1262+
result['stdout'] = stdout.split('\n')
1263+
result['stderr'] = stderr.split('\n')
12721264
result['merged'] = ''
12731265
if output == 'file':
12741266
ret_code = proc.wait()
12751267
stderr.flush()
12761268
stdout.flush()
1277-
result['stdout'] = [line.strip() for line in open(outfile).readlines()]
1278-
result['stderr'] = [line.strip() for line in open(errfile).readlines()]
1269+
result['stdout'] = [line.decode(locale.getdefaultlocale()[1]).strip() for line in open(outfile, 'rb').readlines()]
1270+
result['stderr'] = [line.decode(locale.getdefaultlocale()[1]).strip() for line in open(errfile, 'rb').readlines()]
12791271
result['merged'] = ''
12801272
if output == 'none':
12811273
proc.communicate()

nipype/interfaces/spm/tests/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from nipype.interfaces.spm import no_spm
1313
import nipype.interfaces.matlab as mlab
1414
from nipype.interfaces.spm.base import SPMCommandInputSpec
15-
from nipype.interfaces.base import traits
15+
from nipype.interfaces.base import traits, text_type
1616

1717
try:
1818
matlab_cmd = os.environ['MATLABCMD']
@@ -57,7 +57,7 @@ def test_scan_for_fnames():
5757
def test_spm_path():
5858
spm_path = spm.Info.version()['path']
5959
if spm_path is not None:
60-
yield assert_equal, type(spm_path), type('')
60+
yield assert_equal, type(spm_path), text_type
6161
yield assert_true, 'spm' in spm_path
6262

6363

nipype/interfaces/tests/test_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
skipif)
1515
import nipype.interfaces.base as nib
1616
from nipype.utils.filemanip import split_filename
17-
from nipype.interfaces.base import Undefined, config
17+
from nipype.interfaces.base import Undefined, config, text_type
1818
from traits.testing.nose_tools import skip
1919
import traits.api as traits
2020

@@ -661,7 +661,7 @@ def test_CommandLine_output():
661661
ci.inputs.terminal_output = 'file'
662662
res = ci.run()
663663
yield assert_true, 'stdout.nipype' in res.runtime.stdout
664-
yield assert_equal, type(res.runtime.stdout), type('hi')
664+
yield assert_equal, type(res.runtime.stdout), text_type
665665
ci = nib.CommandLine(command='ls -l')
666666
ci.inputs.terminal_output = 'none'
667667
res = ci.run()

0 commit comments

Comments
 (0)