Browse Source

fix: python3 compatibility (#25762)

David Sanders 4 years ago
parent
commit
c075d14920
1 changed files with 10 additions and 9 deletions
  1. 10 9
      script/run-clang-format.py

+ 10 - 9
script/run-clang-format.py

@@ -119,13 +119,14 @@ def run_clang_format_diff(args, file_name):
         raise DiffError(str(exc))
     proc_stdout = proc.stdout
     proc_stderr = proc.stderr
-    if sys.version_info[0] < 3:
-        # make the pipes compatible with Python 3,
-        # reading lines should output unicode
-        encoding = 'utf-8'
-        proc_stdout = codecs.getreader(encoding)(proc_stdout)
-        proc_stderr = codecs.getreader(encoding)(proc_stderr)
-    # hopefully the stderr pipe won't get full and block the process
+    if sys.version_info[0] == 3:
+        proc_stdout = proc_stdout.detach()
+        proc_stderr = proc_stderr.detach()
+    # make the pipes compatible with Python 3,
+    # reading lines should output unicode
+    encoding = 'utf-8'
+    proc_stdout = codecs.getreader(encoding)(proc_stdout)
+    proc_stderr = codecs.getreader(encoding)(proc_stderr)
     outs = list(proc_stdout.readlines())
     errs = list(proc_stderr.readlines())
     proc.wait()
@@ -330,8 +331,8 @@ def main():
                 if not args.quiet:
                     print_diff(outs, use_color=colored_stdout)
                     for line in outs:
-                        patch_file.write(line)
-                    patch_file.write('\n')
+                        patch_file.write(line.encode('utf-8'))
+                    patch_file.write('\n'.encode('utf-8'))
                 if retcode == ExitStatus.SUCCESS:
                     retcode = ExitStatus.DIFF