diff --git a/src/prompt_toolkit/application/application.py b/src/prompt_toolkit/application/application.py index ecf52d176..687898cca 100644 --- a/src/prompt_toolkit/application/application.py +++ b/src/prompt_toolkit/application/application.py @@ -1030,9 +1030,11 @@ def _enable_breakpointhook(self) -> Generator[None, None, None]: """ Install our custom breakpointhook for the duration of this context manager. (We will only install the hook if no other custom hook was - set.) + set, including through PYTHONBREAKPOINT.) """ - if sys.breakpointhook == sys.__breakpointhook__: + if sys.breakpointhook == sys.__breakpointhook__ and not os.environ.get( + "PYTHONBREAKPOINT" + ): sys.breakpointhook = self._breakpointhook try: diff --git a/tests/test_application.py b/tests/test_application.py new file mode 100644 index 000000000..f4543e433 --- /dev/null +++ b/tests/test_application.py @@ -0,0 +1,78 @@ +import sys +import types +from unittest.mock import Mock + +import pytest + +from prompt_toolkit.application import Application +from prompt_toolkit.input import DummyInput +from prompt_toolkit.output import DummyOutput + + +@pytest.mark.parametrize("value", [None, ""]) +def test_default_breakpointhook(value, monkeypatch): + if value is None: + monkeypatch.delenv("PYTHONBREAKPOINT", raising=False) + else: + monkeypatch.setenv("PYTHONBREAKPOINT", value) + monkeypatch.setattr(sys, "breakpointhook", sys.__breakpointhook__) + app = Application(input=DummyInput(), output=DummyOutput()) + + with app._enable_breakpointhook(): + assert sys.breakpointhook == app._breakpointhook + + assert sys.breakpointhook is sys.__breakpointhook__ + + +@pytest.mark.parametrize("value", ["0", "custom_debugger.hook"]) +def test_environment_breakpointhook(value, monkeypatch): + monkeypatch.setenv("PYTHONBREAKPOINT", value) + monkeypatch.setattr(sys, "breakpointhook", sys.__breakpointhook__) + app = Application(input=DummyInput(), output=DummyOutput()) + + with app._enable_breakpointhook(): + assert sys.breakpointhook is sys.__breakpointhook__ + + assert sys.breakpointhook is sys.__breakpointhook__ + + +@pytest.mark.parametrize("value", ["", "0", "custom_debugger.hook"]) +def test_custom_breakpointhook(value, monkeypatch): + monkeypatch.setenv("PYTHONBREAKPOINT", value) + hook = Mock() + monkeypatch.setattr(sys, "breakpointhook", hook) + app = Application(input=DummyInput(), output=DummyOutput()) + + with app._enable_breakpointhook(): + assert sys.breakpointhook is hook + + assert sys.breakpointhook is hook + + +def test_breakpointhook_restored_after_exception(monkeypatch): + monkeypatch.delenv("PYTHONBREAKPOINT", raising=False) + monkeypatch.setattr(sys, "breakpointhook", sys.__breakpointhook__) + app = Application(input=DummyInput(), output=DummyOutput()) + + with pytest.raises(RuntimeError, match="test exception"): + with app._enable_breakpointhook(): + assert sys.breakpointhook == app._breakpointhook + raise RuntimeError("test exception") + + assert sys.breakpointhook is sys.__breakpointhook__ + + +def test_breakpoint_calls_environment_hook(monkeypatch): + module = types.ModuleType("custom_debugger") + hook = Mock(return_value="debugger result") + module.hook = hook + monkeypatch.setitem(sys.modules, "custom_debugger", module) + monkeypatch.setenv("PYTHONBREAKPOINT", "custom_debugger.hook") + monkeypatch.setattr(sys, "breakpointhook", sys.__breakpointhook__) + app = Application(input=DummyInput(), output=DummyOutput()) + + with app._enable_breakpointhook(): + assert sys.breakpointhook is sys.__breakpointhook__ + assert breakpoint("argument", keyword=True) == "debugger result" # noqa: T100 + + hook.assert_called_once_with("argument", keyword=True)