Don't UnicodeDecodeError on non-ascii not-found hooks. Resolves #207.

This commit is contained in:
Anthony Sottile 2015-05-21 07:49:22 -07:00
parent 20c546a7da
commit 7905594215
5 changed files with 35 additions and 12 deletions

View file

@ -20,3 +20,11 @@ else: # pragma: no cover (PY3 only)
return s
else:
return s.decode('UTF-8')
def to_text(s):
return s if isinstance(s, text) else s.decode('UTF-8')
def to_bytes(s):
return s if isinstance(s, bytes) else s.encode('UTF-8')

View file

@ -7,6 +7,7 @@ import sys
import pkg_resources
from pre_commit import color
from pre_commit import five
from pre_commit.commands.autoupdate import autoupdate
from pre_commit.commands.clean import clean
from pre_commit.commands.install_uninstall import install
@ -25,6 +26,7 @@ os.environ.pop('__PYVENV_LAUNCHER__', None)
def main(argv=None):
argv = argv if argv is not None else sys.argv[1:]
argv = [five.to_text(arg) for arg in argv]
parser = argparse.ArgumentParser()
# http://stackoverflow.com/a/8521644/812183

View file

@ -77,12 +77,8 @@ def get_hook_message(
)
def sys_stdout_write_wrapper(s, stream=sys.stdout):
"""Python 2.6 chokes on unicode being passed to sys.stdout.write.
stdout_byte_stream = getattr(sys.stdout, 'buffer', sys.stdout)
This is an adapter because PY2 is ok with bytes and PY3 requires text.
"""
assert type(s) is five.text
if five.PY2: # pragma: no cover (PY2)
s = s.encode('UTF-8')
stream.write(s)
def sys_stdout_write_wrapper(s, stream=stdout_byte_stream):
stream.write(five.to_bytes(s))