mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 16:24:40 +04:00
Use bytes for sys.stdout.write in PY2. Closes #161.
This commit is contained in:
parent
37d3dc0c82
commit
ecf82ed5e0
4 changed files with 53 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue