Skip to content

Commit c65a11c

Browse files
committed
Replace five with six
1 parent cb8dd33 commit c65a11c

File tree

9 files changed

+27
-57
lines changed

9 files changed

+27
-57
lines changed

pre_commit/error_handler.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from __future__ import unicode_literals
44

55
import contextlib
6-
import io
76
import os.path
87
import traceback
98

9+
import six
10+
1011
from pre_commit import five
1112
from pre_commit import output
1213
from pre_commit.errors import FatalError
@@ -22,7 +23,7 @@ def _to_bytes(exc):
2223
try:
2324
return bytes(exc)
2425
except Exception:
25-
return five.text(exc).encode('UTF-8')
26+
return six.text_type(exc).encode('UTF-8')
2627

2728

2829
def _log_and_exit(msg, exc, formatted):
@@ -35,7 +36,7 @@ def _log_and_exit(msg, exc, formatted):
3536
output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
3637
store = Store()
3738
store.require_created()
38-
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
39+
with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
3940
output.write(error_msg, stream=log)
4041
output.write_line(formatted, stream=log)
4142
raise PreCommitSystemExit(1)

pre_commit/five.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
11
from __future__ import unicode_literals
22

3-
PY2 = str is bytes
4-
PY3 = str is not bytes
5-
6-
if PY2: # pragma: no cover (PY2 only)
7-
text = unicode # flake8: noqa
8-
string_types = (text, bytes)
9-
10-
def n(s):
11-
if isinstance(s, bytes):
12-
return s
13-
else:
14-
return s.encode('UTF-8')
15-
16-
exec("""def reraise(tp, value, tb=None):
17-
raise tp, value, tb
18-
""")
19-
else: # pragma: no cover (PY3 only)
20-
text = str
21-
string_types = (text,)
22-
23-
def n(s):
24-
if isinstance(s, text):
25-
return s
26-
else:
27-
return s.decode('UTF-8')
28-
29-
def reraise(tp, value, tb=None):
30-
if value is None:
31-
value = tp()
32-
if value.__traceback__ is not tb:
33-
raise value.with_traceback(tb)
34-
raise value
3+
import six
354

365

376
def to_text(s):
38-
return s if isinstance(s, text) else s.decode('UTF-8')
7+
return s if isinstance(s, six.text_type) else s.decode('UTF-8')
398

409

4110
def to_bytes(s):
4211
return s if isinstance(s, bytes) else s.encode('UTF-8')
12+
13+
14+
n = to_bytes if six.PY2 else to_text

pre_commit/make_archives.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os.path
66
import tarfile
77

8-
from pre_commit import five
98
from pre_commit import output
109
from pre_commit.util import cmd_output
1110
from pre_commit.util import cwd
@@ -54,7 +53,7 @@ def make_archive(name, repo, ref, destdir):
5453
# runtime
5554
rmtree(os.path.join(tempdir, '.git'))
5655

57-
with tarfile.open(five.n(output_path), 'w|gz') as tf:
56+
with tarfile.open(output_path, 'w|gz') as tf:
5857
tf.add(tempdir, name)
5958

6059
return output_path

pre_commit/schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99
import sys
1010

11-
from pre_commit import five
11+
import six
1212

1313

1414
class ValidationError(ValueError):
@@ -37,7 +37,7 @@ def validate_context(msg):
3737
yield
3838
except ValidationError as e:
3939
_, _, tb = sys.exc_info()
40-
five.reraise(ValidationError, ValidationError(e, ctx=msg), tb)
40+
six.reraise(ValidationError, ValidationError(e, ctx=msg), tb)
4141

4242

4343
@contextlib.contextmanager
@@ -46,7 +46,7 @@ def reraise_as(tp):
4646
yield
4747
except ValidationError as e:
4848
_, _, tb = sys.exc_info()
49-
five.reraise(tp, tp(e), tb)
49+
six.reraise(tp, tp(e), tb)
5050

5151

5252
def _dct_noop(self, dct):
@@ -218,7 +218,7 @@ def check_type_fn(v):
218218

219219

220220
check_bool = check_type(bool)
221-
check_string = check_type(five.string_types, typename='string')
221+
check_string = check_type(six.string_types, typename='string')
222222

223223

224224
def check_regex(v):

pre_commit/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tempfile
1111

1212
import pkg_resources
13+
import six
1314

1415
from pre_commit import five
1516
from pre_commit import parse_shebang
@@ -143,12 +144,12 @@ def to_bytes(self):
143144
def to_text(self):
144145
return self.to_bytes().decode('UTF-8')
145146

146-
if five.PY3: # pragma: no cover (py3)
147-
__bytes__ = to_bytes
148-
__str__ = to_text
149-
else: # pragma: no cover (py2)
147+
if six.PY2: # pragma: no cover (py2)
150148
__str__ = to_bytes
151149
__unicode__ = to_text
150+
else: # pragma: no cover (py3)
151+
__bytes__ = to_bytes
152+
__str__ = to_text
152153

153154

154155
def cmd_output(*cmd, **kwargs):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'cached-property',
4444
'nodeenv>=0.11.1',
4545
'pyyaml',
46+
'six',
4647
'virtualenv',
4748
],
4849
entry_points={

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import mock
1010
import pytest
11+
import six
1112

1213
import pre_commit.constants as C
13-
from pre_commit import five
1414
from pre_commit import output
1515
from pre_commit.logging_handler import add_logging_handler
1616
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
@@ -29,7 +29,7 @@ def __init__(self):
2929
self.tmpdir_count = 0
3030

3131
def get(self):
32-
path = tmpdir.join(five.text(self.tmpdir_count)).strpath
32+
path = tmpdir.join(six.text_type(self.tmpdir_count)).strpath
3333
self.tmpdir_count += 1
3434
os.mkdir(path)
3535
return path

tests/prefixed_command_runner_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import mock
77
import pytest
88

9-
from pre_commit import five
109
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
1110
from pre_commit.util import CalledProcessError
1211

@@ -17,10 +16,7 @@ def norm_slash(input_tup):
1716

1817
def test_CalledProcessError_str():
1918
error = CalledProcessError(
20-
1,
21-
[five.n('git'), five.n('status')],
22-
0,
23-
(five.n('stdout'), five.n('stderr')),
19+
1, [str('git'), str('status')], 0, (str('stdout'), str('stderr')),
2420
)
2521
assert str(error) == (
2622
"Command: ['git', 'status']\n"
@@ -35,7 +31,7 @@ def test_CalledProcessError_str():
3531

3632
def test_CalledProcessError_str_nooutput():
3733
error = CalledProcessError(
38-
1, [five.n('git'), five.n('status')], 0, (five.n(''), five.n(''))
34+
1, [str('git'), str('status')], 0, (str(''), str(''))
3935
)
4036
assert str(error) == (
4137
"Command: ['git', 'status']\n"
@@ -78,7 +74,7 @@ def test_run_substitutes_prefix(popen_mock, makedirs_mock):
7874
)
7975
ret = instance.run(['{prefix}bar', 'baz'], retcode=None)
8076
popen_mock.assert_called_once_with(
81-
(five.n(os.path.join('prefix', 'bar')), five.n('baz')),
77+
(str(os.path.join('prefix', 'bar')), str('baz')),
8278
env=None,
8379
stdin=subprocess.PIPE,
8480
stdout=subprocess.PIPE,

tests/store_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import mock
99
import pytest
10+
import six
1011

11-
from pre_commit import five
1212
from pre_commit.store import _get_default_directory
1313
from pre_commit.store import Store
1414
from pre_commit.util import cmd_output
@@ -116,7 +116,7 @@ def test_clone_cleans_up_on_checkout_failure(store):
116116
# doesn't exist!
117117
store.clone('/i_dont_exist_lol', 'fake_sha')
118118
except Exception as e:
119-
assert '/i_dont_exist_lol' in five.text(e)
119+
assert '/i_dont_exist_lol' in six.text_type(e)
120120

121121
things_starting_with_repo = [
122122
thing for thing in os.listdir(store.directory)

0 commit comments

Comments
 (0)