comparing ascii instead of bytes
This commit is contained in:
parent
ccaf006c2f
commit
bf7b80c3f1
|
@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
|
||||||
write_to = "src/_pytest/_version.py"
|
write_to = "src/_pytest/_version.py"
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
[tool.pytest.ini_options]
|
||||||
minversion = "2.0"
|
#minversion = "2.0"
|
||||||
addopts = "-rfEX -p pytester --strict-markers"
|
addopts = "-rfEX -p pytester --strict-markers"
|
||||||
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
|
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
|
||||||
python_classes = ["Test", "Acceptance"]
|
python_classes = ["Test", "Acceptance"]
|
||||||
|
|
|
@ -94,7 +94,7 @@ def safeformat(obj: object) -> str:
|
||||||
DEFAULT_REPR_MAX_SIZE = 240
|
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.
|
"""Return a size-limited safe repr-string for the given object.
|
||||||
|
|
||||||
Failing __repr__ functions of user instances will be represented
|
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
|
This function is a wrapper around the Repr/reprlib functionality of the
|
||||||
stdlib.
|
stdlib.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if use_ascii:
|
||||||
|
obj = ascii(obj)
|
||||||
|
|
||||||
return SafeRepr(maxsize).repr(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.
|
"""Return an unlimited-size safe repr-string for the given object.
|
||||||
|
|
||||||
As with saferepr, failing __repr__ functions of user instances
|
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.
|
when maxsize=None, but that might affect some other code.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
|
if use_ascii:
|
||||||
|
obj = 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)
|
||||||
|
|
|
@ -157,20 +157,24 @@ def has_default_eq(
|
||||||
return True
|
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."""
|
"""Return specialised explanations for some operators/operands."""
|
||||||
verbose = config.getoption("verbose")
|
verbose = config.getoption("verbose")
|
||||||
|
|
||||||
|
use_ascii = isinstance(left, str) and isinstance(right, str) and normalize("NFD", left) == normalize("NFD", right)
|
||||||
|
|
||||||
if verbose > 1:
|
if verbose > 1:
|
||||||
left_repr = saferepr_unlimited(left)
|
left_repr = saferepr_unlimited(left, use_ascii=use_ascii)
|
||||||
right_repr = saferepr_unlimited(right)
|
right_repr = saferepr_unlimited(right, use_ascii=use_ascii)
|
||||||
else:
|
else:
|
||||||
# XXX: "15 chars indentation" is wrong
|
# XXX: "15 chars indentation" is wrong
|
||||||
# ("E AssertionError: assert "); should use term width.
|
# ("E AssertionError: assert "); should use term width.
|
||||||
maxsize = (
|
maxsize = (
|
||||||
80 - 15 - len(op) - 2
|
80 - 15 - len(op) - 2
|
||||||
) // 2 # 15 chars indentation, 1 space around op
|
) // 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}"
|
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] = []
|
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:
|
if verbose < 1:
|
||||||
i = 0 # just in case left or right has zero length
|
i = 0 # just in case left or right has zero length
|
||||||
for i in range(min(len(left), len(right))):
|
for i in range(min(len(left), len(right))):
|
||||||
|
|
|
@ -781,11 +781,7 @@ class TestAssert_reprcompare:
|
||||||
left = "hyv\xe4"
|
left = "hyv\xe4"
|
||||||
right = "hyva\u0308"
|
right = "hyva\u0308"
|
||||||
expl = callequal(left, right)
|
expl = callequal(left, right)
|
||||||
assert expl == [
|
assert expl == ['"\'hyv\\\\xe4\'" == "\'hyva\\\\u0308\'"', f'- {str(right)}', f'+ {str(left)}']
|
||||||
"'hyvä' == 'hyvä'",
|
|
||||||
"Strings are different but normalize to the same string. Comparing their utf-8 encoding.",
|
|
||||||
"At index 3 diff: b'\\xc3' != b'a'",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class TestAssert_reprcompare_dataclass:
|
class TestAssert_reprcompare_dataclass:
|
||||||
|
|
Loading…
Reference in New Issue