def auto_decode(data):<EOL>
for bom, encoding in BOMS:<EOL><INDENT>if data.startswith(bom):<EOL><INDENT>return data[len(bom):].decode(encoding)<EOL><DEDENT><DEDENT>for line in data.split(b'<STR_LIT:\n>')[:<NUM_LIT:2>]:<EOL><INDENT>if line[<NUM_LIT:0>:<NUM_LIT:1>] == b'<STR_LIT:#>' and ENCODING_RE.search(line):<EOL><INDENT>encoding = ENCODING_RE.search(line).groups()[<NUM_LIT:0>].decode('<STR_LIT:ascii>')<EOL>return data.decode(encoding)<EOL><DEDENT><DEDENT>return data.decode(<EOL>locale.getpreferredencoding(False) or sys.getdefaultencoding(),<EOL>)<EOL>
Check a bytes string for a BOM to correctly detect the encoding

    Fallback to locale.getpreferredencoding(False) like open() on Python3