@@ -41,7 +41,7 @@ class SafeRepr(reprlib.Repr):
41
41
information on exceptions raised during the call.
42
42
"""
43
43
44
- def __init__ (self , maxsize : Optional [int ]) -> None :
44
+ def __init__ (self , maxsize : Optional [int ], use_ascii : bool = False ) -> None :
45
45
"""
46
46
:param maxsize:
47
47
If not None, will truncate the resulting repr to that specific size, using ellipsis
@@ -54,10 +54,15 @@ def __init__(self, maxsize: Optional[int]) -> None:
54
54
# truncation.
55
55
self .maxstring = maxsize if maxsize is not None else 1_000_000_000
56
56
self .maxsize = maxsize
57
+ self .use_ascii = use_ascii
57
58
58
59
def repr (self , x : object ) -> str :
59
60
try :
60
- s = super ().repr (x )
61
+ if self .use_ascii :
62
+ s = ascii (x )
63
+ else :
64
+ s = super ().repr (x )
65
+
61
66
except (KeyboardInterrupt , SystemExit ):
62
67
raise
63
68
except BaseException as exc :
@@ -94,7 +99,9 @@ def safeformat(obj: object) -> str:
94
99
DEFAULT_REPR_MAX_SIZE = 240
95
100
96
101
97
- def saferepr (obj : object , maxsize : Optional [int ] = DEFAULT_REPR_MAX_SIZE ) -> str :
102
+ def saferepr (
103
+ obj : object , maxsize : Optional [int ] = DEFAULT_REPR_MAX_SIZE , use_ascii : bool = False
104
+ ) -> str :
98
105
"""Return a size-limited safe repr-string for the given object.
99
106
100
107
Failing __repr__ functions of user instances will be represented
@@ -104,10 +111,11 @@ def saferepr(obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE) -> str
104
111
This function is a wrapper around the Repr/reprlib functionality of the
105
112
stdlib.
106
113
"""
107
- return SafeRepr (maxsize ).repr (obj )
114
+
115
+ return SafeRepr (maxsize , use_ascii ).repr (obj )
108
116
109
117
110
- def saferepr_unlimited (obj : object ) -> str :
118
+ def saferepr_unlimited (obj : object , use_ascii : bool = True ) -> str :
111
119
"""Return an unlimited-size safe repr-string for the given object.
112
120
113
121
As with saferepr, failing __repr__ functions of user instances
@@ -119,6 +127,8 @@ def saferepr_unlimited(obj: object) -> str:
119
127
when maxsize=None, but that might affect some other code.
120
128
"""
121
129
try :
130
+ if use_ascii :
131
+ return ascii (obj )
122
132
return repr (obj )
123
133
except Exception as exc :
124
134
return _format_repr_exception (exc , obj )
0 commit comments