'Decode a string, `data`, heuristically.
Raise UnicodeError if unsuccessful.
The client application should call ``locale.setlocale`` at the
beginning of processing::
locale.setlocale(locale.LC_ALL, \'\')'

def decode(self, data):

    if (self.encoding and (self.encoding.lower() == 'unicode')):
        assert isinstance(data, UnicodeType), 'input    encoding    is    "unicode"    but    input    is    not    a    unicode    object'
    if isinstance(data, UnicodeType):
        return data
    if self.encoding:
        encodings = [self.encoding]
    else:
        data_encoding = self.determine_encoding_from_data(data)
        if data_encoding:
            encodings = [data_encoding]
        else:
            encodings = ['utf-8']
            try:
                encodings.append(locale.nl_langinfo(locale.CODESET))
            except:
                pass
            try:
                encodings.append(locale.getlocale()[1])
            except:
                pass
            try:
                encodings.append(locale.getdefaultlocale()[1])
            except:
                pass
            encodings.append('latin-1')
    error = None
    error_details = ''
    for enc in encodings:
        if (not enc):
            continue
        try:
            decoded = unicode(data, enc, self.error_handler)
            self.successful_encoding = enc
            return decoded.replace(u'\ufeff', u'')
        except (UnicodeError, LookupError) as error:
            pass
    if (error is not None):
        error_details = ('\n(%s:    %s)' % (error.__class__.__name__, error))
    raise UnicodeError(('Unable    to    decode    input    data.        Tried    the    following    encodings:    %s.%s' % (',    '.join([repr(enc) for enc in encodings if enc]), error_details)))
