From 086fad8602527b40e706ea9dc8743da94d66fb66 Mon Sep 17 00:00:00 2001 From: JJJHolscher Date: Tue, 23 Jan 2024 12:12:05 +0100 Subject: [PATCH] breakpoint --- src/debug.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/debug.py diff --git a/src/debug.py b/src/debug.py new file mode 100644 index 0000000..88d21c5 --- /dev/null +++ b/src/debug.py @@ -0,0 +1,29 @@ +#! /usr/bin/env python3 +# vim:fenc=utf-8 + +import sys +import debugpy + + +def debugger_is_active() -> bool: + """Return if the debugger is currently active + + from https://stackoverflow.com/questions/38634988/ + check-if-program-runs-in-debug-mode + """ + return hasattr(sys, 'gettrace') and sys.gettrace() is not None + + +def breakpoint(wait=True, address="localhost", port=5678): + """ Breakpoint that launches debugpy if it's not already active. + + Args: + wait: Whether debugpy halts execution until a client attaches. + address: The address where debugpy listens for a client. + port: The port where debugpy listens for a client. + """ + if not debugger_is_active(): + debugpy.listen((address, port)) + if wait: + debugpy.wait_for_client() + debugpy.breakpoint()