Discussion:
[Fab-user] remote host output encoding
Mike Miller
2013-10-02 03:30:22 UTC
Permalink
Hi,

I've been searching for information on character encoding and fabric but coming
up empty. I've got a command printing an occasional Unicode char and so when
using the result object I get a UnicodeDecodeError:

output = run('unicode_printer')

'returned: ' + output # kaboom

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

What should I do about this? I could hard-code it to decode with utf8, but
can't guarantee it will be utf8. I can imagine running a command earlier to get
the encoding and then decoding it. For example:

encoding = run("python2 -c 'import locale; " +
"print locale.getdefaultlocale()[1]'")

output = run('unicode_printer')

'returned: ' + output.decode(encoding)

Wondering if there was a parameter or easier way that I'm missing.

Thanks,
Jeff Forcier
2013-10-02 15:55:10 UTC
Permalink
Unfortunately, Fabric isn't great with Unicode at the moment so
there's no 'easy way' you're overlooking. If you're worried that you
can't reliably .decode('utf-8'), which is the usual thing I see folks
do here (in general - not necessarily in fab) then I think your remote
encoding test probably is a good place to start.

Could maybe also test e.g. 'echo $LANG' but if you know the remote
node has Python already, using the locale module is a nice shortcut.

-Jeff
Post by Mike Miller
Hi,
I've been searching for information on character encoding and fabric but
coming up empty. I've got a command printing an occasional Unicode char and
output = run('unicode_printer')
'returned: ' + output # kaboom
ordinal not in range(128)
What should I do about this? I could hard-code it to decode with utf8, but
can't guarantee it will be utf8. I can imagine running a command earlier to
encoding = run("python2 -c 'import locale; " +
"print locale.getdefaultlocale()[1]'")
output = run('unicode_printer')
'returned: ' + output.decode(encoding)
Wondering if there was a parameter or easier way that I'm missing.
Thanks,
_______________________________________________
Fab-user mailing list
https://lists.nongnu.org/mailman/listinfo/fab-user
--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org
Loading...