@@ -20,7 +20,7 @@ clang-format on the changes in current files or a specific commit.
20
20
For further details, run:
21
21
git clang-format -h
22
22
23
- Requires Python 2.7
23
+ Requires Python 2.7 or Python 3
24
24
"""
25
25
26
26
from __future__ import print_function
@@ -258,7 +258,7 @@ def get_object_type(value):
258
258
stdout , stderr = p .communicate ()
259
259
if p .returncode != 0 :
260
260
return None
261
- return stdout .strip ()
261
+ return convert_string ( stdout .strip () )
262
262
263
263
264
264
def compute_diff_and_extract_lines (commits , files ):
@@ -301,6 +301,7 @@ def extract_lines(patch_file):
301
301
list of line `Range`s."""
302
302
matches = {}
303
303
for line in patch_file :
304
+ line = convert_string (line )
304
305
match = re .search (r'^\+\+\+\ [^/]+/(.*)' , line )
305
306
if match :
306
307
filename = match .group (1 ).rstrip ('\r \n ' )
@@ -385,7 +386,7 @@ def create_tree(input_lines, mode):
385
386
with temporary_index_file ():
386
387
p = subprocess .Popen (cmd , stdin = subprocess .PIPE )
387
388
for line in input_lines :
388
- p .stdin .write ('%s\0 ' % line )
389
+ p .stdin .write (to_bytes ( '%s\0 ' % line ) )
389
390
p .stdin .close ()
390
391
if p .wait () != 0 :
391
392
die ('`%s` failed' % ' ' .join (cmd ))
@@ -440,7 +441,7 @@ def clang_format_to_blob(filename, line_ranges, revision=None,
440
441
die ('`%s` failed' % ' ' .join (clang_format_cmd ))
441
442
if git_show and git_show .wait () != 0 :
442
443
die ('`%s` failed' % ' ' .join (git_show_cmd ))
443
- return stdout .rstrip ('\r \n ' )
444
+ return convert_string ( stdout ) .rstrip ('\r \n ' )
444
445
445
446
446
447
@contextlib .contextmanager
@@ -527,6 +528,10 @@ def run(*args, **kwargs):
527
528
p = subprocess .Popen (args , stdout = subprocess .PIPE , stderr = subprocess .PIPE ,
528
529
stdin = subprocess .PIPE )
529
530
stdout , stderr = p .communicate (input = stdin )
531
+
532
+ stdout = convert_string (stdout )
533
+ stderr = convert_string (stderr )
534
+
530
535
if p .returncode == 0 :
531
536
if stderr :
532
537
if verbose :
@@ -547,5 +552,26 @@ def die(message):
547
552
sys .exit (2 )
548
553
549
554
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
+
550
576
if __name__ == '__main__' :
551
577
main ()
0 commit comments