mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Use bytes for sys.stdout.write in PY2. Closes #161.
This commit is contained in:
@@ -9,6 +9,7 @@ from pre_commit import git
|
||||
from pre_commit import color
|
||||
from pre_commit.logging_handler import LoggingHandler
|
||||
from pre_commit.output import get_hook_message
|
||||
from pre_commit.output import sys_stdout_write_wrapper
|
||||
from pre_commit.staged_files_only import staged_files_only
|
||||
from pre_commit.util import noop_context
|
||||
|
||||
@@ -125,7 +126,7 @@ def _has_unmerged_paths(runner):
|
||||
return bool(stdout.strip())
|
||||
|
||||
|
||||
def run(runner, args, write=sys.stdout.write, environ=os.environ):
|
||||
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
|
||||
# Set up our logging handler
|
||||
logger.addHandler(LoggingHandler(args.color, write=write))
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from pre_commit import color
|
||||
from pre_commit import five
|
||||
|
||||
|
||||
# TODO: smell: import side-effects
|
||||
@@ -70,3 +72,14 @@ def get_hook_message(
|
||||
postfix,
|
||||
color.format_color(end_msg, end_color, use_color),
|
||||
)
|
||||
|
||||
|
||||
def sys_stdout_write_wrapper(s, stream=sys.stdout):
|
||||
"""Python 2.6 chokes on unicode being passed to sys.stdout.write.
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user