Takes item from argv and returns ascii native str
    or raises ValueError.
326132
def frame_from_fsnative(arg):
    

    assert isinstance(arg, fsnative)

    text = fsn2text(arg, strict=True)
    if PY2:
        return text.encode("ascii")
    else:
        return text.encode("ascii").decode("ascii")
300870
def str_if_not_none(value):
    
    if not(value is None or isinstance(value, string_types)):
        value = str(value)

    return value
1090191
def to_str(s, encoding=None, errors='strict', normalize=False):
    '''
    
    '''
    def _normalize(s):
        try:
            return unicodedata.normalize('NFC', s) if normalize else s
        except TypeError:
            return s

    if encoding is None:
        # Try utf-8 first, and fall back to detected encoding
        encoding = ('utf-8', __salt_system_encoding__)
    if not isinstance(encoding, (tuple, list)):
        encoding = (encoding,)

    if not encoding:
        raise ValueError('encoding cannot be empty')

    # This shouldn't be six.string_types because if we're on PY2 and we already
    # have a string, we should just return it.
    if isinstance(s, str):
        return _normalize(s)

    exc = None
    if six.PY3:
        if isinstance(s, (bytes, bytearray)):
            for enc in encoding:
                try:
                    return _normalize(s.decode(enc, errors))
                except UnicodeDecodeError as err:
                    exc = err
                    continue
            # The only way we get this far is if a UnicodeDecodeError was
            # raised, otherwise we would have already returned (or raised some
            # other exception).
            raise exc  # pylint: disable=raising-bad-type
        raise TypeError('expected str, bytes, or bytearray not {}'.format(type(s)))
    else:
        if isinstance(s, bytearray):
            return str(s)  # future lint: disable=blacklisted-function
        if isinstance(s, unicode):  # pylint: disable=incompatible-py3-code,undefined-variable
            for enc in encoding:
                try:
                    return _normalize(s).encode(enc, errors)
                except UnicodeEncodeError as err:
                    exc = err
                    continue
            # The only way we get this far is if a UnicodeDecodeError was
            # raised, otherwise we would have already returned (or raised some
            # other exception).
            raise exc  # pylint: disable=raising-bad-type
        raise TypeError('expected str, bytearray, or unicode')
1149949
def repr_arg(d):
    """"""
    if isinstance(d, dict):
        # if d can be expressed in key=value syntax:
        return "{%s}" % ", ".join(
            "%s: %s" % (repr_arg(k), repr_arg(v)) for k, v in d.items())
    if isinstance(d, list):
        return "[%s]" % ", ".join(repr_arg(elem) for elem in d)
    if isinstance(d, unicode):
        try:
            return repr(d.encode("ascii"))
        except UnicodeEncodeError:
            return repr(d)

    return repr(d)
1073312
def code_binary(item):
    
    code_str = code(item)
    if isinstance(code_str, six.string_types):
        return code_str.encode('utf-8')
    return code_str
1108928
def to_str(s):
    
    if bytes != str:
        """
        这里的比较在python2中是True，在Python3中是False
        """
        if type(s) == bytes:
            return s.decode('utf-8')
    return s
1019975
def _fromstring(value):
    '''
    '''

    if not value:
        return None
    std_value = value.strip().lower()
    if std_value == 'true':
        return 'true'
    elif std_value == 'false':
        return 'false'
#     try:
#         return int(std_value)
#     except ValueError:
#         pass
#     try:
#         return float(std_value)
#     except ValueError:
#         pass
    return value
222936
def _unicode(value):
    
    if isinstance(value, _TO_UNICODE_TYPES):
        return value
    if not isinstance(value, bytes_type):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )
    return value.decode("utf-8")
49307
def _unicode_to_native(s):
  """"""
  if six.PY2:
    return s.encode("utf-8") if isinstance(s, unicode) else s
  else:
    return s
434371
async def serialize(self, value, native=False):
        '''
        
        '''
        if native:
            return self.to_python(value)
        return self.to_data(value)
67173
def _to_str(dumped_val, encoding='utf-8', ordered=True):
    
    _dict = OrderedDict if ordered else dict
    if isinstance(dumped_val, dict):
        return OrderedDict((k, _to_str(v, encoding)) for k,v in dumped_val.items())
    elif isinstance(dumped_val, (list, tuple)):
        return [_to_str(v, encoding) for v in dumped_val]
    elif isinstance(dumped_val, bytes):
        try:
            d = dumped_val.decode('utf-8')
        except Exception:
            d = repr(dumped_val)
        return d
    else:
        return dumped_val
1026579
def to_native_string(string, encoding='ascii'):
    
    out = None

    if isinstance(string, builtin_str):
        out = string
    else:
        if is_py2:
            out = string.encode(encoding)
        else:
            out = string.decode(encoding)

    return out
606831
def encode(cls, value):
        
        coerced = unicode(value)
        if coerced == value and cls.PATTERN.match(coerced):
            return coerced.encode(cls._encoding)

        raise InvalidValue('not ascii')
977636
def to_unicode_or_none(value):
    
    if value == ffi.NULL:
        return None
    elif isinstance(value, ffi.CData):
        return ffi.string(value).decode('utf-8')
    else:
        raise ValueError('Value must be char[] or NULL')
1032934
def cast_str(s, encoding='utf8', errors='strict'):
    """"""
    if isinstance(s, bytes):
        return s.decode(encoding, errors)
    elif isinstance(s, str):
        return s
    else:
        raise TypeError("Expected unicode or bytes, got %r" % s)
1345183
def native_string_to_bytes(s, encoding="ascii", errors="strict"):
    
    if not isinstance(s, str):
        raise TypeError("{} must be type str, not {}".format(s, type(s)))
    if str is bytes:
        # Python 2
        return s
    else:
        # Python 3
        return s.encode(encoding=encoding, errors=errors)
478532
def safe_unicode(obj, *args):
    
    try:
        return unicode(obj, *args)  # noqa for undefined-variable
    except UnicodeDecodeError:
        # obj is byte string
        ascii_text = str(obj).encode('string_escape')
        try:
            return unicode(ascii_text)  # noqa for undefined-variable
        except NameError:
            # This is Python 3, just return the obj as it's already unicode
            return obj
    except NameError:
        # This is Python 3, just return the obj as it's already unicode
        return obj