test_unraisableexception.py


Overview

`test_unraisableexception.py` is a test suite designed to verify the behavior of the pytest framework when handling *unraisable exceptions*. Unraisable exceptions occur when an exception is raised during object finalization (such as in a [__del__](/projects/286/67223) method) or during other contexts where exceptions cannot propagate normally, like garbage collection or asyncio task creation failures.

This file contains multiple test cases that:

These tests use the `pytest` testing framework along with its `Pytester` utility for creating test files dynamically and running pytest programmatically.


Detailed Explanations

Constants and Imports


Test Functions

All test functions use the `pytest` marker decorators to skip tests on PyPy (due to GC differences) and to filter or escalate the `PytestUnraisableExceptionWarning`.


test_unraisable(pytester: Pytester) -> None

def test_unraisable(pytester):
    # pytester.makepyfile(...) creates the test file.
    # pytester.runpytest() runs pytest on the created file.
    # Assertions check that unraisable exceptions are correctly warned.

test_unraisable_in_setup(pytester: Pytester) -> None


test_unraisable_in_teardown(pytester: Pytester) -> None


test_unraisable_warning_error(pytester: Pytester) -> None


test_unraisable_warning_multiple_errors(pytester: Pytester) -> None


test_unraisable_collection_failure(pytester: Pytester) -> None


_set_gc_state(enabled: bool) -> bool


test_refcycle_unraisable(pytester: Pytester) -> None


test_refcycle_unraisable_warning_filter(pytester: Pytester) -> None


test_create_task_raises_unraisable_warning_filter(pytester: Pytester) -> None


test_refcycle_unraisable_warning_filter_default(pytester: Pytester) -> None


test_possibly_none_excinfo(pytester: Pytester) -> None


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

flowchart TD
    A[test_unraisableexception.py Tests]

    A --> B[test_unraisable]
    A --> C[test_unraisable_in_setup]
    A --> D[test_unraisable_in_teardown]
    A --> E[test_unraisable_warning_error]
    A --> F[test_unraisable_warning_multiple_errors]
    A --> G[test_unraisable_collection_failure]
    A --> H[test_refcycle_unraisable]
    A --> I[test_refcycle_unraisable_warning_filter]
    A --> J[test_create_task_raises_unraisable_warning_filter]
    A --> K[test_refcycle_unraisable_warning_filter_default]
    A --> L[test_possibly_none_excinfo]

    subgraph Setup/Teardown Tests
        C
        D
    end

    subgraph Warning/Error Handling
        E
        F
        K
        L
    end

    subgraph GC & Reference Cycles
        H
        I
    end

    gmock(Mocking traceback.format_exception) --> G

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style B,C,D,E,F,G,H,I,J,K,L fill:#bbf,stroke:#333,stroke-width:1px

Summary

`test_unraisableexception.py` is a comprehensive test suite validating how pytest detects, warns, and handles unraisable exceptions in various scenarios. It ensures that pytest's warning mechanism behaves correctly across different Python versions, GC states, and execution contexts, thereby improving pytest’s reliability and developer feedback for hard-to-catch errors.