def ensure_unicode(s, encoding=None, confidence=None):
    'Convert/decode bytestring to unicode.\n\n    If `s` isn\'t a bytestring, return it as is.\n\n    Parameters\n    ----------\n    encoding: str, optional\n      Encoding to use.  If None, "utf-8" is tried, and then if not a valid\n      UTF-8, encoding will be guessed\n    confidence: float, optional\n      A value between 0 and 1, so if guessing of encoding is of lower than\n      specified confidence, ValueError is raised\n    '
    if (not isinstance(s, bytes)):
        return s
    if (encoding is None):
        try:
            return s.decode('utf-8')
        except UnicodeDecodeError as exc:
            lgr.debug('Failed to decode a string as utf-8: %s', CapturedException(exc))
        from chardet import detect
        enc = detect(s)
        denc = enc.get('encoding', None)
        if denc:
            denc_confidence = enc.get('confidence', 0)
            if ((confidence is not None) and (denc_confidence < confidence)):
                raise ValueError(('Failed to auto-detect encoding with high enough confidence. Highest confidence was %s for %s' % (denc_confidence, denc)))
            lgr.log(5, 'Auto-detected encoding to be %s', denc)
            return s.decode(denc)
        else:
            raise ValueError(('Could not decode value as utf-8, or to guess its encoding: %s' % repr(s)))
    else:
        return s.decode(encoding)
Convert/decode bytestring to unicode.

If `s` isn't a bytestring, return it as is.

Parameters
----------
encoding: str, optional
  Encoding to use.  If None, "utf-8" is tried, and then if not a valid
  UTF-8, encoding will be guessed
confidence: float, optional
  A value between 0 and 1, so if guessing of encoding is of lower than
  specified confidence, ValueError is raised
def ensure_unicode(s, encoding=None, confidence=None):
    'Convert/decode bytestring to unicode.\n\n    If `s` isn\'t a bytestring, return it as is.\n\n    Parameters\n    ----------\n    encoding: str, optional\n      Encoding to use.  If None, "utf-8" is tried, and then if not a valid\n      UTF-8, encoding will be guessed\n    confidence: float, optional\n      A value between 0 and 1, so if guessing of encoding is of lower than\n      specified confidence, ValueError is raised\n    '
    if (not isinstance(s, bytes)):
        return s
    if (encoding is None):
        try:
            return s.decode('utf-8')
        except UnicodeDecodeError as exc:
            lgr.debug('Failed to decode a string as utf-8: %s', CapturedException(exc))
        from chardet import detect
        enc = detect(s)
        denc = enc.get('encoding', None)
        if denc:
            denc_confidence = enc.get('confidence', 0)
            if ((confidence is not None) and (denc_confidence < confidence)):
                raise ValueError(('Failed to auto-detect encoding with high enough confidence. Highest confidence was %s for %s' % (denc_confidence, denc)))
            lgr.log(5, 'Auto-detected encoding to be %s', denc)
            return s.decode(denc)
        else:
            raise ValueError(('Could not decode value as utf-8, or to guess its encoding: %s' % repr(s)))
    else:
        return s.decode(encoding)