|
40 | 40 | Callable,
|
41 | 41 | Generator,
|
42 | 42 | )
|
| 43 | +from exceptiongroup import catch |
43 | 44 |
|
44 | 45 | if sys.version_info < (3, 11):
|
45 | 46 | from exceptiongroup import BaseExceptionGroup, ExceptionGroup
|
@@ -1845,7 +1846,59 @@ async def no_checkpoint_before_started(
|
1845 | 1846 | raise AssertionError() from exc # pragma: no cover
|
1846 | 1847 | assert not cs.cancelled_caught
|
1847 | 1848 |
|
1848 |
| - # trying to start in a closed nursery raises an error immediately |
| 1849 | + # calling started() while handling a Cancelled raises an error immediately. |
| 1850 | + async def started_while_handling_cancelled( |
| 1851 | + task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED, |
| 1852 | + ) -> None: |
| 1853 | + try: |
| 1854 | + await _core.checkpoint() |
| 1855 | + except _core.Cancelled: |
| 1856 | + task_status.started() |
| 1857 | + raise AssertionError() # pragma: no cover |
| 1858 | + |
| 1859 | + async with _core.open_nursery() as nursery: |
| 1860 | + with _core.CancelScope() as cs: |
| 1861 | + cs.cancel() |
| 1862 | + with pytest.raises(RuntimeError): |
| 1863 | + await nursery.start(started_while_handling_cancelled) |
| 1864 | + |
| 1865 | + # calling started() while handling multiple Cancelleds raises an error |
| 1866 | + # immediately. |
| 1867 | + async def started_while_handling_multiple_cancelled( |
| 1868 | + task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED, |
| 1869 | + ) -> None: |
| 1870 | + with catch({_core.Cancelled: lambda _: task_status.started()}): |
| 1871 | + async with _core.open_nursery() as nursery: |
| 1872 | + nursery.start_soon(_core.checkpoint) |
| 1873 | + nursery.start_soon(_core.checkpoint) |
| 1874 | + raise AssertionError() # pragma: no cover |
| 1875 | + |
| 1876 | + async with _core.open_nursery() as nursery: |
| 1877 | + with _core.CancelScope() as cs: |
| 1878 | + cs.cancel() |
| 1879 | + with pytest.raises(RuntimeError): |
| 1880 | + await nursery.start(started_while_handling_multiple_cancelled) |
| 1881 | + |
| 1882 | + # calling started() while handling an exception while handling Cancelled(s) raises an error immediately. |
| 1883 | + async def started_while_handling_exc_while_handling_cancelled( |
| 1884 | + task_status: _core.TaskStatus[None] = _core.TASK_STATUS_IGNORED, |
| 1885 | + ) -> None: |
| 1886 | + try: |
| 1887 | + await _core.checkpoint() |
| 1888 | + except _core.Cancelled: |
| 1889 | + try: |
| 1890 | + raise ValueError |
| 1891 | + except ValueError: |
| 1892 | + task_status.started() |
| 1893 | + raise AssertionError() # pragma: no cover |
| 1894 | + |
| 1895 | + async with _core.open_nursery() as nursery: |
| 1896 | + with _core.CancelScope() as cs: |
| 1897 | + cs.cancel() |
| 1898 | + with pytest.raises(RuntimeError): |
| 1899 | + await nursery.start(started_while_handling_exc_while_handling_cancelled) |
| 1900 | + |
| 1901 | + # trying to start in a closed nursery raises an error immediately. |
1849 | 1902 | async with _core.open_nursery() as closed_nursery:
|
1850 | 1903 | pass
|
1851 | 1904 | t0 = _core.current_time()
|
|
0 commit comments