open-research / sumatra

http://neuralensemble.org/sumatra/
BSD 2-Clause "Simplified" License
127 stars 48 forks source link

Encoding error when redirecting output to File #345

Open Jencke opened 8 years ago

Jencke commented 8 years ago

This is might be connected to #213 and #172 I'm running sumatra version 0.7.4 in GNU/Linux (debian)

I have a non-ASCII user-name and I get a encoding error when redirecting the output of smt list.

For example running: smt list -l > foo

Results in the Error: Traceback (most recent call last): File "/usr/local/bin/smt", line 31, in <module> main(sys.argv[2:]) File "/usr/local/lib/python2.7/dist-packages/sumatra/commands.py", line 435, in list print(project.format_records(tags=args.tags, mode=args.mode, format=args.format, reverse=args.reverse)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 785: ordinal not in range(128)

This error is due to how Python determines how to encode the output. Python checks sys.stdout.encoding to find out how it should encode. If the output is redirected to a file sys.stdout.encoding is None so Python falls back and encodes in ASCII (Actually only Python 2. Python 3 defaults to UTF8)

I dirty-fixed the problem by changing line 785 in commands.py to: print(project.format_records(tags=args.tags, mode=args.mode, format=args.format, reverse=args.reverse).encode('utf8'))

But this probably happens in multiple places. If you are happy with this kind of fix I could fix it in all places I find and open up a pull request.

apdavison commented 7 years ago

:+1: for the proposed fix