summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKostis Anagnostopoulos <ankostis@gmail.com>2016-09-27 17:23:53 +0200
committerKostis Anagnostopoulos <ankostis@gmail.com>2016-09-28 03:35:38 +0200
commitdf2fb548040c8313f4bb98870788604bc973fa18 (patch)
tree4c11a309cfc6821b5ecd847592a451820c303463
parent25a2ebfa684f7ef37a9298c5ded2fc5af190cb42 (diff)
downloadgitpython-df2fb548040c8313f4bb98870788604bc973fa18.tar.gz
PY2, #519: FIX GitCommandError.tostr() encoding issue
+ PY3 means "PY3 or later" (TODO: fix also for *gitdb* project).
-rw-r--r--git/compat.py21
-rw-r--r--git/exc.py15
2 files changed, 22 insertions, 14 deletions
diff --git a/git/compat.py b/git/compat.py
index ff382ce8..8c5036c6 100644
--- a/git/compat.py
+++ b/git/compat.py
@@ -11,7 +11,6 @@ import os
import sys
from gitdb.utils.compat import (
- PY3,
xrange,
MAXSIZE,
izip,
@@ -24,7 +23,9 @@ from gitdb.utils.encoding import (
force_text
)
+PY3 = sys.version_info[0] >= 3
defenc = sys.getdefaultencoding()
+
if PY3:
import io
FileType = io.IOBase
@@ -74,13 +75,8 @@ def with_metaclass(meta, *bases):
# we set the __metaclass__ attribute explicitly
if not PY3 and '___metaclass__' not in d:
d['__metaclass__'] = meta
- # end
return meta(name, bases, d)
- # end
- # end metaclass
return metaclass(meta.__name__ + 'Helper', None, {})
- # end handle py2
-
def is_win():
return os.name == 'nt'
@@ -93,3 +89,16 @@ def is_posix():
def is_darwin():
return os.name == 'darwin'
+
+## From https://docs.python.org/3.3/howto/pyporting.html
+class UnicodeMixin(object):
+
+ """Mixin class to handle defining the proper __str__/__unicode__
+ methods in Python 2 or 3."""
+
+ if sys.version_info[0] >= 3: # Python 3
+ def __str__(self):
+ return self.__unicode__()
+ else: # Python 2
+ def __str__(self):
+ return self.__unicode__().encode('utf8')
diff --git a/git/exc.py b/git/exc.py
index 34382ecd..3a93c447 100644
--- a/git/exc.py
+++ b/git/exc.py
@@ -6,8 +6,7 @@
""" Module containing all exceptions thrown througout the git package, """
from gitdb.exc import * # NOQA
-
-from git.compat import defenc
+from git.compat import UnicodeMixin, safe_decode
class InvalidGitRepositoryError(Exception):
@@ -28,7 +27,7 @@ class GitCommandNotFound(Exception):
pass
-class GitCommandError(Exception):
+class GitCommandError(UnicodeMixin, Exception):
""" Thrown if execution of the git command fails with non-zero status code. """
def __init__(self, command, status, stderr=None, stdout=None):
@@ -37,13 +36,13 @@ class GitCommandError(Exception):
self.status = status
self.command = command
- def __str__(self):
- ret = "'%s' returned with exit code %i" % \
- (' '.join(str(i) for i in self.command), self.status)
+ def __unicode__(self):
+ ret = u"'%s' returned with exit code %s" % \
+ (u' '.join(safe_decode(i) for i in self.command), self.status)
if self.stderr:
- ret += "\nstderr: '%s'" % self.stderr.decode(defenc)
+ ret += u"\nstderr: '%s'" % safe_decode(self.stderr)
if self.stdout:
- ret += "\nstdout: '%s'" % self.stdout.decode(defenc)
+ ret += u"\nstdout: '%s'" % safe_decode(self.stdout)
return ret