comparing ascii instead of bytes

This commit is contained in:
Itxaso Aizpurua 2022-10-08 08:17:48 +02:00
parent ccaf006c2f
commit bf7b80c3f1
4 changed files with 19 additions and 22 deletions

View File

@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
write_to = "src/_pytest/_version.py"
[tool.pytest.ini_options]
minversion = "2.0"
#minversion = "2.0"
addopts = "-rfEX -p pytester --strict-markers"
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
python_classes = ["Test", "Acceptance"]

View File

@ -94,7 +94,7 @@ def safeformat(obj: object) -> str:
DEFAULT_REPR_MAX_SIZE = 240
def saferepr(obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE) -> str:
def saferepr(obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE, use_ascii: bool = False) -> str:
"""Return a size-limited safe repr-string for the given object.
Failing __repr__ functions of user instances will be represented
@ -104,10 +104,14 @@ def saferepr(obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE) -> str
This function is a wrapper around the Repr/reprlib functionality of the
stdlib.
"""
if use_ascii:
obj = ascii(obj)
return SafeRepr(maxsize).repr(obj)
def saferepr_unlimited(obj: object) -> str:
def saferepr_unlimited(obj: object, use_ascii: bool = True) -> str:
"""Return an unlimited-size safe repr-string for the given object.
As with saferepr, failing __repr__ functions of user instances
@ -119,6 +123,8 @@ def saferepr_unlimited(obj: object) -> str:
when maxsize=None, but that might affect some other code.
"""
try:
if use_ascii:
obj = ascii(obj)
return repr(obj)
except Exception as exc:
return _format_repr_exception(exc, obj)

View File

@ -157,20 +157,24 @@ def has_default_eq(
return True
def assertrepr_compare(config, op: str, left: Any, right: Any) -> Optional[List[str]]:
def assertrepr_compare(config, op: str, left: Any, right: Any, use_ascii: bool=False) -> Optional[List[str]]:
"""Return specialised explanations for some operators/operands."""
verbose = config.getoption("verbose")
use_ascii = isinstance(left, str) and isinstance(right, str) and normalize("NFD", left) == normalize("NFD", right)
if verbose > 1:
left_repr = saferepr_unlimited(left)
right_repr = saferepr_unlimited(right)
left_repr = saferepr_unlimited(left, use_ascii=use_ascii)
right_repr = saferepr_unlimited(right, use_ascii=use_ascii)
else:
# XXX: "15 chars indentation" is wrong
# ("E AssertionError: assert "); should use term width.
maxsize = (
80 - 15 - len(op) - 2
) // 2 # 15 chars indentation, 1 space around op
left_repr = saferepr(left, maxsize=maxsize)
right_repr = saferepr(right, maxsize=maxsize)
left_repr = saferepr(left, maxsize=maxsize, use_ascii=use_ascii)
right_repr = saferepr(right, maxsize=maxsize, use_ascii=use_ascii)
summary = f"{left_repr} {op} {right_repr}"
@ -242,15 +246,6 @@ def _diff_text(left: str, right: str, verbose: int = 0) -> List[str]:
explanation: List[str] = []
# if both are strings are truthy and normalize to the same string, but we're here,
# their byte representation is different. We convert to utf-8 and compare them as bytes.
if left and right and normalize("NFD", left) == normalize("NFD", right):
return [
"Strings are different but normalize to the same string. Comparing their utf-8 encoding.",
*_compare_eq_sequence(left.encode(), right.encode()),
]
if verbose < 1:
i = 0 # just in case left or right has zero length
for i in range(min(len(left), len(right))):

View File

@ -781,11 +781,7 @@ class TestAssert_reprcompare:
left = "hyv\xe4"
right = "hyva\u0308"
expl = callequal(left, right)
assert expl == [
"'hyvä' == 'hyvä'",
"Strings are different but normalize to the same string. Comparing their utf-8 encoding.",
"At index 3 diff: b'\\xc3' != b'a'",
]
assert expl == ['"\'hyv\\\\xe4\'" == "\'hyva\\\\u0308\'"', f'- {str(right)}', f'+ {str(left)}']
class TestAssert_reprcompare_dataclass: