33"""
44from __future__ import annotations
55
6+ import reprlib
67from typing import Any
78from typing import Callable
89from typing import Sequence
910from typing import Tuple
10- import reprlib
1111
1212
1313_Writer = Callable [[str ], object ]
@@ -60,6 +60,7 @@ def setprocessor(self, tags: str | tuple[str, ...], processor: _Processor) -> No
6060 assert isinstance (tags , tuple )
6161 self ._tags2proc [tags ] = processor
6262
63+
6364def _try_repr_or_str (obj : object ) -> str :
6465 try :
6566 return repr (obj )
@@ -68,6 +69,7 @@ def _try_repr_or_str(obj: object) -> str:
6869 except BaseException :
6970 return f'{ type (obj ).__name__ } ("{ obj } ")'
7071
72+
7173def _format_repr_exception (exc : BaseException , obj : object ) -> str :
7274 try :
7375 exc_info = _try_repr_or_str (exc )
@@ -79,20 +81,24 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str:
7981 exc_info , type (obj ).__name__ , id (obj )
8082 )
8183
84+
8285def _ellipsize (s : str , maxsize : int ) -> str :
8386 if len (s ) > maxsize :
8487 i = max (0 , (maxsize - 3 ) // 2 )
8588 j = max (0 , maxsize - 3 - i )
86- return s [:i ] + "..." + s [len (s ) - j :]
89+
90+ x = len (s ) - j
91+ return s [:i ] + "..." + s [x :]
8792 return s
8893
94+
8995class SafeRepr (reprlib .Repr ):
9096 """
9197 repr.Repr that limits the resulting size of repr() and includes
9298 information on exceptions raised during the call.
9399 """
94100
95- def __init__ (self , maxsize : Optional [ int ] , use_ascii : bool = False ) -> None :
101+ def __init__ (self , maxsize : int | None , use_ascii : bool = False ) -> None :
96102 """
97103 :param maxsize:
98104 If not None, will truncate the resulting repr to that specific size, using ellipsis
@@ -136,8 +142,10 @@ def repr_instance(self, x: object, level: int) -> str:
136142
137143# Maximum size of overall repr of objects to display during assertion errors.
138144DEFAULT_REPR_MAX_SIZE = 240
145+
146+
139147def saferepr (
140- obj : object , maxsize : Optional [ int ] = DEFAULT_REPR_MAX_SIZE , use_ascii : bool = False
148+ obj : object , maxsize : int | None = DEFAULT_REPR_MAX_SIZE , use_ascii : bool = False
141149) -> str :
142150 """Return a size-limited safe repr-string for the given object.
143151
@@ -151,6 +159,7 @@ def saferepr(
151159
152160 return SafeRepr (maxsize , use_ascii ).repr (obj )
153161
162+
154163class TagTracerSub :
155164 def __init__ (self , root : TagTracer , tags : tuple [str , ...]) -> None :
156165 self .root = root
0 commit comments