Skip to content

Commit 9cd9f96

Browse files
committed
Cleanup capture.py and enable all debugging tests.
1 parent 6a992da commit 9cd9f96

File tree

3 files changed

+38
-44
lines changed

3 files changed

+38
-44
lines changed

src/_pytask/capture.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -783,40 +783,40 @@ class CaptureManager:
783783

784784
def __init__(self, method: "_CaptureMethod") -> None:
785785
self._method = method
786-
self._global_capturing: Optional[MultiCapture[str]] = None
786+
self._capturing: Optional[MultiCapture[str]] = None
787787

788788
def __repr__(self) -> str:
789-
return ("<CaptureManager _method={!r} _global_capturing={!r}>").format(
790-
self._method, self._global_capturing
789+
return ("<CaptureManager _method={!r} _capturing={!r}>").format(
790+
self._method, self._capturing
791791
)
792792

793793
def is_capturing(self) -> Union[str, bool]:
794794
return self._method != "no"
795795

796796
def start_capturing(self) -> None:
797-
assert self._global_capturing is None
798-
self._global_capturing = _get_multicapture(self._method)
799-
self._global_capturing.start_capturing()
797+
assert self._capturing is None
798+
self._capturing = _get_multicapture(self._method)
799+
self._capturing.start_capturing()
800800

801801
def stop_capturing(self) -> None:
802-
if self._global_capturing is not None:
803-
self._global_capturing.pop_outerr_to_orig()
804-
self._global_capturing.stop_capturing()
805-
self._global_capturing = None
802+
if self._capturing is not None:
803+
self._capturing.pop_outerr_to_orig()
804+
self._capturing.stop_capturing()
805+
self._capturing = None
806806

807807
def resume(self) -> None:
808808
# During teardown of the python process, and on rare occasions, capture
809809
# attributes can be `None` while trying to resume global capture.
810-
if self._global_capturing is not None:
811-
self._global_capturing.resume_capturing()
810+
if self._capturing is not None:
811+
self._capturing.resume_capturing()
812812

813813
def suspend(self, in_: bool = False) -> None:
814-
if self._global_capturing is not None:
815-
self._global_capturing.suspend_capturing(in_=in_)
814+
if self._capturing is not None:
815+
self._capturing.suspend_capturing(in_=in_)
816816

817817
def read(self) -> CaptureResult[str]:
818-
assert self._global_capturing is not None
819-
return self._global_capturing.readouterr()
818+
assert self._capturing is not None
819+
return self._capturing.readouterr()
820820

821821
# Helper context managers
822822

src/_pytask/debugging.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,11 @@ def do_continue(self, arg):
207207
capman = self._pytask_capman
208208
capturing = PytaskPDB._is_capturing(capman)
209209
if capturing:
210-
if capturing == "global":
211-
console.rule(
212-
"PDB continue (IO-capturing resumed)",
213-
characters=">",
214-
style=None,
215-
)
216-
else:
217-
console.rule(
218-
f"PDB continue (IO-capturing resumed for {capturing})",
219-
characters=">",
220-
style=None,
221-
)
210+
console.rule(
211+
"PDB continue (IO-capturing resumed)",
212+
characters=">",
213+
style=None,
214+
)
222215
assert capman is not None
223216
capman.resume()
224217
else:
@@ -293,18 +286,12 @@ def _init_pdb(cls, method, *args, **kwargs):
293286
console.rule(header, characters=">", style=None)
294287
else:
295288
capturing = cls._is_capturing(capman)
296-
if capturing == "global":
289+
if capturing:
297290
console.rule(
298291
f"PDB {method} (IO-capturing turned off)",
299292
characters=">",
300293
style=None,
301294
)
302-
elif capturing:
303-
console.rule(
304-
f"PDB {method} (IO-capturing turned off for {capturing})",
305-
characters=">",
306-
style=None,
307-
)
308295
else:
309296
console.rule(f"PDB {method}", characters=">", style=None)
310297

tests/test_debugging.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import sys
34
import textwrap
45
from contextlib import ExitStack as does_not_raise # noqa: N813
@@ -289,19 +290,19 @@ def task_1():
289290
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(source))
290291

291292
child = pexpect.spawn(f"pytask {tmp_path.as_posix()}")
292-
child.expect(r"PDB set_trace \(IO-capturing turned off\)")
293+
child.expect(["PDB", "set_trace", r"\(IO-capturing", "turned", r"off\)"])
293294
child.expect("task_1")
294295
child.expect("x = 3")
295296
child.expect("Pdb")
296297
child.sendline("c")
297-
child.expect(r"PDB continue \(IO-capturing resumed\)")
298-
child.expect(r"PDB set_trace \(IO-capturing turned off\)")
298+
child.expect(["PDB", "continue", r"\(IO-capturing", r"resumed\)"])
299+
child.expect(["PDB", "set_trace", r"\(IO-capturing", "turned", r"off\)"])
299300
child.expect("x = 4")
300301
child.expect("Pdb")
301302
child.sendline("c")
302-
child.expect(r"PDB continue \(IO-capturing resumed\)")
303+
child.expect(["PDB", "continue", r"\(IO-capturing", r"resumed\)"])
303304
child.expect("task_1 failed")
304-
rest = child.read().decode("utf8")
305+
rest = _escape_ansi(child.read().decode("utf8"))
305306
assert "Captured stdout during call" in rest
306307
assert "hello17" in rest # out is captured
307308
assert "hello18" in rest # out is captured
@@ -361,7 +362,7 @@ def task_1():
361362
env={"PATH": os.environ["PATH"], "PYTHONPATH": f"{tmp_path.as_posix()}"},
362363
)
363364

364-
child.expect(r"PDB set_trace \(IO-capturing turned off\)")
365+
child.expect(["PDB", "set_trace", r"\(IO-capturing", "turned", r"off\)"])
365366
child.expect(r"\n\(Pdb")
366367
child.sendline("debug foo()")
367368
child.expect("ENTERING RECURSIVE DEBUGGER")
@@ -380,8 +381,8 @@ def task_1():
380381
assert b"Quitting debugger" not in child.before
381382

382383
child.sendline("c")
383-
child.expect(r"PDB continue \(IO-capturing resumed\)")
384-
rest = child.read().decode("utf8")
384+
child.expect(["PDB", "continue", r"\(IO-capturing", r"resumed\)"])
385+
rest = _escape_ansi(child.read().decode("utf8"))
385386
assert "hello17" in rest # out is captured
386387
assert "hello18" in rest # out is captured
387388
assert "1 failed" in rest
@@ -405,7 +406,7 @@ def task_1():
405406
child.expect("Pdb")
406407
child.sendline("c")
407408
child.expect(r"PDB continue")
408-
child.expect("1 succeeded")
409+
child.expect(["1", "succeeded"])
409410
_flush(child)
410411

411412

@@ -469,3 +470,9 @@ def test_function():
469470
rest = child.read().decode("utf8")
470471
assert "1 passed" in rest
471472
_flush(child)
473+
474+
475+
def _escape_ansi(line):
476+
"""Escape ANSI sequences produced by rich."""
477+
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
478+
return ansi_escape.sub("", line)

0 commit comments

Comments
 (0)