[PATCH] D43165: [lit] Fix problem in how Python versions open files with different encodings

Matthias Braun via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 22:08:51 PST 2018


MatzeB added a comment.

Reading python docu and playing a bit with a prototype I believe this is the way to do things:

  import sys
  import difflib
  f = open(sys.argv[1], 'rb').readlines()
  f2 = open(sys.argv[2], 'rb').readlines()
  if hasattr(difflib, 'diff_bytes'):
      # python 3.5 or newer
      gen = difflib.diff_bytes(difflib.unified_diff, f, f2)
      sys.stdout.buffer.writelines(gen)
  else:
      # python 2.7
      gen = difflib.unified_diff(f, f2)
      sys.stdout.writelines(gen)

(python 3.0-3.4 difflib before diff_bytes appears to be broken indeed for inconsistent/invalid encoded inputs, but I hope we can just ignore old 3.x versions...)


Repository:
  rL LLVM

https://reviews.llvm.org/D43165





More information about the llvm-commits mailing list