Replace five with six

This commit is contained in:
Anthony Sottile 2017-03-08 13:06:48 -08:00
parent cb8dd335f4
commit c65a11ce3d
9 changed files with 27 additions and 57 deletions

View file

@ -3,10 +3,11 @@ from __future__ import print_function
from __future__ import unicode_literals
import contextlib
import io
import os.path
import traceback
import six
from pre_commit import five
from pre_commit import output
from pre_commit.errors import FatalError
@ -22,7 +23,7 @@ def _to_bytes(exc):
try:
return bytes(exc)
except Exception:
return five.text(exc).encode('UTF-8')
return six.text_type(exc).encode('UTF-8')
def _log_and_exit(msg, exc, formatted):
@ -35,7 +36,7 @@ def _log_and_exit(msg, exc, formatted):
output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
store = Store()
store.require_created()
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
output.write(error_msg, stream=log)
output.write_line(formatted, stream=log)
raise PreCommitSystemExit(1)

View file

@ -1,42 +1,14 @@
from __future__ import unicode_literals
PY2 = str is bytes
PY3 = str is not bytes
if PY2: # pragma: no cover (PY2 only)
text = unicode # flake8: noqa
string_types = (text, bytes)
def n(s):
if isinstance(s, bytes):
return s
else:
return s.encode('UTF-8')
exec("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
else: # pragma: no cover (PY3 only)
text = str
string_types = (text,)
def n(s):
if isinstance(s, text):
return s
else:
return s.decode('UTF-8')
def reraise(tp, value, tb=None):
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
import six
def to_text(s):
return s if isinstance(s, text) else s.decode('UTF-8')
return s if isinstance(s, six.text_type) else s.decode('UTF-8')
def to_bytes(s):
return s if isinstance(s, bytes) else s.encode('UTF-8')
n = to_bytes if six.PY2 else to_text

View file

@ -5,7 +5,6 @@ from __future__ import unicode_literals
import os.path
import tarfile
from pre_commit import five
from pre_commit import output
from pre_commit.util import cmd_output
from pre_commit.util import cwd
@ -54,7 +53,7 @@ def make_archive(name, repo, ref, destdir):
# runtime
rmtree(os.path.join(tempdir, '.git'))
with tarfile.open(five.n(output_path), 'w|gz') as tf:
with tarfile.open(output_path, 'w|gz') as tf:
tf.add(tempdir, name)
return output_path

View file

@ -8,7 +8,7 @@ import os.path
import re
import sys
from pre_commit import five
import six
class ValidationError(ValueError):
@ -37,7 +37,7 @@ def validate_context(msg):
yield
except ValidationError as e:
_, _, tb = sys.exc_info()
five.reraise(ValidationError, ValidationError(e, ctx=msg), tb)
six.reraise(ValidationError, ValidationError(e, ctx=msg), tb)
@contextlib.contextmanager
@ -46,7 +46,7 @@ def reraise_as(tp):
yield
except ValidationError as e:
_, _, tb = sys.exc_info()
five.reraise(tp, tp(e), tb)
six.reraise(tp, tp(e), tb)
def _dct_noop(self, dct):
@ -218,7 +218,7 @@ def check_type(tp, typename=None):
check_bool = check_type(bool)
check_string = check_type(five.string_types, typename='string')
check_string = check_type(six.string_types, typename='string')
def check_regex(v):

View file

@ -10,6 +10,7 @@ import subprocess
import tempfile
import pkg_resources
import six
from pre_commit import five
from pre_commit import parse_shebang
@ -143,12 +144,12 @@ class CalledProcessError(RuntimeError):
def to_text(self):
return self.to_bytes().decode('UTF-8')
if five.PY3: # pragma: no cover (py3)
__bytes__ = to_bytes
__str__ = to_text
else: # pragma: no cover (py2)
if six.PY2: # pragma: no cover (py2)
__str__ = to_bytes
__unicode__ = to_text
else: # pragma: no cover (py3)
__bytes__ = to_bytes
__str__ = to_text
def cmd_output(*cmd, **kwargs):