Ensure pytest.raises does not leave a reference cycle (#1965).
(self, method)
| 241 | "method", ["function", "function_match", "with", "with_raisesexc", "with_group"] |
| 242 | ) |
| 243 | def test_raises_cyclic_reference(self, method): |
| 244 | """Ensure pytest.raises does not leave a reference cycle (#1965).""" |
| 245 | import gc |
| 246 | |
| 247 | class T: |
| 248 | def __call__(self): |
| 249 | raise ValueError |
| 250 | |
| 251 | t = T() |
| 252 | refcount = len(gc.get_referrers(t)) |
| 253 | |
| 254 | if method == "function": |
| 255 | pytest.raises(ValueError, t) |
| 256 | elif method == "function_match": |
| 257 | pytest.raises(ValueError, t).match("^$") |
| 258 | elif method == "with": |
| 259 | with pytest.raises(ValueError): |
| 260 | t() |
| 261 | elif method == "with_raisesexc": |
| 262 | with pytest.RaisesExc(ValueError): |
| 263 | t() |
| 264 | elif method == "with_group": |
| 265 | with pytest.RaisesGroup(ValueError, allow_unwrapped=True): |
| 266 | t() |
| 267 | else: # pragma: no cover |
| 268 | raise AssertionError("bad parametrization") |
| 269 | |
| 270 | # ensure both forms of pytest.raises don't leave exceptions in sys.exc_info() |
| 271 | assert sys.exc_info() == (None, None, None) |
| 272 | |
| 273 | assert refcount == len(gc.get_referrers(t)) |
| 274 | |
| 275 | def test_raises_match(self) -> None: |
| 276 | msg = r"with base \d+" |