def source_to_unicode(txt, errors='replace', skip_encoding_cookie=True):
 if isinstance(txt, str):
  return txt
 if isinstance(txt, bytes):
  buf = io.BytesIO(txt)
 else:
  buf = txt
 try:
  (encoding, _) = detect_encoding(buf.readline)
 except SyntaxError:
  encoding = 'ascii'
 buf.seek(0)
 text = io.TextIOWrapper(buf, encoding, errors=errors, line_buffering=True)
 text.mode = 'r'
 if skip_encoding_cookie:
  return u''.join(strip_encoding_cookie(text))
 else:
  return text.read()

def source_to_unicode ( txt , errors = 'replace' , skip_encoding_cookie = True ) : if isinstance ( txt , str ) : return txt if isinstance ( txt , bytes ) : buf = io . BytesIO ( txt ) else : buf = txt try : ( encoding , _ ) = detect_encoding ( buf . readline ) except SyntaxError : encoding = 'ascii' buf . seek ( 0 ) text = io . TextIOWrapper ( buf , encoding , errors = errors , line_buffering = True ) text . mode = 'r' if skip_encoding_cookie : return u'' . join ( strip_encoding_cookie ( text ) ) else : return text . read ( )
converts a bytes string with python source code to unicode .