Skip to content

Commit 004aefa

Browse files
committed
Catch BaseException from task code
1 parent bc11240 commit 004aefa

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎src/_pytask/execute.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def pytask_execute_task_protocol(session: Session, task: PTask) -> ExecutionRepo
122122
short_exc_info = remove_traceback_from_exc_info(sys.exc_info())
123123
report = ExecutionReport.from_task_and_exception(task, short_exc_info)
124124
session.should_stop = True
125-
except Exception: # noqa: BLE001
125+
except BaseException: # noqa: BLE001
126126
report = ExecutionReport.from_task_and_exception(task, sys.exc_info())
127127
else:
128128
report = ExecutionReport.from_task(task)

‎tests/test_execute.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pytest
1212

1313
import pytask
14+
from _pytask.mark import MARK_GEN
1415
from pytask import CaptureMethod
1516
from pytask import ExitCode
1617
from pytask import NodeNotFoundError
@@ -123,6 +124,45 @@ def task_3(paths = [Path("deleted.txt"), Path("out_2.txt")]):
123124
assert isinstance(exc_info[1], NodeNotFoundError)
124125

125126

127+
@pytest.mark.parametrize("exit_code", [0, 7])
128+
def test_system_exit_is_a_normal_task_failure(tmp_path, exit_code):
129+
source = f"""
130+
from pathlib import Path
131+
132+
def task_exits(produces=Path("upstream.txt")):
133+
raise SystemExit({exit_code})
134+
135+
def task_descendant(
136+
path=Path("upstream.txt"), produces=Path("descendant.txt")
137+
):
138+
produces.write_text(path.read_text())
139+
140+
def task_unrelated(produces=Path("unrelated.txt")):
141+
produces.write_text("unrelated")
142+
"""
143+
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))
144+
145+
session = build(paths=tmp_path)
146+
147+
assert session.exit_code == ExitCode.FAILED
148+
assert tmp_path.joinpath("unrelated.txt").read_text() == "unrelated"
149+
assert not tmp_path.joinpath("descendant.txt").exists()
150+
assert {report.outcome for report in session.execution_reports} == {
151+
TaskOutcome.FAIL,
152+
TaskOutcome.SKIP_PREVIOUS_FAILED,
153+
TaskOutcome.SUCCESS,
154+
}
155+
failed_report = next(
156+
report
157+
for report in session.execution_reports
158+
if report.outcome == TaskOutcome.FAIL
159+
)
160+
assert failed_report.exc_info is not None
161+
assert isinstance(failed_report.exc_info[1], SystemExit)
162+
assert failed_report.exc_info[1].code == exit_code
163+
assert MARK_GEN.config is None
164+
165+
126166
def test_depends_on_and_produces_can_be_used_in_task(tmp_path):
127167
source = """
128168
from pathlib import Path

0 commit comments

Comments
 (0)