old_contents: import difflib
  import os
  from popen2 import Popen3
  import tempfile
  
  def patch(diff, file, filename):
      """Apply a diff to a file.  Delegates out to `patch` because noone
         except Larry Wall knows how to patch."""
      (fd, oldfile) = tempfile.mkstemp()
      f = os.fdopen(fd, "w+b")
      f.write(file)
      f.close()
  
      newfile = '%s-new' % oldfile
      p = Popen3('patch -o %s %s' % (newfile, oldfile))
      p.tochild.write(diff)
      p.tochild.close()
      failure = p.wait()
  
      if failure:
          os.unlink(oldfile)
          os.unlink(newfile)
  
          try:
              os.unlink(newfile + ".rej")
          except:
              pass
  
          raise Exception(("The patch to '%s' didn't apply cleanly. " +
                           "`patch` returned: %s") %
                          (filename, p.fromchild.read()))
  
      f = open(newfile, "r")
      data = f.read()
      f.close()
  
      os.unlink(oldfile)
      os.unlink(newfile)
  
      return data
  
  
  def get_line_changed_regions(oldline, newline):
      if oldline is None or newline is None:
          return [None, None]
  
      s = difflib.SequenceMatcher(None, oldline, newline)
      oldchanges = []
      newchanges = []
      back = (0, 0)
  
      opcodes = s.get_opcodes()
      if s.ratio() < 0.5:
          return (None, None)
  
      for tag, i1, i2, j1, j2 in opcodes:
          if tag == "equal":
              if (i2 - i1 < 3) or (j2 - j1 < 3):
                  back = (j2 - j1, i2 - i1)
              continue
  
          oldstart, oldend = i1 - back[0], i2
          newstart, newend = j1 - back[1], j2
  
          if oldchanges != [] and oldstart <= oldchanges[-1][1] < oldend:
              oldchanges[-1] = (oldchanges[-1][0], oldend)
          elif not oldline[oldstart:oldend].isspace():
              oldchanges.append((oldstart, oldend))
  
          if newchanges != [] and newstart <= newchanges[-1][1] < newend:
              newchanges[-1] = (newchanges[-1][0], newend)
          elif not newline[newstart:newend].isspace():
              newchanges.append((newstart, newend))
  
          back = (0, 0)
  
      return (oldchanges, newchanges)
  
new_contents: import difflib
  import os
  from popen2 import Popen3
  import tempfile
  
  def patch(diff, file, filename):
      """Apply a diff to a file.  Delegates out to `patch` because noone
         except Larry Wall knows how to patch."""
      (fd, oldfile) = tempfile.mkstemp()
      f = os.fdopen(fd, "w+b")
      f.write(file)
      f.close()
  
      newfile = '%s-new' % oldfile
      p = Popen3('patch -o %s %s' % (newfile, oldfile))
      p.tochild.write(diff)
      p.tochild.close()
      failure = p.wait()
  
      if failure:
          os.unlink(oldfile)
          os.unlink(newfile)
  
          try:
              os.unlink(newfile + ".rej")
          except:
              pass
  
          raise Exception(("The patch to '%s' didn't apply cleanly. " +
                           "`patch` returned: %s") %
                          (filename, p.fromchild.read()))
  
      f = open(newfile, "r")
      data = f.read()
      f.close()
  
      os.unlink(oldfile)
      os.unlink(newfile)
  
      return data
  
  
  def get_line_changed_regions(oldline, newline):
      if oldline is None or newline is None:
          return [None, None]
  
      s = difflib.SequenceMatcher(None, oldline, newline)
      oldchanges = []
      newchanges = []
      back = (0, 0)
  
      # This thresholds our results -- we don't want to show inter-line diffs if
      # most of the line has changed.
      opcodes = s.get_opcodes()
      if s.ratio() < 0.6:
          return (None, None)
  
      for tag, i1, i2, j1, j2 in opcodes:
          if tag == "equal":
              if (i2 - i1 < 3) or (j2 - j1 < 3):
                  back = (j2 - j1, i2 - i1)
              continue
  
          oldstart, oldend = i1 - back[0], i2
          newstart, newend = j1 - back[1], j2
  
          if oldchanges != [] and oldstart <= oldchanges[-1][1] < oldend:
              oldchanges[-1] = (oldchanges[-1][0], oldend)
          elif not oldline[oldstart:oldend].isspace():
              oldchanges.append((oldstart, oldend))
  
          if newchanges != [] and newstart <= newchanges[-1][1] < newend:
              newchanges[-1] = (newchanges[-1][0], newend)
          elif not newline[newstart:newend].isspace():
              newchanges.append((newstart, newend))
  
          back = (0, 0)
  
      return (oldchanges, newchanges)
  
subject: Add a comment explaining the thresholding, and frob the ratio to 0.6