From e236fd94c48b949bbbc8e0dc2d55ebfaa1ef0069 Mon Sep 17 00:00:00 2001 From: Crestez Dan Leonard Date: Mon, 28 Jul 2014 20:43:34 +0300 Subject: Fix encoding error when printing to redirected output When redirecting output to a file sys.stdout.encoding is None, so use sys.getdefaultencoding() instead. --- gitlab.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'gitlab.py') 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) -- cgit v1.2.1