Home / File/ _internal_utils.py — requests Source File

_internal_utils.py — requests Source File

Architecture documentation for _internal_utils.py, a python file in the requests codebase. 2 imports, 5 dependents.

File python CoreAPI VerbHandlers 2 imports 5 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  ad9103ba_2c26_1ff0_c67d_e7f70c6f108c["_internal_utils.py"]
  655589d9_9504_8132_6277_d047dcd65486["compat.py"]
  ad9103ba_2c26_1ff0_c67d_e7f70c6f108c --> 655589d9_9504_8132_6277_d047dcd65486
  bf35b237_3fd1_dc85_411c_b1c073e3a773["re"]
  ad9103ba_2c26_1ff0_c67d_e7f70c6f108c --> bf35b237_3fd1_dc85_411c_b1c073e3a773
  d7a739b0_e73b_9565_f5ed_5e8c24943504["auth.py"]
  d7a739b0_e73b_9565_f5ed_5e8c24943504 --> ad9103ba_2c26_1ff0_c67d_e7f70c6f108c
  270696ff_2a4f_ef5b_92e8_33a79e68a2d8["cookies.py"]
  270696ff_2a4f_ef5b_92e8_33a79e68a2d8 --> ad9103ba_2c26_1ff0_c67d_e7f70c6f108c
  461bc6e0_32e7_8eab_ec87_7226e7be0d13["models.py"]
  461bc6e0_32e7_8eab_ec87_7226e7be0d13 --> ad9103ba_2c26_1ff0_c67d_e7f70c6f108c
  ea1101aa_233b_1206_7b38_a38f0fe92a52["sessions.py"]
  ea1101aa_233b_1206_7b38_a38f0fe92a52 --> ad9103ba_2c26_1ff0_c67d_e7f70c6f108c
  2c39b9da_e317_5e6c_bbac_8362bac2110c["utils.py"]
  2c39b9da_e317_5e6c_bbac_8362bac2110c --> ad9103ba_2c26_1ff0_c67d_e7f70c6f108c
  style ad9103ba_2c26_1ff0_c67d_e7f70c6f108c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""
requests._internal_utils
~~~~~~~~~~~~~~

Provides utility functions that are consumed internally by Requests
which depend on extremely few external helpers (such as compat)
"""

import re

from .compat import builtin_str

_VALID_HEADER_NAME_RE_BYTE = re.compile(rb"^[^:\s][^:\r\n]*$")
_VALID_HEADER_NAME_RE_STR = re.compile(r"^[^:\s][^:\r\n]*$")
_VALID_HEADER_VALUE_RE_BYTE = re.compile(rb"^\S[^\r\n]*$|^$")
_VALID_HEADER_VALUE_RE_STR = re.compile(r"^\S[^\r\n]*$|^$")

_HEADER_VALIDATORS_STR = (_VALID_HEADER_NAME_RE_STR, _VALID_HEADER_VALUE_RE_STR)
_HEADER_VALIDATORS_BYTE = (_VALID_HEADER_NAME_RE_BYTE, _VALID_HEADER_VALUE_RE_BYTE)
HEADER_VALIDATORS = {
    bytes: _HEADER_VALIDATORS_BYTE,
    str: _HEADER_VALIDATORS_STR,
}


def to_native_string(string, encoding="ascii"):
    """Given a string object, regardless of type, returns a representation of
    that string in the native string type, encoding and decoding where
    necessary. This assumes ASCII unless told otherwise.
    """
    if isinstance(string, builtin_str):
        out = string
    else:
        out = string.decode(encoding)

    return out


def unicode_is_ascii(u_string):
    """Determine if unicode string only contains ASCII characters.

    :param str u_string: unicode string to check. Must be unicode
        and not Python 2 `str`.
    :rtype: bool
    """
    assert isinstance(u_string, str)
    try:
        u_string.encode("ascii")
        return True
    except UnicodeEncodeError:
        return False

Domain

Subdomains

Dependencies

Frequently Asked Questions

What does _internal_utils.py do?
_internal_utils.py is a source file in the requests codebase, written in python. It belongs to the CoreAPI domain, VerbHandlers subdomain.
What functions are defined in _internal_utils.py?
_internal_utils.py defines 2 function(s): to_native_string, unicode_is_ascii.
What does _internal_utils.py depend on?
_internal_utils.py imports 2 module(s): compat.py, re.
What files import _internal_utils.py?
_internal_utils.py is imported by 5 file(s): auth.py, cookies.py, models.py, sessions.py, utils.py.
Where is _internal_utils.py in the architecture?
_internal_utils.py is located at src/requests/_internal_utils.py (domain: CoreAPI, subdomain: VerbHandlers, directory: src/requests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free