pypa/pip · error · ELFInvalid

unable to parse identification

Error message

unable to parse identification

What it means

The ELFFile class from packaging._elffile parses ELF executables. The constructor reads the first 16 bytes (the e_ident identification array). If the underlying file is too short to provide 16 bytes, struct.unpack raises struct.error, which is caught and re-raised as ELFInvalid('unable to parse identification'). The file is not a valid ELF binary or is severely truncated.

Solutions

  1. Check the file is at least 64 bytes before constructing ELFFile
  2. Verify the magic bytes b'\x7fELF' at offset 0 before parsing
  3. Catch ELFInvalid and skip or handle non-ELF files gracefully

Example fix

# before
with open(path, "rb") as f:
    elf = ELFFile(f)  # raises on tiny/non-ELF file

# after
import os
from packaging._elffile import ELFFile, ELFInvalid

if os.path.getsize(path) < 64:
    raise ValueError(f"{path} is too small to be an ELF file")
with open(path, "rb") as f:
    if f.read(4) != b"\x7fELF":
        raise ValueError(f"{path} is not an ELF file")
    f.seek(0)
    elf = ELFFile(f)
Defensive patterns

Strategy: try-catch

Validate before calling

import os

def is_likely_elf(path):
    if os.path.getsize(path) < 16:
        return False
    with open(path, "rb") as f:
        return f.read(4) == b"\x7fELF"

Try / catch

from packaging._elffile import ELFFile, ELFInvalid

try:
    with open(path, "rb") as f:
        elf = ELFFile(f)
except ELFInvalid:
    elf = None  # not a valid ELF file, handle accordingly

Prevention

When it happens

Trigger: Constructing ELFFile(f) where f is a file object containing fewer than 16 bytes: an empty file, a text script, a truncated download, or a completely non-ELF binary format.

Common situations: pip uses this internally to find interpreter paths in wheel-installed scripts. Developers hit it when passing wrong file handles, using fixture files that are not real ELF binaries, or processing corrupted/incomplete downloads.

Understand the failure class

Related errors


AI-assisted analysis of pypa/pip@f399c37189 (2026-08-08). Data as JSON: /api/errors/77c431fb89e453d2. Report an issue: GitHub.

Appendix: source

Thrown at src/pip/_vendor/packaging/_elffile.py:51

    I386 = 3
    S390 = 22
    Arm = 40
    X8664 = 62
    AArch64 = 183


class ELFFile:
    """
    Representation of an ELF executable.
    """

    def __init__(self, f: IO[bytes]) -> None:
        self._f = f

        try:
            ident = self._read("16B")
        except struct.error as e:
            raise ELFInvalid("unable to parse identification") from e
        magic = bytes(ident[:4])
        if magic != b"\x7fELF":
            raise ELFInvalid(f"invalid magic: {magic!r}")

        self.capacity = ident[4]  # Format for program header (bitness).
        self.encoding = ident[5]  # Data structure encoding (endianness).

        try:
            # e_fmt: Format for the ELF header.
            # p_fmt: Format for a program header.
            # p_idx: Indexes to find p_type, p_offset, and p_filesz.
            e_fmt, self._p_fmt, self._p_idx = {
                (1, 1): ("<HHIIIIIHHH", "<IIIIIIII", (0, 1, 4)),  # 32-bit LSB.
                (1, 2): (">HHIIIIIHHH", ">IIIIIIII", (0, 1, 4)),  # 32-bit MSB.
                (2, 1): ("<HHIQQQIHHH", "<IIQQQQQQ", (0, 2, 5)),  # 64-bit LSB.
                (2, 2): (">HHIQQQIHHH", ">IIQQQQQQ", (0, 2, 5)),  # 64-bit MSB.
            }[(self.capacity, self.encoding)]
        except KeyError as e:

View on GitHub (pinned to f399c37189)