Ensure pytest.raises does not leave a reference cycle (#1965).
(self, method)
| 158 | |
| 159 | @pytest.mark.parametrize("method", ["function", "function_match", "with"]) |
| 160 | def test_raises_cyclic_reference(self, method): |
| 161 | """Ensure pytest.raises does not leave a reference cycle (#1965).""" |
| 162 | import gc |
| 163 | |
| 164 | class T: |
| 165 | def __call__(self): |
| 166 | raise ValueError |
| 167 | |
| 168 | t = T() |
| 169 | refcount = len(gc.get_referrers(t)) |
| 170 | |
| 171 | if method == "function": |
| 172 | pytest.raises(ValueError, t) |
| 173 | elif method == "function_match": |
| 174 | pytest.raises(ValueError, t).match("^$") |
| 175 | else: |
| 176 | with pytest.raises(ValueError): |
| 177 | t() |
| 178 | |
| 179 | # ensure both forms of pytest.raises don't leave exceptions in sys.exc_info() |
| 180 | assert sys.exc_info() == (None, None, None) |
| 181 | |
| 182 | assert refcount == len(gc.get_referrers(t)) |
| 183 | |
| 184 | def test_raises_match(self) -> None: |
| 185 | msg = r"with base \d+" |