Skip to content

Commit df2fb54

Browse files
committed
PY2, #519: FIX GitCommandError.tostr() encoding issue
+ PY3 means "PY3 or later" (TODO: fix also for *gitdb* project).
1 parent 25a2ebf commit df2fb54

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

git/compat.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import sys
1212

1313
from gitdb.utils.compat import (
14-
PY3,
1514
xrange,
1615
MAXSIZE,
1716
izip,
@@ -24,7 +23,9 @@
2423
force_text
2524
)
2625

26+
PY3 = sys.version_info[0] >= 3
2727
defenc = sys.getdefaultencoding()
28+
2829
if PY3:
2930
import io
3031
FileType = io.IOBase
@@ -74,13 +75,8 @@ def __new__(cls, name, nbases, d):
7475
# we set the __metaclass__ attribute explicitly
7576
if not PY3 and '___metaclass__' not in d:
7677
d['__metaclass__'] = meta
77-
# end
7878
return meta(name, bases, d)
79-
# end
80-
# end metaclass
8179
return metaclass(meta.__name__ + 'Helper', None, {})
82-
# end handle py2
83-
8480

8581
def is_win():
8682
return os.name == 'nt'
@@ -93,3 +89,16 @@ def is_posix():
9389
def is_darwin():
9490
return os.name == 'darwin'
9591

92+
93+
## From https://docs.python.org/3.3/howto/pyporting.html
94+
class UnicodeMixin(object):
95+
96+
"""Mixin class to handle defining the proper __str__/__unicode__
97+
methods in Python 2 or 3."""
98+
99+
if sys.version_info[0] >= 3: # Python 3
100+
def __str__(self):
101+
return self.__unicode__()
102+
else: # Python 2
103+
def __str__(self):
104+
return self.__unicode__().encode('utf8')

git/exc.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
""" Module containing all exceptions thrown througout the git package, """
77

88
from gitdb.exc import * # NOQA
9-
10-
from git.compat import defenc
9+
from git.compat import UnicodeMixin, safe_decode
1110

1211

1312
class InvalidGitRepositoryError(Exception):
@@ -28,7 +27,7 @@ class GitCommandNotFound(Exception):
2827
pass
2928

3029

31-
class GitCommandError(Exception):
30+
class GitCommandError(UnicodeMixin, Exception):
3231
""" Thrown if execution of the git command fails with non-zero status code. """
3332

3433
def __init__(self, command, status, stderr=None, stdout=None):
@@ -37,13 +36,13 @@ def __init__(self, command, status, stderr=None, stdout=None):
3736
self.status = status
3837
self.command = command
3938

40-
def __str__(self):
41-
ret = "'%s' returned with exit code %i" % \
42-
(' '.join(str(i) for i in self.command), self.status)
39+
def __unicode__(self):
40+
ret = u"'%s' returned with exit code %s" % \
41+
(u' '.join(safe_decode(i) for i in self.command), self.status)
4342
if self.stderr:
44-
ret += "\nstderr: '%s'" % self.stderr.decode(defenc)
43+
ret += u"\nstderr: '%s'" % safe_decode(self.stderr)
4544
if self.stdout:
46-
ret += "\nstdout: '%s'" % self.stdout.decode(defenc)
45+
ret += u"\nstdout: '%s'" % safe_decode(self.stdout)
4746
return ret
4847

4948

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy