11from __future__ import annotations
22
3- from collections .abc import Generator
4- import contextlib
53import gc
64import sys
75from unittest import mock
@@ -229,19 +227,13 @@ def _set_gc_state(enabled: bool) -> bool:
229227 return was_enabled
230228
231229
232- @contextlib .contextmanager
233- def _disable_gc () -> Generator [None ]:
234- was_enabled = _set_gc_state (enabled = False )
235- try :
236- yield
237- finally :
238- _set_gc_state (enabled = was_enabled )
239-
240-
241230def test_refcycle_unraisable (pytester : Pytester ) -> None :
242231 # see: https://github.com/pytest-dev/pytest/issues/10404
243232 pytester .makepyfile (
244233 test_it = """
234+ # Should catch the unraisable exception even if gc is disabled.
235+ import gc; gc.disable()
236+
245237 import pytest
246238
247239 class BrokenDel:
@@ -256,23 +248,22 @@ def test_it():
256248 """
257249 )
258250
259- with _disable_gc ():
260- result = pytester .runpytest ()
251+ result = pytester .runpytest_subprocess (
252+ "-Wdefault::pytest.PytestUnraisableExceptionWarning"
253+ )
261254
262- # TODO: should be a test failure or error
263- assert result .ret == pytest .ExitCode .INTERNAL_ERROR
255+ assert result .ret == 0
264256
265257 result .assert_outcomes (passed = 1 )
266258 result .stderr .fnmatch_lines ("ValueError: del is broken" )
267259
268260
269- @pytest .mark .filterwarnings ("default::pytest.PytestUnraisableExceptionWarning" )
270261def test_refcycle_unraisable_warning_filter (pytester : Pytester ) -> None :
271- # note that the host pytest warning filter is disabled and the pytester
272- # warning filter applies during config teardown of unraisablehook.
273- # see: https://github.com/pytest-dev/pytest/issues/10404
274262 pytester .makepyfile (
275263 test_it = """
264+ # Should catch the unraisable exception even if gc is disabled.
265+ import gc; gc.disable()
266+
276267 import pytest
277268
278269 class BrokenDel:
@@ -287,17 +278,18 @@ def test_it():
287278 """
288279 )
289280
290- with _disable_gc ():
291- result = pytester .runpytest ("-Werror" )
281+ result = pytester .runpytest_subprocess (
282+ "-Werror::pytest.PytestUnraisableExceptionWarning"
283+ )
292284
293- # TODO: should be a test failure or error
294- assert result .ret == pytest .ExitCode .INTERNAL_ERROR
285+ # TODO: Should be a test failure or error. Currently the exception
286+ # propagates all the way to the top resulting in exit code 1.
287+ assert result .ret == 1
295288
296289 result .assert_outcomes (passed = 1 )
297290 result .stderr .fnmatch_lines ("ValueError: del is broken" )
298291
299292
300- @pytest .mark .filterwarnings ("default::pytest.PytestUnraisableExceptionWarning" )
301293def test_create_task_raises_unraisable_warning_filter (pytester : Pytester ) -> None :
302294 # note that the host pytest warning filter is disabled and the pytester
303295 # warning filter applies during config teardown of unraisablehook.
@@ -306,6 +298,9 @@ def test_create_task_raises_unraisable_warning_filter(pytester: Pytester) -> Non
306298 # the issue
307299 pytester .makepyfile (
308300 test_it = """
301+ # Should catch the unraisable exception even if gc is disabled.
302+ import gc; gc.disable()
303+
309304 import asyncio
310305 import pytest
311306
@@ -318,11 +313,11 @@ def test_scheduler_must_be_created_within_running_loop() -> None:
318313 """
319314 )
320315
321- with _disable_gc ():
322- result = pytester .runpytest ("-Werror" )
316+ result = pytester .runpytest_subprocess ("-Werror" )
323317
324- # TODO: should be a test failure or error
325- assert result .ret == pytest .ExitCode .INTERNAL_ERROR
318+ # TODO: Should be a test failure or error. Currently the exception
319+ # propagates all the way to the top resulting in exit code 1.
320+ assert result .ret == 1
326321
327322 result .assert_outcomes (passed = 1 )
328323 result .stderr .fnmatch_lines ("RuntimeWarning: coroutine 'my_task' was never awaited" )
0 commit comments