Clean up calls to .encode() / .decode()

This commit is contained in:
Anthony Sottile
2020-01-12 10:46:33 -08:00
parent b2faf339ce
commit aefbe71765
11 changed files with 17 additions and 18 deletions

View File

@@ -16,7 +16,7 @@ class FatalError(RuntimeError):
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:

View File

@@ -2,11 +2,11 @@ from typing import Union
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:
return s if isinstance(s, bytes) else s.encode('UTF-8')
return s if isinstance(s, bytes) else s.encode()
n = to_text

View File

@@ -69,7 +69,7 @@ def is_in_merge_conflict() -> bool:
def parse_merge_msg_for_conflicts(merge_msg: bytes) -> List[str]:
# Conflicted files start with tabs
return [
line.lstrip(b'#').strip().decode('UTF-8')
line.lstrip(b'#').strip().decode()
for line in merge_msg.splitlines()
# '#\t' for git 2.4.1
if line.startswith((b'\t', b'#\t'))

View File

@@ -18,6 +18,6 @@ def run_hook(
file_args: Sequence[str],
color: bool,
) -> Tuple[int, bytes]:
out = hook.entry.encode('UTF-8') + b'\n\n'
out += b'\n'.join(f.encode('UTF-8') for f in file_args) + b'\n'
out = hook.entry.encode() + b'\n\n'
out += b'\n'.join(f.encode() for f in file_args) + b'\n'
return 1, out

View File

@@ -9,7 +9,7 @@ from identify.identify import parse_shebang_from_file
class ExecutableNotFoundError(OSError):
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, ...]:

View File

@@ -39,7 +39,7 @@ def _norm_exe(exe: str) -> Tuple[str, ...]:
if f.read(2) != b'#!':
return ()
try:
first_line = f.readline().decode('UTF-8')
first_line = f.readline().decode()
except UnicodeDecodeError:
return ()
@@ -77,7 +77,7 @@ def _run_legacy() -> Tuple[int, bytes]:
def _validate_config() -> None:
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)
if os.path.isfile(cfg):
pass
@@ -127,7 +127,7 @@ def _pre_push(stdin: bytes) -> Tuple[str, ...]:
remote = sys.argv[1]
opts: Tuple[str, ...] = ()
for line in stdin.decode('UTF-8').splitlines():
for line in stdin.decode().splitlines():
_, local_sha, _, remote_sha = line.split()
if local_sha == Z40:
continue

View File

@@ -109,13 +109,13 @@ class CalledProcessError(RuntimeError):
'return code: {}\n'
'expected return code: {}\n'.format(
self.cmd, self.returncode, self.expected_returncode,
).encode('UTF-8'),
).encode(),
b'stdout:', _indent_or_none(self.stdout), b'\n',
b'stderr:', _indent_or_none(self.stderr),
))
def __str__(self) -> str:
return self.__bytes__().decode('UTF-8')
return self.__bytes__().decode()
def _cmd_kwargs(
@@ -157,8 +157,8 @@ def cmd_output_b(
def cmd_output(*cmd: str, **kwargs: Any) -> Tuple[int, str, Optional[str]]:
returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
stdout = stdout_b.decode('UTF-8') if stdout_b is not None else None
stderr = stderr_b.decode('UTF-8') if stderr_b is not None else None
stdout = stdout_b.decode() if stdout_b is not None else None
stderr = stderr_b.decode() if stderr_b is not None else None
return returncode, stdout, stderr

View File

@@ -49,7 +49,6 @@ def _command_length(*cmd: str) -> int:
# win32 uses the amount of characters, more details at:
# https://github.com/pre-commit/pre-commit/pull/839
if sys.platform == 'win32':
# the python2.x apis require bytes, we encode as UTF-8
return len(full_cmd.encode('utf-16le')) // 2
else:
return len(full_cmd.encode(sys.getfilesystemencoding()))