diff options
author | Sebastian Thiel <byronimo@gmail.com> | 2016-10-01 20:42:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-01 20:42:27 +0200 |
commit | 9d6b417ea3a4507ea78714f0cb7add75b13032d5 (patch) | |
tree | 77365cb808a255eb53889725bfce775b5090330e /git/compat.py | |
parent | 4592785004ad1a4869d650dc35a1e9099245dad9 (diff) | |
parent | 9a521681ff8614beb8e2c566cf3c475baca22169 (diff) | |
download | gitpython-9d6b417ea3a4507ea78714f0cb7add75b13032d5.tar.gz |
Merge pull request #519 from ankostis/appveyor
Test project on Windows with MINGW/Cygwin git (conda2.7&3.4/cpy-3.5)
Diffstat (limited to 'git/compat.py')
-rw-r--r-- | git/compat.py | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/git/compat.py b/git/compat.py index b3572474..441a3761 100644 --- a/git/compat.py +++ b/git/compat.py @@ -7,15 +7,15 @@ """utilities to help provide compatibility with python 3""" # flake8: noqa +import locale +import os import sys from gitdb.utils.compat import ( - PY3, xrange, MAXSIZE, izip, ) - from gitdb.utils.encoding import ( string_types, text_type, @@ -23,7 +23,13 @@ from gitdb.utils.encoding import ( force_text ) + +PY3 = sys.version_info[0] >= 3 +is_win = (os.name == 'nt') +is_posix = (os.name == 'posix') +is_darwin = (os.name == 'darwin') defenc = sys.getdefaultencoding() + if PY3: import io FileType = io.IOBase @@ -57,7 +63,28 @@ def safe_decode(s): return s elif isinstance(s, bytes): return s.decode(defenc, 'replace') - raise TypeError('Expected bytes or text, but got %r' % (s,)) + elif s is not None: + raise TypeError('Expected bytes or text, but got %r' % (s,)) + + +def safe_encode(s): + """Safely decodes a binary string to unicode""" + if isinstance(s, unicode): + return s.encode(defenc) + elif isinstance(s, bytes): + return s + elif s is not None: + raise TypeError('Expected bytes or text, but got %r' % (s,)) + + +def win_encode(s): + """Encode unicodes for process arguments on Windows.""" + if isinstance(s, unicode): + return s.encode(locale.getpreferredencoding(False)) + elif isinstance(s, bytes): + return s + elif s is not None: + raise TypeError('Expected bytes or text, but got %r' % (s,)) def with_metaclass(meta, *bases): @@ -73,9 +100,19 @@ 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 + + +## 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 PY3: + def __str__(self): + return self.__unicode__() + else: # Python 2 + def __str__(self): + return self.__unicode__().encode(defenc) |