44
55import functools
66import os
7- import sys
87import warnings
98from contextlib import redirect_stderr
109from contextlib import redirect_stdout
4241
4342 from pytask import Mark
4443 from rich .console import ConsoleOptions
44+ from rich .traceback import Trace
4545
4646
4747__all__ = ["wrap_task_in_process" , "wrap_task_in_thread" ]
@@ -52,7 +52,8 @@ class WrapperResult:
5252 carry_over_products : PyTree [CarryOverPath | PythonNode | None ]
5353 warning_reports : list [WarningReport ]
5454 exc_info : (
55- tuple [type [BaseException ], BaseException , TracebackType | str | None ] | None
55+ tuple [type [BaseException ], BaseException , TracebackType | Trace | str | None ]
56+ | None
5657 )
5758 stdout : str
5859 stderr : str
@@ -69,16 +70,8 @@ def wrap_task_in_thread(task: PTask, *, remote: bool, **kwargs: Any) -> WrapperR
6970 __tracebackhide__ = True
7071 try :
7172 out = task .function (** kwargs )
72- except Exception : # noqa: BLE001
73- exc_info_raw = sys .exc_info ()
74- exc_info = (
75- cast (
76- "tuple[type[BaseException], BaseException, TracebackType | str | None]" ,
77- exc_info_raw ,
78- )
79- if exc_info_raw [0 ] is not None
80- else None
81- )
73+ except Exception as exc : # noqa: BLE001
74+ exc_info = (type (exc ), exc , exc .__traceback__ )
8275 else :
8376 _handle_function_products (task , out , remote = remote )
8477 exc_info = None
@@ -94,7 +87,7 @@ def wrap_task_in_thread(task: PTask, *, remote: bool, **kwargs: Any) -> WrapperR
9487def wrap_task_in_process ( # noqa: PLR0913
9588 task : PTask ,
9689 * ,
97- console_options : ConsoleOptions ,
90+ console_options : ConsoleOptions | None ,
9891 kwargs : dict [str , Any ],
9992 remote : bool ,
10093 session_filterwarnings : tuple [str , ...],
@@ -103,8 +96,8 @@ def wrap_task_in_process( # noqa: PLR0913
10396) -> WrapperResult :
10497 """Execute a task in a spawned process.
10598
106- This function receives bytes and unpickles them to a task which is them execute in a
107- spawned process or thread .
99+ When console options are supplied, render tracebacks before returning them.
100+ Otherwise, return a Rich trace for the caller to render .
108101
109102 """
110103 # Hide this function from tracebacks.
@@ -131,22 +124,20 @@ def wrap_task_in_process( # noqa: PLR0913
131124 for arg in mark .args :
132125 warnings .filterwarnings (* parse_warning_filter (arg , escape = False ))
133126
134- processed_exc_info : tuple [type [BaseException ], BaseException , str ] | None
135-
136127 try :
137128 resolved_kwargs = _write_local_files_to_remote (kwargs )
138129 out = task .execute (** resolved_kwargs )
139- except Exception : # noqa: BLE001
140- exc_info = sys .exc_info ()
141- processed_exc_info = _render_traceback_to_string (
142- (
143- cast ("type[BaseException]" , exc_info [0 ]),
144- cast ("BaseException" , exc_info [1 ]),
145- exc_info [2 ],
146- ),
147- show_locals ,
148- console_options ,
130+ except Exception as exc : # noqa: BLE001
131+ exc_type = type (exc )
132+ traceback = Traceback (
133+ (exc_type , exc , exc .__traceback__ ), show_locals = show_locals
134+ )
135+ transported_traceback = (
136+ traceback .extract ()
137+ if console_options is None
138+ else _render_traceback_to_string (traceback , console_options )
149139 )
140+ processed_exc_info = (exc_type , exc , transported_traceback )
150141 products = None
151142 else :
152143 products = _handle_function_products (task , out , remote = remote )
@@ -216,15 +207,12 @@ def _patch_set_trace_and_breakpoint() -> None:
216207
217208
218209def _render_traceback_to_string (
219- exc_info : tuple [type [BaseException ], BaseException , TracebackType | None ],
220- show_locals : bool , # noqa: FBT001
210+ traceback : Traceback ,
221211 console_options : ConsoleOptions ,
222- ) -> tuple [type [BaseException ], BaseException , str ]:
223- """Process the exception and convert the traceback to a string."""
224- traceback = Traceback (exc_info , show_locals = show_locals )
212+ ) -> str :
213+ """Render a traceback while the worker still has access to its source files."""
225214 segments = console .render (cast ("Any" , traceback ), options = console_options )
226- text = "" .join (segment .text for segment in segments )
227- return exc_info [0 ], exc_info [1 ], text
215+ return "" .join (segment .text for segment in segments )
228216
229217
230218def _handle_function_products (
0 commit comments