Skip to content

Commit 40304af

Browse files
committedMay 25, 2017
Make git-clang-format python 3 compatible
Summary: This patch attempts to make `git-clang-format` both python2 and python3 compatible. Currently it only works in python2. Reviewers: modocache, compnerd, djasper, jbcoe, srhines, ddunbar Reviewed By: jbcoe Subscribers: kimgr, mgorny, llvm-commits, cfe-commits Differential Revision: https://reviews.llvm.org/D30773 llvm-svn: 303871
1 parent 315eafc commit 40304af

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed
 

‎clang/tools/clang-format/git-clang-format

+30-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ clang-format on the changes in current files or a specific commit.
2020
For further details, run:
2121
git clang-format -h
2222
23-
Requires Python 2.7
23+
Requires Python 2.7 or Python 3
2424
"""
2525

2626
from __future__ import print_function
@@ -258,7 +258,7 @@ def get_object_type(value):
258258
stdout, stderr = p.communicate()
259259
if p.returncode != 0:
260260
return None
261-
return stdout.strip()
261+
return convert_string(stdout.strip())
262262

263263

264264
def compute_diff_and_extract_lines(commits, files):
@@ -301,6 +301,7 @@ def extract_lines(patch_file):
301301
list of line `Range`s."""
302302
matches = {}
303303
for line in patch_file:
304+
line = convert_string(line)
304305
match = re.search(r'^\+\+\+\ [^/]+/(.*)', line)
305306
if match:
306307
filename = match.group(1).rstrip('\r\n')
@@ -385,7 +386,7 @@ def create_tree(input_lines, mode):
385386
with temporary_index_file():
386387
p = subprocess.Popen(cmd, stdin=subprocess.PIPE)
387388
for line in input_lines:
388-
p.stdin.write('%s\0' % line)
389+
p.stdin.write(to_bytes('%s\0' % line))
389390
p.stdin.close()
390391
if p.wait() != 0:
391392
die('`%s` failed' % ' '.join(cmd))
@@ -440,7 +441,7 @@ def clang_format_to_blob(filename, line_ranges, revision=None,
440441
die('`%s` failed' % ' '.join(clang_format_cmd))
441442
if git_show and git_show.wait() != 0:
442443
die('`%s` failed' % ' '.join(git_show_cmd))
443-
return stdout.rstrip('\r\n')
444+
return convert_string(stdout).rstrip('\r\n')
444445

445446

446447
@contextlib.contextmanager
@@ -527,6 +528,10 @@ def run(*args, **kwargs):
527528
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
528529
stdin=subprocess.PIPE)
529530
stdout, stderr = p.communicate(input=stdin)
531+
532+
stdout = convert_string(stdout)
533+
stderr = convert_string(stderr)
534+
530535
if p.returncode == 0:
531536
if stderr:
532537
if verbose:
@@ -547,5 +552,26 @@ def die(message):
547552
sys.exit(2)
548553

549554

555+
def to_bytes(str_input):
556+
# Encode to UTF-8 to get binary data.
557+
if isinstance(str_input, bytes):
558+
return str_input
559+
return str_input.encode('utf-8')
560+
561+
562+
def to_string(bytes_input):
563+
if isinstance(bytes_input, str):
564+
return bytes_input
565+
return bytes_input.encode('utf-8')
566+
567+
568+
def convert_string(bytes_input):
569+
try:
570+
return to_string(bytes_input.decode('utf-8'))
571+
except AttributeError: # 'str' object has no attribute 'decode'.
572+
return str(bytes_input)
573+
except UnicodeError:
574+
return str(bytes_input)
575+
550576
if __name__ == '__main__':
551577
main()

0 commit comments

Comments
 (0)