diff --git a/src/pluggy/_tracing.py b/src/pluggy/_tracing.py index 7be55493..79149721 100644 --- a/src/pluggy/_tracing.py +++ b/src/pluggy/_tracing.py @@ -3,11 +3,11 @@ """ from __future__ import annotations +import reprlib from typing import Any from typing import Callable from typing import Sequence from typing import Tuple -import reprlib _Writer = Callable[[str], object] @@ -60,6 +60,7 @@ def setprocessor(self, tags: str | tuple[str, ...], processor: _Processor) -> No assert isinstance(tags, tuple) self._tags2proc[tags] = processor + def _try_repr_or_str(obj: object) -> str: try: return repr(obj) @@ -68,6 +69,7 @@ def _try_repr_or_str(obj: object) -> str: except BaseException: return f'{type(obj).__name__}("{obj}")' + def _format_repr_exception(exc: BaseException, obj: object) -> str: try: exc_info = _try_repr_or_str(exc) @@ -79,6 +81,7 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str: exc_info, type(obj).__name__, id(obj) ) + def _ellipsize(s: str, maxsize: int) -> str: if len(s) > maxsize: i = max(0, (maxsize - 3) // 2) @@ -86,6 +89,7 @@ def _ellipsize(s: str, maxsize: int) -> str: return s[:i] + "..." + s[len(s) - j :] return s + class SafeRepr(reprlib.Repr): """ repr.Repr that limits the resulting size of repr() and includes @@ -136,6 +140,8 @@ def repr_instance(self, x: object, level: int) -> str: # Maximum size of overall repr of objects to display during assertion errors. DEFAULT_REPR_MAX_SIZE = 240 + + def saferepr( obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False ) -> str: @@ -151,6 +157,7 @@ def saferepr( return SafeRepr(maxsize, use_ascii).repr(obj) + class TagTracerSub: def __init__(self, root: TagTracer, tags: tuple[str, ...]) -> None: self.root = root diff --git a/testing/benchmark.py b/testing/benchmark.py index 2860dfd4..ea13bfb2 100644 --- a/testing/benchmark.py +++ b/testing/benchmark.py @@ -3,14 +3,13 @@ """ import pytest +from ._tracing import saferepr from pluggy import HookimplMarker from pluggy import HookspecMarker from pluggy import PluginManager from pluggy._callers import _multicall from pluggy._hooks import HookImpl -from ._tracing import saferepr - hookspec = HookspecMarker("example") hookimpl = HookimplMarker("example") diff --git a/testing/test_hookcaller.py b/testing/test_hookcaller.py index 8a7d1aeb..eaa81adf 100644 --- a/testing/test_hookcaller.py +++ b/testing/test_hookcaller.py @@ -450,7 +450,10 @@ def conflict(self) -> None: ".Api1'>" ) -def test_hookcaller_repr_with_saferepr_failure(hc: HookCaller, addmeth: AddMeth) -> None: + +def test_hookcaller_repr_with_saferepr_failure( + hc: HookCaller, addmeth: AddMeth +) -> None: @addmeth() def he_method1() -> None: pass @@ -466,4 +469,4 @@ def he_method3() -> None: # Verify that HookCaller.repr with saferepr still works despite the error expected_repr = f"" - assert repr(hc) == expected_repr \ No newline at end of file + assert repr(hc) == expected_repr diff --git a/testing/test_tracer.py b/testing/test_tracer.py index b63637c6..fa23b52d 100644 --- a/testing/test_tracer.py +++ b/testing/test_tracer.py @@ -2,7 +2,8 @@ import pytest -from pluggy._tracing import saferepr, DEFAULT_REPR_MAX_SIZE +from pluggy._tracing import DEFAULT_REPR_MAX_SIZE +from pluggy._tracing import saferepr from pluggy._tracing import TagTracer @@ -79,6 +80,7 @@ def test_setprocessor(rootlogger: TagTracer) -> None: tags, args = l2[0] assert args == ("seen",) + def test_saferepr_simple_repr(): assert saferepr(1) == "1" assert saferepr(None) == "None" @@ -247,4 +249,4 @@ def __repr__(self): assert saferepr(SomeClass()).startswith( "<[RuntimeError() raised in repr()] SomeClass object at 0x" - ) \ No newline at end of file + )