jpayne@7: """ jpayne@7: requests._internal_utils jpayne@7: ~~~~~~~~~~~~~~ jpayne@7: jpayne@7: Provides utility functions that are consumed internally by Requests jpayne@7: which depend on extremely few external helpers (such as compat) jpayne@7: """ jpayne@7: import re jpayne@7: jpayne@7: from .compat import builtin_str jpayne@7: jpayne@7: _VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$") jpayne@7: _VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$") jpayne@7: _VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$") jpayne@7: _VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$") jpayne@7: jpayne@7: _HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR) jpayne@7: _HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE) jpayne@7: HEADER_VALIDATORS = { jpayne@7: bytes: _HEADER_VALIDATORS_BYTE, jpayne@7: str: _HEADER_VALIDATORS_STR, jpayne@7: } jpayne@7: jpayne@7: jpayne@7: def to_native_string(string, encoding="ascii"): jpayne@7: """Given a string object, regardless of type, returns a representation of jpayne@7: that string in the native string type, encoding and decoding where jpayne@7: necessary. This assumes ASCII unless told otherwise. jpayne@7: """ jpayne@7: if isinstance(string, builtin_str): jpayne@7: out = string jpayne@7: else: jpayne@7: out = string.decode(encoding) jpayne@7: jpayne@7: return out jpayne@7: jpayne@7: jpayne@7: def unicode_is_ascii(u_string): jpayne@7: """Determine if unicode string only contains ASCII characters. jpayne@7: jpayne@7: :param str u_string: unicode string to check. Must be unicode jpayne@7: and not Python 2 `str`. jpayne@7: :rtype: bool jpayne@7: """ jpayne@7: assert isinstance(u_string, str) jpayne@7: try: jpayne@7: u_string.encode("ascii") jpayne@7: return True jpayne@7: except UnicodeEncodeError: jpayne@7: return False