hub/venv/lib/python3.7/site-packages/parso/_compatibility.py

70 lines
1.6 KiB
Python
Raw Normal View History

"""
To ensure compatibility from Python ``2.7`` - ``3.3``, a module has been
created. Clearly there is huge need to use conforming syntax.
"""
import sys
import platform
# unicode function
try:
unicode = unicode
except NameError:
unicode = str
is_pypy = platform.python_implementation() == 'PyPy'
def use_metaclass(meta, *bases):
""" Create a class with a metaclass. """
if not bases:
bases = (object,)
return meta("HackClass", bases, {})
try:
encoding = sys.stdout.encoding
if encoding is None:
encoding = 'utf-8'
except AttributeError:
encoding = 'ascii'
def u(string):
"""Cast to unicode DAMMIT!
Written because Python2 repr always implicitly casts to a string, so we
have to cast back to a unicode (and we know that we always deal with valid
unicode, because we check that in the beginning).
"""
if sys.version_info.major >= 3:
return str(string)
if not isinstance(string, unicode):
return unicode(str(string), 'UTF-8')
return string
try:
# Python 2.7
FileNotFoundError = FileNotFoundError
except NameError:
# Python 3.3+
FileNotFoundError = IOError
def utf8_repr(func):
"""
``__repr__`` methods in Python 2 don't allow unicode objects to be
returned. Therefore cast them to utf-8 bytes in this decorator.
"""
def wrapper(self):
result = func(self)
if isinstance(result, unicode):
return result.encode('utf-8')
else:
return result
if sys.version_info.major >= 3:
return func
else:
return wrapper