|
12 | 12 | import trio |
13 | 13 |
|
14 | 14 | from ._abc import AsyncResource, ReceiveStream, SendStream |
15 | | -from ._core import Cancelled, ClosedResourceError, TaskStatus, TrioInternalError |
| 15 | +from ._core import ClosedResourceError, TaskStatus |
16 | 16 | from ._deprecate import deprecated |
17 | 17 | from ._highlevel_generic import StapledStream |
18 | 18 | from ._subprocess_platform import ( |
|
30 | 30 |
|
31 | 31 | from typing_extensions import Self, TypeAlias |
32 | 32 |
|
33 | | -if sys.version_info < (3, 11): |
34 | | - from exceptiongroup import BaseExceptionGroup |
35 | | - |
36 | 33 |
|
37 | 34 | # Sphinx cannot parse the stringified version |
38 | 35 | if sys.version_info >= (3, 9): |
@@ -690,6 +687,7 @@ async def my_deliver_cancel(process): |
690 | 687 | and the process exits with a nonzero exit status |
691 | 688 | OSError: if an error is encountered starting or communicating with |
692 | 689 | the process |
| 690 | + ExceptionGroup: if exceptions occur in `deliver_cancel`, or when exceptions occur when communicating with the subprocess. If strict_exception_groups is set to false in the global context, then single exceptions will be collapsed. |
693 | 691 |
|
694 | 692 | .. note:: The child process runs in the same process group as the parent |
695 | 693 | Trio process, so a Ctrl+C will be delivered simultaneously to both |
@@ -767,41 +765,36 @@ async def read_output( |
767 | 765 | # so any exceptions get directly seen by users. |
768 | 766 | # options needs a complex TypedDict. The overload error only occurs on Unix. |
769 | 767 | proc = await open_process(command, **options) # type: ignore[arg-type, call-overload, unused-ignore] |
770 | | - try: |
771 | | - async with trio.open_nursery() as nursery: |
772 | | - try: |
773 | | - if input is not None: |
774 | | - assert proc.stdin is not None |
775 | | - nursery.start_soon(feed_input, proc.stdin) |
776 | | - proc.stdin = None |
777 | | - proc.stdio = None |
778 | | - if capture_stdout: |
779 | | - assert proc.stdout is not None |
780 | | - nursery.start_soon(read_output, proc.stdout, stdout_chunks) |
781 | | - proc.stdout = None |
782 | | - proc.stdio = None |
783 | | - if capture_stderr: |
784 | | - assert proc.stderr is not None |
785 | | - nursery.start_soon(read_output, proc.stderr, stderr_chunks) |
786 | | - proc.stderr = None |
787 | | - task_status.started(proc) |
| 768 | + async with trio.open_nursery() as nursery: |
| 769 | + try: |
| 770 | + if input is not None: |
| 771 | + assert proc.stdin is not None |
| 772 | + nursery.start_soon(feed_input, proc.stdin) |
| 773 | + proc.stdin = None |
| 774 | + proc.stdio = None |
| 775 | + if capture_stdout: |
| 776 | + assert proc.stdout is not None |
| 777 | + nursery.start_soon(read_output, proc.stdout, stdout_chunks) |
| 778 | + proc.stdout = None |
| 779 | + proc.stdio = None |
| 780 | + if capture_stderr: |
| 781 | + assert proc.stderr is not None |
| 782 | + nursery.start_soon(read_output, proc.stderr, stderr_chunks) |
| 783 | + proc.stderr = None |
| 784 | + task_status.started(proc) |
| 785 | + await proc.wait() |
| 786 | + except BaseException: |
| 787 | + with trio.CancelScope(shield=True): |
| 788 | + killer_cscope = trio.CancelScope(shield=True) |
| 789 | + |
| 790 | + async def killer() -> None: |
| 791 | + with killer_cscope: |
| 792 | + await deliver_cancel(proc) |
| 793 | + |
| 794 | + nursery.start_soon(killer) |
788 | 795 | await proc.wait() |
789 | | - except BaseException: |
790 | | - with trio.CancelScope(shield=True): |
791 | | - killer_cscope = trio.CancelScope(shield=True) |
792 | | - |
793 | | - async def killer() -> None: |
794 | | - with killer_cscope: |
795 | | - await deliver_cancel(proc) |
796 | | - |
797 | | - nursery.start_soon(killer) |
798 | | - await proc.wait() |
799 | | - killer_cscope.cancel() |
800 | | - raise |
801 | | - except BaseExceptionGroup as exc: |
802 | | - if all(isinstance(e, Cancelled) for e in exc.exceptions): |
803 | | - raise exc |
804 | | - raise TrioInternalError("Error interacting with opened process") from exc |
| 796 | + killer_cscope.cancel() |
| 797 | + raise |
805 | 798 |
|
806 | 799 | stdout = b"".join(stdout_chunks) if capture_stdout else None |
807 | 800 | stderr = b"".join(stderr_chunks) if capture_stderr else None |
|
0 commit comments