Skip to content

python 2 bytes/str issue #1393

Closed
Closed
@satra

Description

@satra

in CommandLine interface it terminal_output is allatonce, we do the following:

https://github.com/nipy/nipype/blob/master/nipype/interfaces/base.py#L1257

however on python 2, when the underlying command generates an error the returned string (at least on centos 7), contains some unsavory bytes. this results in an error as seen below

one solution is to detect whether we are on python 2 and not decode the string. any ideas how to solve this for both platforms simultaneously? @matthew-brett @bcipolli

In [45]: from subprocess import Popen, PIPE

In [46]: proc = Popen('cp foo goo', shell=True, stdout=PIPE, stderr=PIPE)

In [47]: stdout, stderr = proc.communicate()

In [48]: stderr
Out[48]: 'cp: cannot stat \xe2\x80\x98foo\xe2\x80\x99: No such file or directory\n'

In [49]: str(stderr)
Out[49]: 'cp: cannot stat \xe2\x80\x98foo\xe2\x80\x99: No such file or directory\n'

In [50]: stderr.decode()
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-50-80fc7c733b30> in <module>()
----> 1 stderr.decode()

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 16: ordinal not in range(128)

In [51]: stderr.decode('ISO-8859-1')
Out[51]: u'cp: cannot stat \xe2\x80\x98foo\xe2\x80\x99: No such file or directory\n'

In [53]: str(stderr.decode('ISO-8859-1'))
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-53-e11a9dfd7944> in <module>()
----> 1 str(stderr.decode('ISO-8859-1'))

UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-18: ordinal not in range(128)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions