From b7d197c2416d67f1f50f6bf6692e6b6dfd69ac76 Mon Sep 17 00:00:00 2001 From: baldulin Date: Thu, 8 May 2014 13:51:52 +0200 Subject: [PATCH] Fixed encoding error while piped output If a ran python-gitlab with the -v option piping to another process was not possible. This was due to the fact that sys.stdout.encoding was set to None. The problem is solved if the default encoding is used if sys.stdout.encoding is None. --- gitlab.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gitlab.py b/gitlab.py index a64fdcada..90b33024d 100644 --- a/gitlab.py +++ b/gitlab.py @@ -574,7 +574,11 @@ 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") + if sys.stdout.encoding is None: + return obj.encode(sys.getdefaultencoding(), "replace") + else: + return obj.encode(sys.stdout.encoding, "replace") + else: return str(obj) @@ -585,8 +589,14 @@ def pretty_print(self, depth=0): if k == self.idAttr: continue v = self.__dict__[k] - pretty_k = k.replace('_', '-').encode(sys.stdout.encoding, - "replace") + + if sys.stdout.encoding is None: + pretty_k = k.replace('_','-').encode(sys.getdefaultencoding(), + "replace") + else: + pretty_k = k.replace('_', '-').encode(sys.stdout.encoding, + "replace") + if isinstance(v, GitlabObject): if depth == 0: print("%s:" % pretty_k)