Convert a string (bytestring in `encoding` or unicode), to unicode.
968088
def any2unicode(text, encoding='utf8', errors='strict'):
    """"""
    if isinstance(text, unicode):
        return text
    return unicode(text, encoding, errors=errors)
486475
def convert(self, string, preprocess = None):
        
        string = unicode(preprocess(string) if preprocess else string, encoding="utf-8")
        if self.regex:
            return self.regex.sub(lambda x: self.substitutes[x.group()], string).encode('utf-8')
        else:
            return string
1259522
def to_str(s):
    
    if isinstance(s, bytes):
        s = s.decode('utf-8')
    elif not isinstance(s, str):
        s = str(s)
    return s
542112
def serialiseString(self, s):
        
        if type(s) is unicode:
            s = self.context.getBytesForString(s)

        self.serialiseBytes(s)
221376
def as_text(str_or_bytes, encoding='utf-8', errors='strict'):
    
    if isinstance(str_or_bytes, text):
        return str_or_bytes
    return str_or_bytes.decode(encoding, errors)
193456
def make_unicode(s, encoding='utf-8', encoding_errors='strict'):
    
    if not isinstance(s, unicode):
        if not isinstance(s, basestring):
            return unicode(str(s), encoding, encoding_errors)
        return unicode(s, encoding, encoding_errors)
    return s
598135
def to_unicode(text):
    
    if not isinstance(text, str):
        tmpl = 'expected unicode string, got %s value %s'
        raise TypeError(tmpl % (type(text), text))
    return text
1113636
def to_bytes(s):  # pragma: nocover
    
    if isinstance(s, bytes):
        return s
    if isinstance(s, str) or is_unicode(s):
        return s.encode("utf-8")
    try:
        return unicode(s).encode("utf-8")
    except NameError:
        return str(s).encode("utf-8")
685497
def convert_to_utf8(string):
    '''  '''
    if (isinstance(string, unicode)):
        return string.encode('utf-8')
    try:
        u = unicode(string, 'utf-8')
    except TypeError:
        return str(string)
    utf8 = u.encode('utf-8')
    return utf8
650264
def encoded(string, encoding='utf-8'):
    
    assert isinstance(string, string_types) or isinstance(string, binary_type)
    if isinstance(string, text_type):
        return string.encode(encoding)
    try:
        # make sure the string can be decoded in the specified encoding ...
        string.decode(encoding)
        return string
    except UnicodeDecodeError:
        # ... if not use latin1 as best guess to decode the string before encoding as
        # specified.
        return string.decode('latin1').encode(encoding)
669069
def to_unicode(value: Union[None, str, bytes]) -> Optional[str]:  # noqa: F811
    
    if isinstance(value, _TO_UNICODE_TYPES):
        return value
    if not isinstance(value, bytes):
        raise TypeError("Expected bytes, unicode, or None; got %r" % type(value))
    return value.decode("utf-8")
1034623
def to_unicode(s, encoding=None, errors='strict'):
    
    encoding = encoding or 'utf-8'

    if is_unicode(s):
        return s
    elif is_strlike(s):
        return s.decode(encoding, errors)
    else:
        if six.PY2:
            return str(s).decode(encoding, errors)
        else:
            return str(s)
679048
def unicodestr(s, encoding='utf-8', fallback='iso-8859-1'):
    """"""

    if isinstance(s, unicode):
        return s

    try:
        return s.decode(encoding)
    except UnicodeError:
        return s.decode(fallback)
799804
def str_or_unicode(text):
    
    encoding = sys.stdout.encoding
    if sys.version_info > (3, 0):
        return text.encode(encoding).decode(encoding)
    return text.encode(encoding)
1192617
def _tostring(value):
        ''''''
        if value is True:
            value = 'true'
        elif value is False:
            value = 'false'
        elif value is None:
            value = ''
        return unicode(value)
1064188
def ensure_unicode_string(value):
    """"""
    if not isinstance(value, six.string_types):
        raise TypeError(u'Expected string value, got: {}'.format(value))
    return six.text_type(value)
941126
def _encode_string(string):
    """"""
    if not isinstance(string, bytes):
        string = string.encode('utf8')
    return ffi.new('char[]', string)