Skip to content

Commit e236fd9

Browse files
committed
Fix encoding error when printing to redirected output
When redirecting output to a file sys.stdout.encoding is None, so use sys.getdefaultencoding() instead.
1 parent 8ce3e30 commit e236fd9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

gitlab.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ def Team(self, id=None, **kwargs):
445445
"""
446446
return self._getListOrObject(Team, id, **kwargs)
447447

448+
def _get_display_encoding():
449+
return sys.stdout.encoding or sys.getdefaultencoding()
448450

449451
class GitlabObject(object):
450452
_url = None
@@ -574,7 +576,7 @@ def _obj_to_str(obj):
574576
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
575577
return "[ %s ]" % s
576578
elif isinstance(obj, unicode):
577-
return obj.encode(sys.stdout.encoding, "replace")
579+
return obj.encode(_get_display_encoding(), "replace")
578580
else:
579581
return str(obj)
580582

@@ -585,8 +587,8 @@ def pretty_print(self, depth=0):
585587
if k == self.idAttr:
586588
continue
587589
v = self.__dict__[k]
588-
pretty_k = k.replace('_', '-').encode(sys.stdout.encoding,
589-
"replace")
590+
pretty_k = k.replace('_', '-')
591+
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
590592
if isinstance(v, GitlabObject):
591593
if depth == 0:
592594
print("%s:" % pretty_k)

0 commit comments

Comments
 (0)