3
3
"""
4
4
from __future__ import annotations
5
5
6
+ import reprlib
6
7
from typing import Any
7
8
from typing import Callable
8
9
from typing import Sequence
9
10
from typing import Tuple
10
- import reprlib
11
11
12
12
13
13
_Writer = Callable [[str ], object ]
@@ -60,6 +60,7 @@ def setprocessor(self, tags: str | tuple[str, ...], processor: _Processor) -> No
60
60
assert isinstance (tags , tuple )
61
61
self ._tags2proc [tags ] = processor
62
62
63
+
63
64
def _try_repr_or_str (obj : object ) -> str :
64
65
try :
65
66
return repr (obj )
@@ -68,6 +69,7 @@ def _try_repr_or_str(obj: object) -> str:
68
69
except BaseException :
69
70
return f'{ type (obj ).__name__ } ("{ obj } ")'
70
71
72
+
71
73
def _format_repr_exception (exc : BaseException , obj : object ) -> str :
72
74
try :
73
75
exc_info = _try_repr_or_str (exc )
@@ -79,20 +81,24 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str:
79
81
exc_info , type (obj ).__name__ , id (obj )
80
82
)
81
83
84
+
82
85
def _ellipsize (s : str , maxsize : int ) -> str :
83
86
if len (s ) > maxsize :
84
87
i = max (0 , (maxsize - 3 ) // 2 )
85
88
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 :]
87
92
return s
88
93
94
+
89
95
class SafeRepr (reprlib .Repr ):
90
96
"""
91
97
repr.Repr that limits the resulting size of repr() and includes
92
98
information on exceptions raised during the call.
93
99
"""
94
100
95
- def __init__ (self , maxsize : Optional [ int ] , use_ascii : bool = False ) -> None :
101
+ def __init__ (self , maxsize : int | None , use_ascii : bool = False ) -> None :
96
102
"""
97
103
:param maxsize:
98
104
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:
136
142
137
143
# Maximum size of overall repr of objects to display during assertion errors.
138
144
DEFAULT_REPR_MAX_SIZE = 240
145
+
146
+
139
147
def 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
141
149
) -> str :
142
150
"""Return a size-limited safe repr-string for the given object.
143
151
@@ -151,6 +159,7 @@ def saferepr(
151
159
152
160
return SafeRepr (maxsize , use_ascii ).repr (obj )
153
161
162
+
154
163
class TagTracerSub :
155
164
def __init__ (self , root : TagTracer , tags : tuple [str , ...]) -> None :
156
165
self .root = root
0 commit comments