MCPcopy
hub / github.com/pytest-dev/pytest / test_baseexception

Function test_baseexception

testing/io/test_saferepr.py:69–127  ·  view source on GitHub ↗

Test saferepr() with BaseExceptions, which includes pytest outcomes.

()

Source from the content-addressed store, hash-verified

67
68
69def test_baseexception():
70 """Test saferepr() with BaseExceptions, which includes pytest outcomes."""
71
72 class RaisingOnStrRepr(BaseException):
73 def __init__(self, exc_types):
74 self.exc_types = exc_types
75
76 def raise_exc(self, *args):
77 try:
78 self.exc_type = self.exc_types.pop(0)
79 except IndexError:
80 pass
81 if hasattr(self.exc_type, "__call__"):
82 raise self.exc_type(*args)
83 raise self.exc_type
84
85 def __str__(self): # noqa: PLE0307
86 self.raise_exc("__str__")
87
88 def __repr__(self):
89 self.raise_exc("__repr__")
90
91 class BrokenObj:
92 def __init__(self, exc):
93 self.exc = exc
94
95 def __repr__(self):
96 raise self.exc
97
98 __str__ = __repr__
99
100 baseexc_str = BaseException("__str__")
101 obj = BrokenObj(RaisingOnStrRepr([BaseException]))
102 assert saferepr(obj) == (
103 f"<[unpresentable exception ({baseexc_str!r}) "
104 f"raised in repr()] BrokenObj object at 0x{id(obj):x}>"
105 )
106 obj = BrokenObj(RaisingOnStrRepr([RaisingOnStrRepr([BaseException])]))
107 assert saferepr(obj) == (
108 f"<[{baseexc_str!r} raised in repr()] BrokenObj object at 0x{id(obj):x}>"
109 )
110
111 with pytest.raises(KeyboardInterrupt):
112 saferepr(BrokenObj(KeyboardInterrupt()))
113
114 with pytest.raises(SystemExit):
115 saferepr(BrokenObj(SystemExit()))
116
117 with pytest.raises(KeyboardInterrupt):
118 saferepr(BrokenObj(RaisingOnStrRepr([KeyboardInterrupt])))
119
120 with pytest.raises(SystemExit):
121 saferepr(BrokenObj(RaisingOnStrRepr([SystemExit])))
122
123 with pytest.raises(KeyboardInterrupt):
124 print(saferepr(BrokenObj(RaisingOnStrRepr([BaseException, KeyboardInterrupt]))))
125
126 with pytest.raises(SystemExit):

Callers

nothing calls this directly

Calls 3

safereprFunction · 0.90
BrokenObjClass · 0.85
RaisingOnStrReprClass · 0.85

Tested by

no test coverage detected