mirror of
https://github.com/Manuel83/craftbeerpi3
synced 2026-09-19 15:59:07 +02:00
python 3 migration
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
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
|
||||
# Assume no memory view ...
|
||||
def to_bytes(i):
|
||||
return i
|
||||
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
|
||||
# smmap can return memory view objects, which can't be compared as buffers/bytes can ...
|
||||
def to_bytes(i):
|
||||
if isinstance(i, memoryview):
|
||||
return i.tobytes()
|
||||
return i
|
||||
|
||||
memoryview = memoryview
|
||||
|
||||
try:
|
||||
MAXSIZE = sys.maxint
|
||||
except AttributeError:
|
||||
MAXSIZE = sys.maxsize
|
||||
@@ -0,0 +1,31 @@
|
||||
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, bytes):
|
||||
return data.decode(encoding)
|
||||
|
||||
if compat.PY3:
|
||||
return text_type(data, encoding)
|
||||
else:
|
||||
return text_type(data)
|
||||
Reference in New Issue
Block a user