git-hyper-blame: Fix unicode handling.

git hyper_blame might use a subprocess' stdin for its stdout,
which is opened to accept byte input.
The text must be encoded before printing to stdout to avoid
unicode errors.

Bug: 1028709
Change-Id: If2a270a7f3f69a818d367616f6732245de364db9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1937500
Reviewed-by: Andy Perelson <ajp@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
This commit is contained in:
Edward Lemur
2019-11-26 21:44:08 +00:00
committed by Commit Bot
parent 5604ce9f20
commit 5e94b80c34
3 changed files with 32 additions and 28 deletions

View File

@@ -95,7 +95,7 @@ def parse_blame(blameoutput):
yield BlameLine(commit, context, lineno_then, lineno_now, False)
def print_table(table, colsep=' ', rowsep='\n', align=None, out=sys.stdout):
def print_table(table, align=None, out=sys.stdout):
"""Print a 2D rectangular array, aligning columns with spaces.
Args:
@@ -124,9 +124,10 @@ def print_table(table, colsep=' ', rowsep='\n', align=None, out=sys.stdout):
elif i < len(row) - 1:
# Do not pad the final column if left-aligned.
cell += padding
cell = cell.encode('utf-8', 'replace')
cells.append(cell)
try:
print(*cells, sep=colsep, end=rowsep, file=out)
out.write(b' '.join(cells) + b'\n')
except IOError: # pragma: no cover
# Can happen on Windows if the pipe is closed early.
pass