Home / Function/ super_len() — requests Function Reference

super_len() — requests Function Reference

Architecture documentation for the super_len() function in utils.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  afb5f427_a5f4_14c6_bceb_0bab74c9174b["super_len()"]
  2c39b9da_e317_5e6c_bbac_8362bac2110c["utils.py"]
  afb5f427_a5f4_14c6_bceb_0bab74c9174b -->|defined in| 2c39b9da_e317_5e6c_bbac_8362bac2110c
  fdb744d2_4cc0_a0f9_c288_a0abeac5e58e["prepare_body()"]
  fdb744d2_4cc0_a0f9_c288_a0abeac5e58e -->|calls| afb5f427_a5f4_14c6_bceb_0bab74c9174b
  ad57bfe9_2bba_ea6e_5e85_44c51303ed35["prepare_content_length()"]
  ad57bfe9_2bba_ea6e_5e85_44c51303ed35 -->|calls| afb5f427_a5f4_14c6_bceb_0bab74c9174b
  style afb5f427_a5f4_14c6_bceb_0bab74c9174b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/requests/utils.py lines 134–202

def super_len(o):
    total_length = None
    current_position = 0

    if not is_urllib3_1 and isinstance(o, str):
        # urllib3 2.x+ treats all strings as utf-8 instead
        # of latin-1 (iso-8859-1) like http.client.
        o = o.encode("utf-8")

    if hasattr(o, "__len__"):
        total_length = len(o)

    elif hasattr(o, "len"):
        total_length = o.len

    elif hasattr(o, "fileno"):
        try:
            fileno = o.fileno()
        except (io.UnsupportedOperation, AttributeError):
            # AttributeError is a surprising exception, seeing as how we've just checked
            # that `hasattr(o, 'fileno')`.  It happens for objects obtained via
            # `Tarfile.extractfile()`, per issue 5229.
            pass
        else:
            total_length = os.fstat(fileno).st_size

            # Having used fstat to determine the file length, we need to
            # confirm that this file was opened up in binary mode.
            if "b" not in o.mode:
                warnings.warn(
                    (
                        "Requests has determined the content-length for this "
                        "request using the binary size of the file: however, the "
                        "file has been opened in text mode (i.e. without the 'b' "
                        "flag in the mode). This may lead to an incorrect "
                        "content-length. In Requests 3.0, support will be removed "
                        "for files in text mode."
                    ),
                    FileModeWarning,
                )

    if hasattr(o, "tell"):
        try:
            current_position = o.tell()
        except OSError:
            # This can happen in some weird situations, such as when the file
            # is actually a special file descriptor like stdin. In this
            # instance, we don't know what the length is, so set it to zero and
            # let requests chunk it instead.
            if total_length is not None:
                current_position = total_length
        else:
            if hasattr(o, "seek") and total_length is None:
                # StringIO and BytesIO have seek but no usable fileno
                try:
                    # seek to end of file
                    o.seek(0, 2)
                    total_length = o.tell()

                    # seek back to current position to support
                    # partially read file-like objects
                    o.seek(current_position or 0)
                except OSError:
                    total_length = 0

    if total_length is None:
        total_length = 0

    return max(0, total_length - current_position)

Domain

Subdomains

Frequently Asked Questions

What does super_len() do?
super_len() is a function in the requests codebase, defined in src/requests/utils.py.
Where is super_len() defined?
super_len() is defined in src/requests/utils.py at line 134.
What calls super_len()?
super_len() is called by 2 function(s): prepare_body, prepare_content_length.

Analyze Your Own Codebase

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

Try Supermodel Free