From c65a11ce3d9f1a92738ec1a8bc99de647d6260a2 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 8 Mar 2017 13:06:48 -0800 Subject: [PATCH] Replace five with six --- pre_commit/error_handler.py | 7 ++--- pre_commit/five.py | 38 ++++----------------------- pre_commit/make_archives.py | 3 +-- pre_commit/schema.py | 8 +++--- pre_commit/util.py | 9 ++++--- setup.py | 1 + tests/conftest.py | 4 +-- tests/prefixed_command_runner_test.py | 10 +++---- tests/store_test.py | 4 +-- 9 files changed, 27 insertions(+), 57 deletions(-) diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py index 7cb8053c..a661cc4f 100644 --- a/pre_commit/error_handler.py +++ b/pre_commit/error_handler.py @@ -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) diff --git a/pre_commit/five.py b/pre_commit/five.py index 5bab4e56..de017267 100644 --- a/pre_commit/five.py +++ b/pre_commit/five.py @@ -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 diff --git a/pre_commit/make_archives.py b/pre_commit/make_archives.py index bd12eda3..4baaaa18 100644 --- a/pre_commit/make_archives.py +++ b/pre_commit/make_archives.py @@ -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 diff --git a/pre_commit/schema.py b/pre_commit/schema.py index 60da2530..d34ad737 100644 --- a/pre_commit/schema.py +++ b/pre_commit/schema.py @@ -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): diff --git a/pre_commit/util.py b/pre_commit/util.py index 73719d1b..4c3ad421 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -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): diff --git a/setup.py b/setup.py index 22eb717e..2e016947 100644 --- a/setup.py +++ b/setup.py @@ -43,6 +43,7 @@ setup( 'cached-property', 'nodeenv>=0.11.1', 'pyyaml', + 'six', 'virtualenv', ], entry_points={ diff --git a/tests/conftest.py b/tests/conftest.py index 34f194b0..23eddb59 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,9 +8,9 @@ import os.path import mock import pytest +import six import pre_commit.constants as C -from pre_commit import five from pre_commit import output from pre_commit.logging_handler import add_logging_handler from pre_commit.prefixed_command_runner import PrefixedCommandRunner @@ -29,7 +29,7 @@ def tempdir_factory(tmpdir): self.tmpdir_count = 0 def get(self): - path = tmpdir.join(five.text(self.tmpdir_count)).strpath + path = tmpdir.join(six.text_type(self.tmpdir_count)).strpath self.tmpdir_count += 1 os.mkdir(path) return path diff --git a/tests/prefixed_command_runner_test.py b/tests/prefixed_command_runner_test.py index bb412101..132c2a86 100644 --- a/tests/prefixed_command_runner_test.py +++ b/tests/prefixed_command_runner_test.py @@ -6,7 +6,6 @@ import subprocess import mock import pytest -from pre_commit import five from pre_commit.prefixed_command_runner import PrefixedCommandRunner from pre_commit.util import CalledProcessError @@ -17,10 +16,7 @@ def norm_slash(input_tup): def test_CalledProcessError_str(): error = CalledProcessError( - 1, - [five.n('git'), five.n('status')], - 0, - (five.n('stdout'), five.n('stderr')), + 1, [str('git'), str('status')], 0, (str('stdout'), str('stderr')), ) assert str(error) == ( "Command: ['git', 'status']\n" @@ -35,7 +31,7 @@ def test_CalledProcessError_str(): def test_CalledProcessError_str_nooutput(): error = CalledProcessError( - 1, [five.n('git'), five.n('status')], 0, (five.n(''), five.n('')) + 1, [str('git'), str('status')], 0, (str(''), str('')) ) assert str(error) == ( "Command: ['git', 'status']\n" @@ -78,7 +74,7 @@ def test_run_substitutes_prefix(popen_mock, makedirs_mock): ) ret = instance.run(['{prefix}bar', 'baz'], retcode=None) popen_mock.assert_called_once_with( - (five.n(os.path.join('prefix', 'bar')), five.n('baz')), + (str(os.path.join('prefix', 'bar')), str('baz')), env=None, stdin=subprocess.PIPE, stdout=subprocess.PIPE, diff --git a/tests/store_test.py b/tests/store_test.py index 950693cf..26857965 100644 --- a/tests/store_test.py +++ b/tests/store_test.py @@ -7,8 +7,8 @@ import sqlite3 import mock import pytest +import six -from pre_commit import five from pre_commit.store import _get_default_directory from pre_commit.store import Store from pre_commit.util import cmd_output @@ -116,7 +116,7 @@ def test_clone_cleans_up_on_checkout_failure(store): # doesn't exist! store.clone('/i_dont_exist_lol', 'fake_sha') except Exception as e: - assert '/i_dont_exist_lol' in five.text(e) + assert '/i_dont_exist_lol' in six.text_type(e) things_starting_with_repo = [ thing for thing in os.listdir(store.directory)