This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve debugging support * Formatting, and including .python-version in ignore * Add breakpointhook * Testing around breakpointhooks * Add docs on debugging support * Prepare 0.52.0b0
- Loading branch information
1 parent
6de0557
commit 50ad3e0
Showing
12 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ share/ | |
.idea/ | ||
.vscode/ | ||
codealike.json | ||
.python-version |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ path = ["ward/tests"] | |
|
||
[tool.poetry] | ||
name = "ward" | ||
version = "0.51.2b0" | ||
version = "0.52.0b0" | ||
description = "A modern Python testing framework" | ||
exclude = ["ward/tests"] | ||
authors = ["Darren Burns <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import importlib | ||
import inspect | ||
import io | ||
import os | ||
import warnings | ||
|
||
import click | ||
import sys | ||
|
||
from ward.config import _breakpoint_supported | ||
from ward.terminal import console | ||
|
||
original_stdout = sys.stdout | ||
|
||
|
||
def init_breakpointhooks(pdb_module, sys_module) -> None: | ||
# breakpoint() is Python 3.7+ | ||
if _breakpoint_supported(): | ||
sys_module.breakpointhook = _breakpointhook | ||
pdb_module.set_trace = _breakpointhook | ||
|
||
|
||
def _breakpointhook(*args, **kwargs): | ||
hookname = os.getenv("PYTHONBREAKPOINT") | ||
if hookname is None or len(hookname) == 0: | ||
hookname = "pdb.set_trace" | ||
kwargs.setdefault("frame", inspect.currentframe().f_back) | ||
elif hookname == "0": | ||
return None | ||
|
||
modname, dot, funcname = hookname.rpartition(".") | ||
if dot == "": | ||
modname = "builtins" | ||
|
||
try: | ||
module = importlib.import_module(modname) | ||
if hookname == "pdb.set_trace": | ||
set_trace = module.Pdb(stdout=original_stdout, skip=["ward*"]).set_trace | ||
hook = set_trace | ||
else: | ||
hook = getattr(module, funcname) | ||
except: | ||
warnings.warn( | ||
f"Ignoring unimportable $PYTHONBREAKPOINT: {hookname}", RuntimeWarning | ||
) | ||
return None | ||
|
||
context = click.get_current_context() | ||
capture_enabled = context.params.get("capture_output") | ||
capture_active = isinstance(sys.stdout, io.StringIO) | ||
|
||
if capture_enabled and capture_active: | ||
sys.stdout = original_stdout | ||
console.print(f"Entering {modname} - output capturing disabled.", style="info") | ||
return hook(*args, **kwargs) | ||
return hook(*args, **kwargs) | ||
|
||
|
||
__breakpointhook__ = _breakpointhook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from types import SimpleNamespace | ||
from unittest.mock import Mock | ||
|
||
from ward import test, debug | ||
from ward.debug import init_breakpointhooks, _breakpointhook | ||
|
||
|
||
@test("init_breakpointhooks always patches pdb.set_trace") | ||
def _(): | ||
mock_pdb = Mock() | ||
init_breakpointhooks(pdb_module=mock_pdb, sys_module=Mock()) | ||
assert mock_pdb.set_trace == _breakpointhook | ||
|
||
|
||
@test("init_breakpointhooks sets sys.breakpointhook when it's supported") | ||
def _(): | ||
old_func = debug._breakpoint_supported | ||
debug._breakpoint_supported = lambda: True | ||
mock_sys = SimpleNamespace() | ||
init_breakpointhooks(pdb_module=Mock(), sys_module=mock_sys) | ||
debug._breakpoint_supported = old_func | ||
assert mock_sys.breakpointhook == _breakpointhook | ||
|
||
|
||
@test("init_breakpointhooks doesnt set breakpointhook when it's unsupported") | ||
def _(): | ||
old_func = debug._breakpoint_supported | ||
debug._breakpoint_supported = lambda: False | ||
mock_sys = SimpleNamespace() | ||
init_breakpointhooks(pdb_module=Mock(), sys_module=mock_sys) | ||
debug._breakpoint_supported = old_func | ||
assert not hasattr(mock_sys, "breakpointhook") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters