Skip to content

Commit

Permalink
Backport #580 to v0.4.7. (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasraabe authored Mar 19, 2024
1 parent 19325a1 commit 72f7289
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ chronological order. Releases follow [semantic versioning](https://semver.org/)
releases are available on [PyPI](https://pypi.org/project/pytask) and
[Anaconda.org](https://anaconda.org/conda-forge/pytask).

## 0.4.7 - 2024-03-19

- {pull}`580` is a backport of {pull}`579`.

## 0.4.6 - 2024-03-13

- {pull}`576` fixes accidentally collecting `pytask.MarkGenerator` when using
Expand Down
6 changes: 4 additions & 2 deletions src/_pytask/debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def wrapper(*args: Any, **kwargs: Any) -> None:
capman = session.config["pm"].get_plugin("capturemanager")
live_manager = session.config["pm"].get_plugin("live_manager")
try:
task_function(*args, **kwargs)
return task_function(*args, **kwargs)

except Exception:
# Order is important! Pausing the live object before the capturemanager
Expand Down Expand Up @@ -409,11 +409,13 @@ def wrapper(*args: Any, **kwargs: Any) -> None:
console.rule("Captured stderr", style="default")
console.print(err)

_pdb.runcall(task_function, *args, **kwargs)
out = _pdb.runcall(task_function, *args, **kwargs)

live_manager.resume()
capman.resume()

return out

task.function = wrapper


Expand Down
47 changes: 43 additions & 4 deletions tests/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
IS_PEXPECT_INSTALLED = True


def _escape_ansi(line):
"""Escape ANSI sequences produced by rich."""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)


@pytest.mark.unit()
@pytest.mark.parametrize(
("value", "expected", "expectation"),
Expand Down Expand Up @@ -487,7 +493,40 @@ def test_function():
_flush(child)


def _escape_ansi(line):
"""Escape ANSI sequences produced by rich."""
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)
@pytest.mark.end_to_end()
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
def test_pdb_with_task_that_returns(tmp_path, runner):
source = """
from typing_extensions import Annotated
from pathlib import Path
def task_example() -> Annotated[str, Path("data.txt")]:
return "1"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix(), "--pdb"])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("data.txt").read_text() == "1"


@pytest.mark.end_to_end()
@pytest.mark.skipif(not IS_PEXPECT_INSTALLED, reason="pexpect is not installed.")
@pytest.mark.skipif(sys.platform == "win32", reason="pexpect cannot spawn on Windows.")
def test_trace_with_task_that_returns(tmp_path):
source = """
from typing_extensions import Annotated
from pathlib import Path
def task_example() -> Annotated[str, Path("data.txt")]:
return "1"
"""
tmp_path.joinpath("task_module.py").write_text(textwrap.dedent(source))

child = pexpect.spawn(f"pytask {tmp_path.as_posix()}")
child.sendline("c")
rest = child.read().decode("utf8")
assert "1 Succeeded" in _escape_ansi(rest)
assert tmp_path.joinpath("data.txt").read_text() == "1"
_flush(child)

0 comments on commit 72f7289

Please sign in to comment.