2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2021-03-03 08:47:37 +08:00
|
|
|
from _pytest._io.saferepr import DEFAULT_REPR_MAX_SIZE
|
2019-01-21 02:50:18 +08:00
|
|
|
from _pytest._io.saferepr import saferepr
|
2022-03-19 19:55:39 +08:00
|
|
|
from _pytest._io.saferepr import saferepr_unlimited
|
2019-10-23 12:59:18 +08:00
|
|
|
import pytest
|
2019-01-21 02:40:20 +08:00
|
|
|
|
|
|
|
|
2019-01-21 02:50:18 +08:00
|
|
|
def test_simple_repr():
|
|
|
|
assert saferepr(1) == "1"
|
|
|
|
assert saferepr(None) == "None"
|
|
|
|
|
|
|
|
|
|
|
|
def test_maxsize():
|
|
|
|
s = saferepr("x" * 50, maxsize=25)
|
|
|
|
assert len(s) == 25
|
|
|
|
expected = repr("x" * 10 + "..." + "x" * 10)
|
|
|
|
assert s == expected
|
|
|
|
|
|
|
|
|
2021-03-03 08:47:37 +08:00
|
|
|
def test_no_maxsize():
|
|
|
|
text = "x" * DEFAULT_REPR_MAX_SIZE * 10
|
|
|
|
s = saferepr(text, maxsize=None)
|
|
|
|
expected = repr(text)
|
|
|
|
assert s == expected
|
|
|
|
|
|
|
|
|
2019-01-21 02:50:18 +08:00
|
|
|
def test_maxsize_error_on_instance():
|
|
|
|
class A:
|
2019-06-07 00:13:02 +08:00
|
|
|
def __repr__(self):
|
2019-01-21 02:50:18 +08:00
|
|
|
raise ValueError("...")
|
|
|
|
|
|
|
|
s = saferepr(("*" * 50, A()), maxsize=25)
|
|
|
|
assert len(s) == 25
|
|
|
|
assert s[0] == "(" and s[-1] == ")"
|
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_exceptions() -> None:
|
2019-01-21 02:50:18 +08:00
|
|
|
class BrokenRepr:
|
|
|
|
def __init__(self, ex):
|
|
|
|
self.ex = ex
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
raise self.ex
|
|
|
|
|
|
|
|
class BrokenReprException(Exception):
|
2020-07-10 14:44:14 +08:00
|
|
|
__str__ = None # type: ignore[assignment]
|
|
|
|
__repr__ = None # type: ignore[assignment]
|
2019-01-21 02:50:18 +08:00
|
|
|
|
|
|
|
assert "Exception" in saferepr(BrokenRepr(Exception("broken")))
|
|
|
|
s = saferepr(BrokenReprException("really broken"))
|
|
|
|
assert "TypeError" in s
|
|
|
|
assert "TypeError" in saferepr(BrokenRepr("string"))
|
|
|
|
|
2019-11-06 21:10:20 +08:00
|
|
|
none = None
|
2019-10-23 12:59:18 +08:00
|
|
|
try:
|
2020-07-10 14:44:14 +08:00
|
|
|
none() # type: ignore[misc]
|
2019-11-06 21:10:20 +08:00
|
|
|
except BaseException as exc:
|
2019-10-23 12:59:18 +08:00
|
|
|
exp_exc = repr(exc)
|
|
|
|
obj = BrokenRepr(BrokenReprException("omg even worse"))
|
|
|
|
s2 = saferepr(obj)
|
|
|
|
assert s2 == (
|
|
|
|
f"<[unpresentable exception ({exp_exc!s}) raised in repr()] BrokenRepr object at 0x{id(obj):x}>"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_baseexception():
|
|
|
|
"""Test saferepr() with BaseExceptions, which includes pytest outcomes."""
|
|
|
|
|
|
|
|
class RaisingOnStrRepr(BaseException):
|
|
|
|
def __init__(self, exc_types):
|
|
|
|
self.exc_types = exc_types
|
|
|
|
|
|
|
|
def raise_exc(self, *args):
|
|
|
|
try:
|
|
|
|
self.exc_type = self.exc_types.pop(0)
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
if hasattr(self.exc_type, "__call__"):
|
|
|
|
raise self.exc_type(*args)
|
|
|
|
raise self.exc_type
|
|
|
|
|
2024-04-23 22:59:12 +08:00
|
|
|
def __str__(self): # noqa: PLE0307
|
2019-10-23 12:59:18 +08:00
|
|
|
self.raise_exc("__str__")
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
self.raise_exc("__repr__")
|
|
|
|
|
|
|
|
class BrokenObj:
|
|
|
|
def __init__(self, exc):
|
|
|
|
self.exc = exc
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
raise self.exc
|
|
|
|
|
|
|
|
__str__ = __repr__
|
|
|
|
|
|
|
|
baseexc_str = BaseException("__str__")
|
|
|
|
obj = BrokenObj(RaisingOnStrRepr([BaseException]))
|
|
|
|
assert saferepr(obj) == (
|
|
|
|
f"<[unpresentable exception ({baseexc_str!r}) "
|
|
|
|
f"raised in repr()] BrokenObj object at 0x{id(obj):x}>"
|
|
|
|
)
|
|
|
|
obj = BrokenObj(RaisingOnStrRepr([RaisingOnStrRepr([BaseException])]))
|
|
|
|
assert saferepr(obj) == (
|
|
|
|
f"<[{baseexc_str!r} raised in repr()] BrokenObj object at 0x{id(obj):x}>"
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
|
|
|
saferepr(BrokenObj(KeyboardInterrupt()))
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
saferepr(BrokenObj(SystemExit()))
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
|
|
|
saferepr(BrokenObj(RaisingOnStrRepr([KeyboardInterrupt])))
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
saferepr(BrokenObj(RaisingOnStrRepr([SystemExit])))
|
|
|
|
|
|
|
|
with pytest.raises(KeyboardInterrupt):
|
|
|
|
print(saferepr(BrokenObj(RaisingOnStrRepr([BaseException, KeyboardInterrupt]))))
|
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
saferepr(BrokenObj(RaisingOnStrRepr([BaseException, SystemExit])))
|
2019-01-21 02:50:18 +08:00
|
|
|
|
|
|
|
|
2019-07-15 03:24:12 +08:00
|
|
|
def test_buggy_builtin_repr():
|
|
|
|
# Simulate a case where a repr for a builtin raises.
|
|
|
|
# reprlib dispatches by type name, so use "int".
|
|
|
|
|
|
|
|
class int:
|
|
|
|
def __repr__(self):
|
|
|
|
raise ValueError("Buggy repr!")
|
|
|
|
|
|
|
|
assert "Buggy" in saferepr(int())
|
|
|
|
|
|
|
|
|
2019-01-21 02:50:18 +08:00
|
|
|
def test_big_repr():
|
|
|
|
from _pytest._io.saferepr import SafeRepr
|
|
|
|
|
2019-07-15 02:50:59 +08:00
|
|
|
assert len(saferepr(range(1000))) <= len("[" + SafeRepr(0).maxlist * "1000" + "]")
|
2019-01-21 02:50:18 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_repr_on_newstyle() -> None:
|
2019-01-21 02:50:18 +08:00
|
|
|
class Function:
|
|
|
|
def __repr__(self):
|
2020-07-10 14:44:14 +08:00
|
|
|
return f"<{self.name}>" # type: ignore[attr-defined]
|
2019-01-21 02:50:18 +08:00
|
|
|
|
|
|
|
assert saferepr(Function())
|
|
|
|
|
|
|
|
|
|
|
|
def test_unicode():
|
|
|
|
val = "£€"
|
|
|
|
reprval = "'£€'"
|
|
|
|
assert saferepr(val) == reprval
|
2020-02-12 23:07:57 +08:00
|
|
|
|
|
|
|
|
2020-05-06 06:20:34 +08:00
|
|
|
def test_broken_getattribute():
|
|
|
|
"""saferepr() can create proper representations of classes with
|
|
|
|
broken __getattribute__ (#7145)
|
|
|
|
"""
|
|
|
|
|
|
|
|
class SomeClass:
|
|
|
|
def __getattribute__(self, attr):
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
assert saferepr(SomeClass()).startswith(
|
|
|
|
"<[RuntimeError() raised in repr()] SomeClass object at 0x"
|
|
|
|
)
|
2022-03-19 19:55:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_saferepr_unlimited():
|
|
|
|
dict5 = {f"v{i}": i for i in range(5)}
|
|
|
|
assert saferepr_unlimited(dict5) == "{'v0': 0, 'v1': 1, 'v2': 2, 'v3': 3, 'v4': 4}"
|
|
|
|
|
|
|
|
dict_long = {f"v{i}": i for i in range(1_000)}
|
|
|
|
r = saferepr_unlimited(dict_long)
|
|
|
|
assert "..." not in r
|
|
|
|
assert "\n" not in r
|
|
|
|
|
|
|
|
|
|
|
|
def test_saferepr_unlimited_exc():
|
|
|
|
class A:
|
|
|
|
def __repr__(self):
|
|
|
|
raise ValueError(42)
|
|
|
|
|
|
|
|
assert saferepr_unlimited(A()).startswith(
|
|
|
|
"<[ValueError(42) raised in repr()] A object at 0x"
|
|
|
|
)
|