Test that warnings issued from a thread pool can be caught by the main thread, even when the thread pool was already warm when entering `warnings.catch_warnings`. This also affects `@pytest.mark.filterwarnings` and similar mechanisms. This needs special handling on Python 3.14t, which u
(num_workers)
| 237 | |
| 238 | @pytest.mark.parametrize("num_workers", [None, 1]) |
| 239 | def test_context_aware_warnings(num_workers): |
| 240 | """Test that warnings issued from a thread pool can be caught by the main thread, |
| 241 | even when the thread pool was already warm when entering `warnings.catch_warnings`. |
| 242 | This also affects `@pytest.mark.filterwarnings` and similar mechanisms. |
| 243 | |
| 244 | This needs special handling on Python 3.14t, which uses context-aware warnings. |
| 245 | The system is also available opt-in on 3.14 (GIL-enabled) with |
| 246 | python -X context_aware_warnings=1 |
| 247 | """ |
| 248 | warm_up_thread_pool(num_workers) |
| 249 | |
| 250 | def warn(): |
| 251 | warnings.warn("Hello world", RuntimeWarning, stacklevel=2) |
| 252 | |
| 253 | with warnings.catch_warnings(record=True) as w: |
| 254 | warnings.simplefilter("always") |
| 255 | # Reuse thread pool started by warm_up_thread_pool |
| 256 | get({"x": (warn,)}, "x", num_workers=num_workers) |
| 257 | |
| 258 | assert len(w) == 1 |
| 259 | assert isinstance(w[0].message, RuntimeWarning) |
| 260 | assert str(w[0].message) == "Hello world" |
nothing calls this directly
no test coverage detected