Skip to content

Commit f1d8f08

Browse files
Merge pull request #1189 from allmightyspiff/issues771
Issues771 - Fixing windows Unicode Errors
2 parents f40fffa + 1a59b8a commit f1d8f08

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

SoftLayer/CLI/environment.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,20 @@ def err(self, output, newline=True):
4646
"""Outputs an error string to the console (stderr)."""
4747
click.echo(output, nl=newline, err=True)
4848

49-
def fmt(self, output):
49+
def fmt(self, output, fmt=None):
5050
"""Format output based on current the environment format."""
51-
return formatting.format_output(output, fmt=self.format)
51+
if fmt is None:
52+
fmt = self.format
53+
return formatting.format_output(output, fmt)
5254

5355
def fout(self, output, newline=True):
5456
"""Format the input and output to the console (stdout)."""
5557
if output is not None:
56-
self.out(self.fmt(output), newline=newline)
58+
try:
59+
self.out(self.fmt(output), newline=newline)
60+
except UnicodeEncodeError:
61+
# If we hit an undecodeable entry, just try outputting as json.
62+
self.out(self.fmt(output, 'json'), newline=newline)
5763

5864
def input(self, prompt, default=None, show_default=True):
5965
"""Provide a command prompt."""

tests/CLI/environment_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ def test_resolve_alias(self):
6262

6363
r = self.env.resolve_alias('realname')
6464
self.assertEqual(r, 'realname')
65+
66+
@mock.patch('click.echo')
67+
def test_print_unicode(self, echo):
68+
output = "\u3010TEST\u3011 image"
69+
# https://docs.python.org/3.6/library/exceptions.html#UnicodeError
70+
echo.side_effect = [
71+
UnicodeEncodeError('utf8', output, 0, 1, "Test Exception"),
72+
output
73+
]
74+
self.env.fout(output)
75+
self.assertEqual(2, echo.call_count)

0 commit comments

Comments
 (0)