summaryrefslogtreecommitdiff
path: root/gitlab.py
diff options
context:
space:
mode:
authorCrestez Dan Leonard <lcrestez@ixiacom.com>2014-07-28 20:43:34 +0300
committerCrestez Dan Leonard <lcrestez@ixiacom.com>2014-07-28 20:50:54 +0300
commite236fd94c48b949bbbc8e0dc2d55ebfaa1ef0069 (patch)
treea48c4d63c99ebc4a9a7dc315451c421103953ffe /gitlab.py
parent8ce3e30ba1fd3f9c587746dbe050a528bc6e952a (diff)
downloadgitlab-e236fd94c48b949bbbc8e0dc2d55ebfaa1ef0069.tar.gz
Fix encoding error when printing to redirected output
When redirecting output to a file sys.stdout.encoding is None, so use sys.getdefaultencoding() instead.
Diffstat (limited to 'gitlab.py')
-rw-r--r--gitlab.py8
1 files changed, 5 insertions, 3 deletions
diff --git a/gitlab.py b/gitlab.py
index a64fdca..7eb844f 100644
--- a/gitlab.py
+++ b/gitlab.py
@@ -445,6 +445,8 @@ class Gitlab(object):
"""
return self._getListOrObject(Team, id, **kwargs)
+def _get_display_encoding():
+ return sys.stdout.encoding or sys.getdefaultencoding()
class GitlabObject(object):
_url = None
@@ -574,7 +576,7 @@ class GitlabObject(object):
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)
@@ -585,8 +587,8 @@ class GitlabObject(object):
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)