mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Clean up calls to .encode() / .decode()
This commit is contained in:
parent
b2faf339ce
commit
aefbe71765
11 changed files with 17 additions and 18 deletions
|
|
@ -16,7 +16,7 @@ class FatalError(RuntimeError):
|
||||||
|
|
||||||
|
|
||||||
def _to_bytes(exc: BaseException) -> bytes:
|
def _to_bytes(exc: BaseException) -> bytes:
|
||||||
return str(exc).encode('UTF-8')
|
return str(exc).encode()
|
||||||
|
|
||||||
|
|
||||||
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ from typing import Union
|
||||||
|
|
||||||
|
|
||||||
def to_text(s: Union[str, bytes]) -> str:
|
def to_text(s: Union[str, bytes]) -> str:
|
||||||
return s if isinstance(s, str) else s.decode('UTF-8')
|
return s if isinstance(s, str) else s.decode()
|
||||||
|
|
||||||
|
|
||||||
def to_bytes(s: Union[str, bytes]) -> bytes:
|
def to_bytes(s: Union[str, bytes]) -> bytes:
|
||||||
return s if isinstance(s, bytes) else s.encode('UTF-8')
|
return s if isinstance(s, bytes) else s.encode()
|
||||||
|
|
||||||
|
|
||||||
n = to_text
|
n = to_text
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@ def is_in_merge_conflict() -> bool:
|
||||||
def parse_merge_msg_for_conflicts(merge_msg: bytes) -> List[str]:
|
def parse_merge_msg_for_conflicts(merge_msg: bytes) -> List[str]:
|
||||||
# Conflicted files start with tabs
|
# Conflicted files start with tabs
|
||||||
return [
|
return [
|
||||||
line.lstrip(b'#').strip().decode('UTF-8')
|
line.lstrip(b'#').strip().decode()
|
||||||
for line in merge_msg.splitlines()
|
for line in merge_msg.splitlines()
|
||||||
# '#\t' for git 2.4.1
|
# '#\t' for git 2.4.1
|
||||||
if line.startswith((b'\t', b'#\t'))
|
if line.startswith((b'\t', b'#\t'))
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,6 @@ def run_hook(
|
||||||
file_args: Sequence[str],
|
file_args: Sequence[str],
|
||||||
color: bool,
|
color: bool,
|
||||||
) -> Tuple[int, bytes]:
|
) -> Tuple[int, bytes]:
|
||||||
out = hook.entry.encode('UTF-8') + b'\n\n'
|
out = hook.entry.encode() + b'\n\n'
|
||||||
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
|
out += b'\n'.join(f.encode() for f in file_args) + b'\n'
|
||||||
return 1, out
|
return 1, out
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ from identify.identify import parse_shebang_from_file
|
||||||
|
|
||||||
class ExecutableNotFoundError(OSError):
|
class ExecutableNotFoundError(OSError):
|
||||||
def to_output(self) -> Tuple[int, bytes, None]:
|
def to_output(self) -> Tuple[int, bytes, None]:
|
||||||
return (1, self.args[0].encode('UTF-8'), None)
|
return (1, self.args[0].encode(), None)
|
||||||
|
|
||||||
|
|
||||||
def parse_filename(filename: str) -> Tuple[str, ...]:
|
def parse_filename(filename: str) -> Tuple[str, ...]:
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ def _norm_exe(exe: str) -> Tuple[str, ...]:
|
||||||
if f.read(2) != b'#!':
|
if f.read(2) != b'#!':
|
||||||
return ()
|
return ()
|
||||||
try:
|
try:
|
||||||
first_line = f.readline().decode('UTF-8')
|
first_line = f.readline().decode()
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ def _run_legacy() -> Tuple[int, bytes]:
|
||||||
|
|
||||||
def _validate_config() -> None:
|
def _validate_config() -> None:
|
||||||
cmd = ('git', 'rev-parse', '--show-toplevel')
|
cmd = ('git', 'rev-parse', '--show-toplevel')
|
||||||
top_level = subprocess.check_output(cmd).decode('UTF-8').strip()
|
top_level = subprocess.check_output(cmd).decode().strip()
|
||||||
cfg = os.path.join(top_level, CONFIG)
|
cfg = os.path.join(top_level, CONFIG)
|
||||||
if os.path.isfile(cfg):
|
if os.path.isfile(cfg):
|
||||||
pass
|
pass
|
||||||
|
|
@ -127,7 +127,7 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
|
||||||
remote = sys.argv[1]
|
remote = sys.argv[1]
|
||||||
|
|
||||||
opts: Tuple[str, ...] = ()
|
opts: Tuple[str, ...] = ()
|
||||||
for line in stdin.decode('UTF-8').splitlines():
|
for line in stdin.decode().splitlines():
|
||||||
_, local_sha, _, remote_sha = line.split()
|
_, local_sha, _, remote_sha = line.split()
|
||||||
if local_sha == Z40:
|
if local_sha == Z40:
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -109,13 +109,13 @@ class CalledProcessError(RuntimeError):
|
||||||
'return code: {}\n'
|
'return code: {}\n'
|
||||||
'expected return code: {}\n'.format(
|
'expected return code: {}\n'.format(
|
||||||
self.cmd, self.returncode, self.expected_returncode,
|
self.cmd, self.returncode, self.expected_returncode,
|
||||||
).encode('UTF-8'),
|
).encode(),
|
||||||
b'stdout:', _indent_or_none(self.stdout), b'\n',
|
b'stdout:', _indent_or_none(self.stdout), b'\n',
|
||||||
b'stderr:', _indent_or_none(self.stderr),
|
b'stderr:', _indent_or_none(self.stderr),
|
||||||
))
|
))
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.__bytes__().decode('UTF-8')
|
return self.__bytes__().decode()
|
||||||
|
|
||||||
|
|
||||||
def _cmd_kwargs(
|
def _cmd_kwargs(
|
||||||
|
|
@ -157,8 +157,8 @@ def cmd_output_b(
|
||||||
|
|
||||||
def cmd_output(*cmd: str, **kwargs: Any) -> Tuple[int, str, Optional[str]]:
|
def cmd_output(*cmd: str, **kwargs: Any) -> Tuple[int, str, Optional[str]]:
|
||||||
returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
|
returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
|
||||||
stdout = stdout_b.decode('UTF-8') if stdout_b is not None else None
|
stdout = stdout_b.decode() if stdout_b is not None else None
|
||||||
stderr = stderr_b.decode('UTF-8') if stderr_b is not None else None
|
stderr = stderr_b.decode() if stderr_b is not None else None
|
||||||
return returncode, stdout, stderr
|
return returncode, stdout, stderr
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,6 @@ def _command_length(*cmd: str) -> int:
|
||||||
# win32 uses the amount of characters, more details at:
|
# win32 uses the amount of characters, more details at:
|
||||||
# https://github.com/pre-commit/pre-commit/pull/839
|
# https://github.com/pre-commit/pre-commit/pull/839
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
# the python2.x apis require bytes, we encode as UTF-8
|
|
||||||
return len(full_cmd.encode('utf-16le')) // 2
|
return len(full_cmd.encode('utf-16le')) // 2
|
||||||
else:
|
else:
|
||||||
return len(full_cmd.encode(sys.getfilesystemencoding()))
|
return len(full_cmd.encode(sys.getfilesystemencoding()))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Intentionally write mixed encoding to the output. This should not crash
|
# Intentionally write mixed encoding to the output. This should not crash
|
||||||
# pre-commit and should write bytes to the output.
|
# pre-commit and should write bytes to the output.
|
||||||
# '☃'.encode('UTF-8') + '²'.encode('latin1')
|
# '☃'.encode() + '²'.encode('latin1')
|
||||||
echo -e '\xe2\x98\x83\xb2'
|
echo -e '\xe2\x98\x83\xb2'
|
||||||
# exit 1 to trigger printing
|
# exit 1 to trigger printing
|
||||||
exit 1
|
exit 1
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ class Fixture:
|
||||||
|
|
||||||
def get(self):
|
def get(self):
|
||||||
"""Get the output assuming it was written as UTF-8 bytes"""
|
"""Get the output assuming it was written as UTF-8 bytes"""
|
||||||
return self.get_bytes().decode('UTF-8')
|
return self.get_bytes().decode()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ def test_file_doesnt_exist():
|
||||||
|
|
||||||
def test_simple_case(tmpdir):
|
def test_simple_case(tmpdir):
|
||||||
x = tmpdir.join('f')
|
x = tmpdir.join('f')
|
||||||
x.write_text('#!/usr/bin/env echo', encoding='UTF-8')
|
x.write('#!/usr/bin/env echo')
|
||||||
make_executable(x.strpath)
|
make_executable(x.strpath)
|
||||||
assert parse_shebang.parse_filename(x.strpath) == ('echo',)
|
assert parse_shebang.parse_filename(x.strpath) == ('echo',)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue