From e94f1261b5b28f13f217bb7417f2eb8fbe7f14fb Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Mon, 2 Oct 2023 18:10:34 -0400 Subject: [PATCH] Move `maybe_open_crash_handler()` CLI `--pdb`-driven wrapper to debug mod --- tractor/devx/__init__.py | 2 ++ tractor/devx/_debug.py | 18 ++++++++++++++++++ tractor/devx/cli.py | 13 ------------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tractor/devx/__init__.py b/tractor/devx/__init__.py index e24405a..89b9a33 100644 --- a/tractor/devx/__init__.py +++ b/tractor/devx/__init__.py @@ -29,6 +29,7 @@ from ._debug import ( shield_sigint_handler, MultiActorPdb, open_crash_handler, + maybe_open_crash_handler, post_mortem, ) @@ -41,5 +42,6 @@ __all__ = [ 'shield_sigint_handler', 'MultiActorPdb', 'open_crash_handler', + 'maybe_open_crash_handler', 'post_mortem', ] diff --git a/tractor/devx/_debug.py b/tractor/devx/_debug.py index eef5c84..24baba0 100644 --- a/tractor/devx/_debug.py +++ b/tractor/devx/_debug.py @@ -30,6 +30,7 @@ from functools import ( from contextlib import ( asynccontextmanager as acm, contextmanager as cm, + nullcontext, ) from typing import ( Any, @@ -1043,3 +1044,20 @@ def open_crash_handler( except tuple(catch): pdbp.xpm() raise + + +@cm +def maybe_open_crash_handler(pdb: bool = False): + ''' + Same as `open_crash_handler()` but with bool input flag + to allow conditional handling. + + Normally this is used with CLI endpoints such that if the --pdb + flag is passed the pdb REPL is engaed on any crashes B) + ''' + rtctx = nullcontext + if pdb: + rtctx = open_crash_handler + + with rtctx(): + yield diff --git a/tractor/devx/cli.py b/tractor/devx/cli.py index 353389d..7689066 100644 --- a/tractor/devx/cli.py +++ b/tractor/devx/cli.py @@ -25,7 +25,6 @@ Currently popular frameworks supported are: from __future__ import annotations from contextlib import ( # asynccontextmanager as acm, - nullcontext, contextmanager as cm, ) from typing import ( @@ -135,15 +134,3 @@ def load_runtime_vars( ) ctx.params = {'ctx': ctx} cmd.invoke(ctx) - - -@cm -def maybe_open_crash_handler(pdb: bool = False): - # if the --pdb flag is passed always engage - # the pdb REPL on any crashes B) - rtctx = nullcontext - if pdb: - rtctx = open_crash_handler - - with rtctx(): - yield