From e236fd94c48b949bbbc8e0dc2d55ebfaa1ef0069 Mon Sep 17 00:00:00 2001 From: Crestez Dan Leonard Date: Mon, 28 Jul 2014 20:43:34 +0300 Subject: [PATCH 1/2] 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(-) diff --git a/gitlab.py b/gitlab.py index a64fdcada..7eb844fb4 100644 --- a/gitlab.py +++ b/gitlab.py @@ -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 @@ -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) @@ -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) From ec185cf416adce98e0a3ef338720091c0e2a53fe Mon Sep 17 00:00:00 2001 From: Crestez Dan Leonard Date: Mon, 28 Jul 2014 20:42:46 +0300 Subject: [PATCH 2/2] Fix encoding error when updating with redirected output When output is redirected sys.stdout.encoding is None. Fix this by always encoding to utf-8, assuming gitlab handles that. The encoding used by the local system is irrelevant here. --- gitlab.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gitlab.py b/gitlab.py index 7eb844fb4..a7ec48747 100644 --- a/gitlab.py +++ b/gitlab.py @@ -89,6 +89,8 @@ def __init__(self, url, private_token=None, self.email = email self.password = password self.ssl_verify = ssl_verify + # Gitlab should handle UTF-8 + self.gitlab_encoding = 'UTF-8' def auth(self): """Performs an authentication using either the private token, or the @@ -302,7 +304,7 @@ def update(self, obj): if type(v) in (int, str, bool): d[k] = str(v) elif type(v) == unicode: - d[k] = str(v.encode(sys.stdout.encoding, "replace")) + d[k] = str(v.encode(self.gitlab_encoding, "replace")) try: r = requests.put(url, d,