def value_from_fsnative(arg, escape): """Takes an item from argv and returns a text_type value without surrogate escapes or raises ValueError. """ assert isinstance(arg, fsnative) if escape: bytes_ = fsn2bytes(arg, "utf-8") if PY2: bytes_ = bytes_.decode("string_escape") else: bytes_ = codecs.escape_decode(bytes_)[0] arg = bytes2fsn(bytes_, "utf-8") text = fsn2text(arg, strict=True) return text
def value_from_fsnative(arg, escape): """Takes an item from argv and returns a str value without surrogate escapes or raises ValueError. """ assert isinstance(arg, fsnative) if escape: bytes_ = fsn2bytes(arg) # With py3.7 this has started to warn for invalid escapes, but we # don't control the input so ignore it. with warnings.catch_warnings(): warnings.simplefilter("ignore") bytes_ = codecs.escape_decode(bytes_)[0] arg = bytes2fsn(bytes_) text = fsn2text(arg, strict=True) return text
def value_from_fsnative(arg, escape): """Takes an item from argv and returns a text_type value without surrogate escapes or raises ValueError. """ assert isinstance(arg, fsnative) if escape: bytes_ = fsn2bytes(arg, "utf-8") if PY2: bytes_ = bytes_.decode("string_escape") else: bytes_ = codecs.escape_decode(bytes_)[0] arg = bytes2fsn(bytes_, "utf-8") text = fsn2text(arg) # only allow values which can be converted back if arg != text2fsn(text): raise ValueError("%r can't be decoded" % arg) return text
def value_from_fsnative(arg, escape): """Takes an item from argv and returns a text_type value without surrogate escapes or raises ValueError. """ assert isinstance(arg, fsnative) if escape: bytes_ = fsn2bytes(arg) if PY2: bytes_ = bytes_.decode("string_escape") else: # With py3.7 this has started to warn for invalid escapes, but we # don't control the input so ignore it. with warnings.catch_warnings(): warnings.simplefilter("ignore") bytes_ = codecs.escape_decode(bytes_)[0] arg = bytes2fsn(bytes_) text = fsn2text(arg, strict=True) return text