Move `_debug.pformat_cs()` into `devx.pformat`

runtime_to_msgspec
Tyler Goodlet 2024-05-08 13:30:15 -04:00
parent 05b143d9ef
commit 4d528b76a0
2 changed files with 37 additions and 15 deletions

View File

@ -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],

View File

@ -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
)