test_saferepr.py


Overview

The `test_saferepr.py` file contains a suite of automated tests designed to verify the correctness, robustness, and edge-case behaviors of the `saferepr` and `saferepr_unlimited` functions from the `_pytest._io.saferepr` module. These functions provide safe, truncated, and exception-handling string representations (`repr`) of Python objects, which are vital in testing frameworks such as `pytest` for displaying readable and failure-resilient representations of test data and exceptions.

The tests cover a wide range of scenarios, including:

These tests ensure that `saferepr` behaves reliably even under unusual or error-prone conditions, which is critical for stable test output formatting in `pytest`.


Detailed Explanation of Functions and Tests

Imported Entities


Test Functions

Each function is prefixed with `test_` to be recognized by `pytest`.


test_simple_repr()

**Purpose:** Tests basic usage of `saferepr` with simple objects.

**Behavior:**

**Example:**

assert saferepr(1) == "1"
assert saferepr(None) == "None"

test_maxsize()

**Purpose:** Tests behavior of `saferepr` truncation when `maxsize` is specified.

**Parameters:**

**Behavior:**


test_no_maxsize()

**Purpose:** Tests `saferepr` without truncation (`maxsize=None`).

**Behavior:**


test_maxsize_error_on_instance()

**Purpose:** Verifies `saferepr` handles tuple containing an object whose `__repr__` raises an error, respecting truncation.

**Details:**


test_exceptions()

**Purpose:** Extensive test of `saferepr` on objects whose `__repr__` or `__str__` methods raise exceptions.

**Classes:**

**Behavior:**


test_baseexception()

**Purpose:** Tests `saferepr` on `BaseException` subclasses that raise exceptions during their `__str__` or `__repr__`.

**Classes:**

**Behavior:**


test_buggy_builtin_repr()

**Purpose:** Simulates a built-in type (`int`) with a buggy `__repr__` that raises an exception.

**Behavior:**


test_big_repr()

**Purpose:** Tests that the representation of large iterables (e.g., `range(1000)`) does not exceed expected length limits.


test_repr_on_newstyle()

**Purpose:** Validates `saferepr` on an object with a user-defined `__repr__` referencing an attribute that may not exist.


test_unicode()

**Purpose:** Tests `saferepr` on Unicode strings to verify correct quoting and encoding in output.


test_broken_getattribute()

**Purpose:** Tests `saferepr` on objects with broken `__getattribute__` and `__repr__` methods that raise exceptions.

**Behavior:**


test_saferepr_unlimited()

**Purpose:** Tests `saferepr_unlimited` which produces full, non-truncated representations.

**Details:**


test_saferepr_unlimited_exc()

**Purpose:** Tests `saferepr_unlimited` on an object whose `__repr__` raises an exception.

**Behavior:**


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Below are simplified usage examples inspired by the tests:

from _pytest._io.saferepr import saferepr

# Basic usage
print(saferepr(123))  # Output: "123"

# Truncated representation
long_str = "x" * 50
print(saferepr(long_str, maxsize=25))  # Output: "'xxxxxxxxxx...xxxxxxxxxx'"

# Handling object with broken __repr__
class Broken:
    def __repr__(self):
        raise ValueError("error")

print(saferepr(Broken()))  
# Output: "<[ValueError('error') raised in repr()] Broken object at 0x...>"

Mermaid Diagram: Flowchart of Main Test Functions and Their Relationships

flowchart TD
    A[test_saferepr.py] --> B[test_simple_repr]
    A --> C[test_maxsize]
    A --> D[test_no_maxsize]
    A --> E[test_maxsize_error_on_instance]
    A --> F[test_exceptions]
    A --> G[test_baseexception]
    A --> H[test_buggy_builtin_repr]
    A --> I[test_big_repr]
    A --> J[test_repr_on_newstyle]
    A --> K[test_unicode]
    A --> L[test_broken_getattribute]
    A --> M[test_saferepr_unlimited]
    A --> N[test_saferepr_unlimited_exc]

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

Summary

`test_saferepr.py` is a critical test suite ensuring that the `saferepr` functions from pytest provide safe, readable, and robust string representations of Python objects, even in the presence of faulty or exception-raising `__repr__` methods. It covers truncation behavior, exception safety, and special cases for exception objects, supporting pytest’s goal of clear and stable test output display.