'Automatically decode :param:`data` into a `unicode` object.'

def decode(self, data):

    def fix_data(d):
        return d.replace('\r\n', '\n').replace('\r', '\n')
    if isinstance(data, unicode):
        return fix_data(data)
    bom_enc = None
    if (data[:4] in ('\x00\x00\xfe\xff', '\xff\xfe\x00\x00')):
        bom_enc = {'\x00\x00\xfe\xff': 'utf-32-be', '\xff\xfe\x00\x00': 'utf-32-le'}[data[:4]]
        data = data[4:]
    elif (data[:2] in ('\xff\xfe', '\xfe\xff')):
        bom_enc = {'\xff\xfe': 'utf-16-le', '\xfe\xff': 'utf-16-be'}[data[:2]]
        data = data[2:]
    elif (data[:3] == '\xef\xbb\xbf'):
        bom_enc = 'utf-8'
        data = data[3:]
    if (bom_enc is not None):
        try:
            return fix_data(data.decode(bom_enc))
        except UnicodeDecodeError:
            pass
    if self.input_encoding:
        try:
            return fix_data(data.decode(self.input_encoding, 'replace'))
        except UnicodeDecodeError:
            pass
    try:
        return fix_data(data.decode('utf-8'))
    except UnicodeDecodeError:
        pass
    (data, _) = xml_to_unicode(data)
    return fix_data(data)
