summaryrefslogtreecommitdiff
path: root/gitdb/utils
diff options
context:
space:
mode:
Diffstat (limited to 'gitdb/utils')
-rw-r--r--gitdb/utils/__init__.py0
-rw-r--r--gitdb/utils/compat.py35
-rw-r--r--gitdb/utils/encoding.py29
3 files changed, 64 insertions, 0 deletions
diff --git a/gitdb/utils/__init__.py b/gitdb/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/gitdb/utils/__init__.py
diff --git a/gitdb/utils/compat.py b/gitdb/utils/compat.py
new file mode 100644
index 0000000..a2640fd
--- /dev/null
+++ b/gitdb/utils/compat.py
@@ -0,0 +1,35 @@
+import sys
+
+PY3 = sys.version_info[0] == 3
+
+try:
+ from itertools import izip
+ xrange = xrange
+except ImportError:
+ # py3
+ izip = zip
+ xrange = range
+# end handle python version
+
+try:
+ # Python 2
+ buffer = buffer
+ memoryview = buffer
+except NameError:
+ # Python 3 has no `buffer`; only `memoryview`
+ # However, it's faster to just slice the object directly, maybe it keeps a view internally
+ def buffer(obj, offset, size=None):
+ if size is None:
+ # return memoryview(obj)[offset:]
+ return obj[offset:]
+ else:
+ # return memoryview(obj)[offset:offset+size]
+ return obj[offset:offset+size]
+ # end buffer reimplementation
+
+ memoryview = memoryview
+
+try:
+ MAXSIZE = sys.maxint
+except AttributeError:
+ MAXSIZE = sys.maxsize
diff --git a/gitdb/utils/encoding.py b/gitdb/utils/encoding.py
new file mode 100644
index 0000000..2d03ad3
--- /dev/null
+++ b/gitdb/utils/encoding.py
@@ -0,0 +1,29 @@
+from gitdb.utils import compat
+
+if compat.PY3:
+ string_types = (str, )
+ text_type = str
+else:
+ string_types = (basestring, )
+ text_type = unicode
+
+def force_bytes(data, encoding="ascii"):
+ if isinstance(data, bytes):
+ return data
+
+ if isinstance(data, string_types):
+ return data.encode(encoding)
+
+ return data
+
+def force_text(data, encoding="utf-8"):
+ if isinstance(data, text_type):
+ return data
+
+ if isinstance(data, string_types):
+ return data.decode(encoding)
+
+ if compat.PY3:
+ return text_type(data, encoding)
+ else:
+ return text_type(data)