Debugging
This package provides utilities to help with debugging memory allocations, function timing, stack traces, capturing signals, and pyparsing DSLs.
Mallocs
Utilities for measuring memory usage.
See MallocTool,
LogDel, and
LogDestruction.
from jgdv.debugging import MallocTool
with MallocTool(frame_count=1) as dm:
dm.whitelist(__file__)
dm.blacklist("*.venv")
val = 2
dm.snapshot("before")
vals = [random.random() for x in range(1000)]
a_dict = {"blah": 23, "bloo": set([1,2,3,4])}
dm.snapshot("after")
empty_dict = {"basic": [10, 20]}
vals = None
dm.snapshot("cleared")
dm.compare("before", "after", filter=True, fullpath=False)
Results in:
[TraceMalloc]: --> Entering, tracking 1 frames
[TraceMalloc]: Taking Snapshot: _init_ (Current: 0 B , Peak: 0 B)
[TraceMalloc]: Taking Snapshot: before (Current: 584 B , Peak: 632 B)
[TraceMalloc]: Taking Snapshot: after (Current: 32.2 KiB , Peak: 32.3 KiB)
[TraceMalloc]: Taking Snapshot: cleared (Current: 16 B , Peak: 16 B)
[TraceMalloc]: Taking Snapshot: _final_ (Current: 0 B , Peak: 0 B)
[TraceMalloc]: <-- Exited, with 5 snapshots
[TraceMalloc]: ---- Comparing (traceback): before -> after. Objects:2 ----
[TraceMalloc]: (obj:0) +32.0 KiB : vals = [random.random() for x in range(1000)] (test_malloc_tool.py:130)
[TraceMalloc]: (obj:1) +216 B : a_dict = {"blah": 23, "bloo": set([1,2,3,4])} (test_malloc_tool.py:131)
[TraceMalloc]: -- Compare (2/2) --
Timing
See TimeCtx
and TimeDec.
The first is a context manager timer, the second wraps it into
a decorator.
from jgdv.debugging.timing import TimeCtx
with TimeCtx() as obj:
some_func()
logging.info("The Function took: %s seconds", obj.total_s)
from jgdv.debugging.timing import TimeDec
@TimeDec()
def basic():
time.sleep(10)
basic()
Results in something like:
Timed: basic took 10.005232 seconds
Traces
See TraceContext and its
utility classes TraceObj and
TraceWriter.
from jgdv.debugging.trace_context import TraceContext
obj = TraceContext(targets=("call", "line", "return"),
targets=("trace","call","called"))
with obj:
other.do_something()
obj.assert_called("package.module.class.method")
Tracebacks
See TracebackFactory.
A Simple way of creating a traceback of frames,
using item access to allow a slice of available frames.
from jgdv.debugging import TracebackFactory
tb = TracebackFactory()
raise Exception().with_traceback(tb[:])
Signals
See SignalHandler and it’s
default NullHandler.
SignalHandler traps SIGINT signals and handles them,
rather than exit the program.
As SignalHandler is a a context manager, allows:
from jgdv.debugging import SignalHandler
with SignalHandler():
sys.exit(-1)
Debuggers
See RunningDebugger.