From bf7b80c3f1bbe5b2513cdfd07011d70f5677074c Mon Sep 17 00:00:00 2001 From: Itxaso Aizpurua Date: Sat, 8 Oct 2022 08:17:48 +0200 Subject: [PATCH] comparing ascii instead of bytes --- pyproject.toml | 2 +- src/_pytest/_io/saferepr.py | 10 ++++++++-- src/_pytest/assertion/util.py | 23 +++++++++-------------- testing/test_assertion.py | 6 +----- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index fc9a119f6..40e18be41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"] diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index a27e8c2a6..07159e7b3 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -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) diff --git a/src/_pytest/assertion/util.py b/src/_pytest/assertion/util.py index 7f83767a0..a854f7171 100644 --- a/src/_pytest/assertion/util.py +++ b/src/_pytest/assertion/util.py @@ -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))): diff --git a/testing/test_assertion.py b/testing/test_assertion.py index 1fc65a80a..d0f266769 100644 --- a/testing/test_assertion.py +++ b/testing/test_assertion.py @@ -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: