diff --git a/gitlab.py b/gitlab.py index a64fdcada..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, @@ -445,6 +447,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 +578,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 +589,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)