Skip to content

Commit

Permalink
Fix encoding error when printing to redirected output
Browse files Browse the repository at this point in the history
When redirecting output to a file sys.stdout.encoding is None, so use
sys.getdefaultencoding() instead.
  • Loading branch information
cdleonard committed Jul 28, 2014
1 parent 8ce3e30 commit e236fd9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions gitlab.py
Expand Up @@ -445,6 +445,8 @@ def Team(self, id=None, **kwargs):
"""
return self._getListOrObject(Team, id, **kwargs)

def _get_display_encoding():
return sys.stdout.encoding or sys.getdefaultencoding()

class GitlabObject(object):
_url = None
Expand Down Expand Up @@ -574,7 +576,7 @@ def _obj_to_str(obj):
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
return "[ %s ]" % s
elif isinstance(obj, unicode):
return obj.encode(sys.stdout.encoding, "replace")
return obj.encode(_get_display_encoding(), "replace")
else:
return str(obj)

Expand All @@ -585,8 +587,8 @@ def pretty_print(self, depth=0):
if k == self.idAttr:
continue
v = self.__dict__[k]
pretty_k = k.replace('_', '-').encode(sys.stdout.encoding,
"replace")
pretty_k = k.replace('_', '-')
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
if isinstance(v, GitlabObject):
if depth == 0:
print("%s:" % pretty_k)
Expand Down

0 comments on commit e236fd9

Please sign in to comment.