From 4d528b76a0b7ac359785360867bad271d906a604 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 8 May 2024 13:30:15 -0400 Subject: [PATCH] Move `_debug.pformat_cs()` into `devx.pformat` --- tractor/devx/_debug.py | 19 ++++--------------- tractor/devx/pformat.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/tractor/devx/_debug.py b/tractor/devx/_debug.py index 0567e42..da32240 100644 --- a/tractor/devx/_debug.py +++ b/tractor/devx/_debug.py @@ -73,7 +73,10 @@ from tractor._state import ( debug_mode, current_ipc_ctx, ) -# from .pformat import pformat_caller_frame +from .pformat import ( + # pformat_caller_frame, + pformat_cs, +) if TYPE_CHECKING: from tractor._ipc import Channel @@ -868,20 +871,6 @@ def apply_debug_pldec() -> _codec.MsgCodec: f'{orig_pldec}\n' ) -# TODO: add this formatter to `.devx.pformat()`! -def pformat_cs( - cs: CancelScope, - var_name: str = 'cs', -) -> str: - return ( - f'{var_name}: {cs}\n' - f'{var_name}.cancel_called = {cs.cancel_called}\n' - f'{var_name}.cancelled_caught = {cs.cancelled_caught}\n' - f'{var_name}._cancel_status = {cs._cancel_status}\n' - f'{var_name}.shield = {cs.shield}\n' - ) - - async def request_root_stdio_lock( actor_uid: tuple[str, str], task_uid: tuple[str, int], diff --git a/tractor/devx/pformat.py b/tractor/devx/pformat.py index 0b35fee..5fe9bc6 100644 --- a/tractor/devx/pformat.py +++ b/tractor/devx/pformat.py @@ -22,6 +22,8 @@ Mostly handy for logging and exception message content. import textwrap import traceback +from trio import CancelScope + def add_div( message: str, @@ -133,3 +135,34 @@ def pformat_caller_frame( indent='', ) return tb_str + + +def pformat_cs( + cs: CancelScope, + var_name: str = 'cs', + field_prefix: str = ' |_', +) -> str: + ''' + Pretty format info about a `trio.CancelScope` including most + of its public state and `._cancel_status`. + + The output can be modified to show a "var name" for the + instance as a field prefix, just a simple str before each + line more or less. + + ''' + + fields: str = textwrap.indent( + ( + f'cancel_called = {cs.cancel_called}\n' + f'cancelled_caught = {cs.cancelled_caught}\n' + f'_cancel_status = {cs._cancel_status}\n' + f'shield = {cs.shield}\n' + ), + prefix=field_prefix, + ) + return ( + f'{var_name}: {cs}\n' + + + fields + )