Skip to content

Example Post Mortem Debugger

rafageist edited this page Dec 2, 2026 · 1 revision

Example: a post-mortem debugger

examples/debugger/ runs a Python program and, when it fails, shows where in Turbo Pascal's debugger windows: the call stack, innermost call first, and each call's variables, with the call's line under the debugger's bar in the editor. It needs no extra.

python examples/debugger/post_mortem.py              # runs examples/debugger/sample/average.py
python examples/debugger/post_mortem.py my_script.py

The call stack where the program failed; Enter on a call shows its variables and line

Key Does
Up, Down in the Call stack choose a call
Enter (or a double click) show its variables and its line
Enter in Variables a variable's whole value
typing in a list filters it
Alt+X exit

The files

File What it is
post_mortem.py the application: the debugger windows, an editor and the program's output
tracer.py runs the program in a child Python; if it fails, writes the failing calls and their variables as JSON
sample/average.py a program with a bug: the average of an empty group divides by zero

What a debugger is (this tracer, a DAP or DBGp session) stays in the application. TurboFrame gives the windows: see Debugger Windows.

The windows

class VariablesWindow(ListWindow):
    """A call's variables, ``name = value``; Enter shows a value whole."""

    EMPTY = "(no call chosen)"

    def __init__(self, number: int = 0) -> None:
        super().__init__("Variables", number=number)
        self.values: list[tuple[str, str]] = []

    def set_values(self, values: list[tuple[str, str]]) -> None:
        self.values = values
        self.show([f"{name} = {value}" for name, value in values])

    def activate(self, index: int) -> None:
        if index < len(self.values):
            name, value = self.values[index]
            self.app.run_worker(self.app.run_dialog(information(name, value)))
  • A ListWindow shows lines in the debugger's colours. show(lines) replaces them, keeping the chosen line when only their text changes; typing filters them; Enter or a double click calls activate(index).
  • CallStackWindow is a ListWindow whose Enter asks the application's select_frame(index), a coroutine, in a worker.

Choosing a call

    async def select_frame(self, index: int) -> None:
        """CallStackWindow's Enter: the call's variables, and its line in the editor."""
        frame = self.frames[index]
        self.variables.set_values([(name, value) for name, value in frame["locals"].items()])
        window = await self.open_file(Path(frame["file"]))
        if window is not None:
            row = frame["line"] - 1
            window.editor.go_to(row)
            window.editor.set_debug_marks(set(), row)  # the debugger's bar on the line
            self.raise_window(self.stack)
            self.call_after_refresh(self.stack.list.focus)  # after open_file's own focus on the editor
        self.hint(f"{frame['error']}  ·  in {frame['function']}()")

set_debug_marks(breakpoints, execution) draws the debugger's bars in the editor: the breakpoints' rows and the row about to run (0-based). The program's output, its traceback included, streams into a LogWindow through process.run; see Log and Output Windows.

report(): its variables, and line 14 under the debugger's bar

Clone this wiki locally