def auto_decode(data):
 for (bom, encoding) in BOMS:
  if data.startswith(bom):
   return data[len(bom):].decode(encoding)
 for line in data.split('\n')[:2]:
  if ((line[0:1] == '#') and ENCODING_RE.search(line)):
   encoding = ENCODING_RE.search(line).groups()[0].decode('ascii')
   return data.decode(encoding)
 return data.decode(locale.getpreferredencoding(False))

def auto_decode ( data ) : for ( bom , encoding ) in BOMS : if data . startswith ( bom ) : return data [ len ( bom ) : ] . decode ( encoding ) for line in data . split ( '\n' ) [ : 2 ] : if ( ( line [ 0 : 1 ] == '#' ) and ENCODING_RE . search ( line ) ) : encoding = ENCODING_RE . search ( line ) . groups ( ) [ 0 ] . decode ( 'ascii' ) return data . decode ( encoding ) return data . decode ( locale . getpreferredencoding ( False ) )
check a bytes string for a bom to correctly detect the encoding fallback to locale .