Skip to content

livery.workshop._pytest_console

livery.workshop._pytest_console

Name a child that ended on a console control event, and who shared the console.

Registered through livery-workshop's pytest11 entry point, so every pytest this venv starts carries it. A process on Windows that receives a console control event, Ctrl+C or Ctrl+Break or the console closing, exits with STATUS_CONTROL_C_EXIT (0xC000013A, 3221225786 as Python reads it). A test whose git child ends that way fails with the bare number, and one sighting in a run of thousands of tests leaves nothing to bisect. When a failed test's exception carries that status, this plugin adds a section to the failure report: the command, the process and its worker, the run, every control event this process's own handler saw in the session with the time it arrived, and every process attached to the console at the moment of the report, by id and image name. On another platform the section says so and carries what it can.

The handler this plugin installs records events and passes them on: it returns false to the console, so the interpreter's own handling is untouched.

Functions:

Name Description
control_exit

Whether code is STATUS_CONTROL_C_EXIT, as Python or the shell spells it.

exit_codes

Every exit code a failure carries: a called process's, down its cause chain.

pytest_configure

Install the recording handler on Windows; elsewhere there is no console.

console_processes

The processes attached to this console, by id and image; None off Windows.

diagnosis

The section's text for a failure that carries the control-exit status.

pytest_runtest_makereport

Add the section to a failed report whose exception carries the status.

Attributes:

Name Type Description
STATUS_CONTROL_C_EXIT

The NTSTATUS a process exits with after a console control event.

STATUS_CONTROL_C_EXIT module-attribute

STATUS_CONTROL_C_EXIT = 3221225786

The NTSTATUS a process exits with after a console control event.

control_exit

control_exit(code: object) -> bool

Whether code is STATUS_CONTROL_C_EXIT, as Python or the shell spells it.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
def control_exit(code: object) -> bool:
    """Whether *code* is ``STATUS_CONTROL_C_EXIT``, as Python or the shell spells it."""
    return code in (STATUS_CONTROL_C_EXIT, _SIGNED)

exit_codes

exit_codes(error: BaseException | None) -> list[int]

Every exit code a failure carries: a called process's, down its cause chain.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
def exit_codes(error: BaseException | None) -> list[int]:
    """Every exit code a failure carries: a called process's, down its cause chain."""
    codes: list[int] = []
    seen = 0
    current = error
    while current is not None and seen < 10:
        code = getattr(current, "returncode", None)
        if isinstance(code, int) and not isinstance(code, bool):
            codes.append(code)
        text = str(current)
        if str(STATUS_CONTROL_C_EXIT) in text and STATUS_CONTROL_C_EXIT not in codes:
            codes.append(STATUS_CONTROL_C_EXIT)
        current = current.__cause__ or current.__context__
        seen += 1
    return codes

pytest_configure

pytest_configure(config: Config) -> None

Install the recording handler on Windows; elsewhere there is no console.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
def pytest_configure(config: pytest.Config) -> None:
    """Install the recording handler on Windows; elsewhere there is no console."""
    del config
    if sys.platform != "win32":
        return
    _install_handler()

console_processes

console_processes() -> list[tuple[int, str]] | None

The processes attached to this console, by id and image; None off Windows.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
def console_processes() -> list[tuple[int, str]] | None:
    """The processes attached to this console, by id and image; None off Windows."""
    if sys.platform != "win32":
        return None
    import ctypes

    kernel32 = ctypes.windll.kernel32
    room = 64
    while True:
        ids = (ctypes.c_uint * room)()
        count = kernel32.GetConsoleProcessList(ids, room)
        if count == 0:
            return []
        if count <= room:
            break
        room = count
    found: list[tuple[int, str]] = []
    for pid in list(ids)[:count]:
        found.append((int(pid), _image_name(int(pid))))
    return found

diagnosis

diagnosis(error: BaseException | None) -> str

The section's text for a failure that carries the control-exit status.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
def diagnosis(error: BaseException | None) -> str:
    """The section's text for a failure that carries the control-exit status."""
    lines = [
        "a child ended with STATUS_CONTROL_C_EXIT (0xC000013A, 3221225786): a"
        " console control event reached it; the exit is not the child's own",
    ]
    command = getattr(error, "cmd", None)
    if command is not None:
        lines.append(f"command: {command!r}")
    lines.append(f"cwd: {os.getcwd()}")
    lines.append(
        f"this process: pid {os.getpid()}, parent {os.getppid()}, worker"
        f" {os.environ.get('PYTEST_XDIST_WORKER', 'none')}"
    )
    run = {
        key: os.environ[key]
        for key in ("GITHUB_RUN_ID", "GITHUB_JOB", "RUNNER_NAME", "RUNNER_OS")
        if os.environ.get(key)
    }
    lines.append(f"run: {run or 'not a CI run'}")
    if _EVENTS:
        lines.append("control events this process saw:")
        lines.extend(
            f"  {time.strftime('%H:%M:%S', time.gmtime(when))}Z"
            f" {_EVENT_NAMES.get(kind, str(kind))}"
            for when, kind in _EVENTS
        )
    else:
        lines.append("control events this process saw: none")
    attached = console_processes()
    if attached is None:
        lines.append("console processes: not a Windows console")
    elif not attached:
        lines.append("console processes: none, this process has no console")
    else:
        lines.append("console processes:")
        lines.extend(f"  {pid} {name}" for pid, name in attached)
    lines.append(f"reported at: {time.strftime('%Y-%m-%dT%H:%M:%S', time.gmtime())}Z")
    return "\n".join(lines)

pytest_runtest_makereport

pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> Any

Add the section to a failed report whose exception carries the status.

Source code in packages/workshop/src/livery/workshop/_pytest_console.py
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item: pytest.Item, call: pytest.CallInfo[None]) -> Any:
    """Add the section to a failed report whose exception carries the status."""
    del item
    outcome = yield
    report = outcome.get_result()
    if not report.failed or call.excinfo is None:
        return
    error = call.excinfo.value
    if not any(control_exit(code) for code in exit_codes(error)):
        return
    report.sections.append(("console control event", diagnosis(error)))