Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/prompt_toolkit/application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
78 changes: 78 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
@@ -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)
Loading