Return ascii(obj) or repr(obj) + tests
This commit is contained in:
parent
50d75b3822
commit
f9302f1ca1
|
@ -41,7 +41,7 @@ class SafeRepr(reprlib.Repr):
|
||||||
information on exceptions raised during the call.
|
information on exceptions raised during the call.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, maxsize: Optional[int]) -> None:
|
def __init__(self, maxsize: Optional[int], use_ascii: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
:param maxsize:
|
:param maxsize:
|
||||||
If not None, will truncate the resulting repr to that specific size, using ellipsis
|
If not None, will truncate the resulting repr to that specific size, using ellipsis
|
||||||
|
@ -54,9 +54,13 @@ class SafeRepr(reprlib.Repr):
|
||||||
# truncation.
|
# truncation.
|
||||||
self.maxstring = maxsize if maxsize is not None else 1_000_000_000
|
self.maxstring = maxsize if maxsize is not None else 1_000_000_000
|
||||||
self.maxsize = maxsize
|
self.maxsize = maxsize
|
||||||
|
self.use_ascii = use_ascii
|
||||||
|
|
||||||
def repr(self, x: object) -> str:
|
def repr(self, x: object) -> str:
|
||||||
try:
|
try:
|
||||||
|
if self.use_ascii:
|
||||||
|
return ascii(x)
|
||||||
|
|
||||||
s = super().repr(x)
|
s = super().repr(x)
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
raise
|
raise
|
||||||
|
@ -107,10 +111,7 @@ def saferepr(
|
||||||
stdlib.
|
stdlib.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if use_ascii:
|
return SafeRepr(maxsize, use_ascii).repr(obj)
|
||||||
obj = ascii(obj)
|
|
||||||
|
|
||||||
return SafeRepr(maxsize).repr(obj)
|
|
||||||
|
|
||||||
|
|
||||||
def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
|
def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
|
||||||
|
@ -126,7 +127,7 @@ def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if use_ascii:
|
if use_ascii:
|
||||||
obj = ascii(obj)
|
return ascii(obj)
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return _format_repr_exception(exc, obj)
|
return _format_repr_exception(exc, obj)
|
||||||
|
|
|
@ -163,6 +163,8 @@ def assertrepr_compare(
|
||||||
"""Return specialised explanations for some operators/operands."""
|
"""Return specialised explanations for some operators/operands."""
|
||||||
verbose = config.getoption("verbose")
|
verbose = config.getoption("verbose")
|
||||||
|
|
||||||
|
# Strings which normalize equal are often hard to distinguish when printed; use ascii() to make this easier.
|
||||||
|
# See issue #3246.
|
||||||
use_ascii = (
|
use_ascii = (
|
||||||
isinstance(left, str)
|
isinstance(left, str)
|
||||||
and isinstance(right, str)
|
and isinstance(right, str)
|
||||||
|
|
|
@ -782,14 +782,14 @@ class TestAssert_reprcompare:
|
||||||
right = "hyva\u0308"
|
right = "hyva\u0308"
|
||||||
expl = callequal(left, right)
|
expl = callequal(left, right)
|
||||||
assert expl == [
|
assert expl == [
|
||||||
"\"'hyv\\\\xe4'\" == \"'hyva\\\\u0308'\"",
|
r"'hyv\xe4' == 'hyva\u0308'",
|
||||||
f"- {str(right)}",
|
f"- {str(right)}",
|
||||||
f"+ {str(left)}",
|
f"+ {str(left)}",
|
||||||
]
|
]
|
||||||
|
|
||||||
expl = callequal(left, right, verbose=2)
|
expl = callequal(left, right, verbose=2)
|
||||||
assert expl == [
|
assert expl == [
|
||||||
"\"'hyv\\\\xe4'\" == \"'hyva\\\\u0308'\"",
|
r"'hyv\xe4' == 'hyva\u0308'",
|
||||||
f"- {str(right)}",
|
f"- {str(right)}",
|
||||||
f"+ {str(left)}",
|
f"+ {str(left)}",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue