summaryrefslogtreecommitdiff
path: root/git/compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'git/compat.py')
-rw-r--r--git/compat.py74
1 files changed, 58 insertions, 16 deletions
diff --git a/git/compat.py b/git/compat.py
index b3572474..e7243e25 100644
--- a/git/compat.py
+++ b/git/compat.py
@@ -7,37 +7,47 @@
"""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,
+ MAXSIZE, # @UnusedImport
+ izip, # @UnusedImport
)
-
from gitdb.utils.encoding import (
- string_types,
- text_type,
- force_bytes,
- force_text
+ string_types, # @UnusedImport
+ text_type, # @UnusedImport
+ force_bytes, # @UnusedImport
+ force_text # @UnusedImport
)
+
+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
+
def byte_ord(b):
return b
+
def bchr(n):
return bytes([n])
+
def mviter(d):
return d.values()
- range = xrange
+
+ range = xrange # @ReservedAssignment
unicode = str
binary_type = bytes
else:
- FileType = file
+ FileType = file # @UndefinedVariable on PY3
# usually, this is just ascii, which might not enough for our encoding needs
# Unless it's set specifically, we override it to be utf-8
if defenc == 'ascii':
@@ -46,7 +56,8 @@ else:
bchr = chr
unicode = unicode
binary_type = str
- range = xrange
+ range = xrange # @ReservedAssignment
+
def mviter(d):
return d.itervalues()
@@ -57,7 +68,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 +105,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)