code: def open_compat(*args, **kwargs):
      
      if six.PY3:
          return open(*args, **kwargs)
  
      else:
          f = io.open(*args, **kwargs)
  
          if f.encoding and f.encoding.lower().replace("-", "") == "utf8":
              write_orig = f.write
  
              def write(data, *args, **kwargs):
                  u = unicode  # noqa: F821
                  if not isinstance(data, u):
                      data = u(data)
                  return write_orig(data, *args, **kwargs)
  
              f.write = write
  
          return f
docs: Polyfill for python's ``open`` factory, returning the plain ``open`` in python 3, and
      ``io.open`` in python 2 with a patched ``write`` method that internally handles unicode
      conversion of its first argument. All *args* and *kwargs* are forwarded.