MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_baseexception

Function test_baseexception

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

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

()

Source from the content-addressed store, hash-verified

65
66
67def test_baseexception():
68 """Test saferepr() with BaseExceptions, which includes pytest outcomes."""
69
70 class RaisingOnStrRepr(BaseException):
71 def __init__(self, exc_types):
72 self.exc_types = exc_types
73
74 def raise_exc(self, *args):
75 try:
76 self.exc_type = self.exc_types.pop(0)
77 except IndexError:
78 pass
79 if hasattr(self.exc_type, "__call__"):
80 raise self.exc_type(*args)
81 raise self.exc_type
82
83 def __str__(self):
84 self.raise_exc("__str__")
85
86 def __repr__(self):
87 self.raise_exc("__repr__")
88
89 class BrokenObj:
90 def __init__(self, exc):
91 self.exc = exc
92
93 def __repr__(self):
94 raise self.exc
95
96 __str__ = __repr__
97
98 baseexc_str = BaseException("__str__")
99 obj = BrokenObj(RaisingOnStrRepr([BaseException]))
100 assert saferepr(obj) == (
101 "<[unpresentable exception ({!r}) "
102 "raised in repr()] BrokenObj object at 0x{:x}>".format(baseexc_str, id(obj))
103 )
104 obj = BrokenObj(RaisingOnStrRepr([RaisingOnStrRepr([BaseException])]))
105 assert saferepr(obj) == (
106 "<[{!r} raised in repr()] BrokenObj object at 0x{:x}>".format(
107 baseexc_str, id(obj)
108 )
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]))))

Callers

nothing calls this directly

Calls 4

safereprFunction · 0.90
BrokenObjClass · 0.85
RaisingOnStrReprClass · 0.85
formatMethod · 0.45

Tested by

no test coverage detected