Move `maybe_open_crash_handler()` CLI `--pdb`-driven wrapper to debug mod

multihomed
Tyler Goodlet 2023-10-02 18:10:34 -04:00
parent 86da79a854
commit e94f1261b5
3 changed files with 20 additions and 13 deletions

View File

@ -29,6 +29,7 @@ from ._debug import (
shield_sigint_handler, shield_sigint_handler,
MultiActorPdb, MultiActorPdb,
open_crash_handler, open_crash_handler,
maybe_open_crash_handler,
post_mortem, post_mortem,
) )
@ -41,5 +42,6 @@ __all__ = [
'shield_sigint_handler', 'shield_sigint_handler',
'MultiActorPdb', 'MultiActorPdb',
'open_crash_handler', 'open_crash_handler',
'maybe_open_crash_handler',
'post_mortem', 'post_mortem',
] ]

View File

@ -30,6 +30,7 @@ from functools import (
from contextlib import ( from contextlib import (
asynccontextmanager as acm, asynccontextmanager as acm,
contextmanager as cm, contextmanager as cm,
nullcontext,
) )
from typing import ( from typing import (
Any, Any,
@ -1043,3 +1044,20 @@ def open_crash_handler(
except tuple(catch): except tuple(catch):
pdbp.xpm() pdbp.xpm()
raise 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

View File

@ -25,7 +25,6 @@ Currently popular frameworks supported are:
from __future__ import annotations from __future__ import annotations
from contextlib import ( from contextlib import (
# asynccontextmanager as acm, # asynccontextmanager as acm,
nullcontext,
contextmanager as cm, contextmanager as cm,
) )
from typing import ( from typing import (
@ -135,15 +134,3 @@ def load_runtime_vars(
) )
ctx.params = {'ctx': ctx} ctx.params = {'ctx': ctx}
cmd.invoke(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