Assorted debugging facilities.
Latest release 20260912:
- New tabulate_class and print_class.
- Get Trace from cs.trace.
If the environment variable $CS_DEBUG_BUILTINS is set to a comma
separated list of names then the builtins module will be monkey
patched with those names, enabling trite debug use of those names
anywhere in the code provided this module has been imported somewhere.
Particularly, when debugging programmes which read data from the
standard input (sys.stdin) it is helpful to monkey patch breakpoint
with the function from this module, which attaches to /dev/tty
for the duration of the breakpoint call.
The allowed names are the list cs.debug.__all__ and include:
X:cs.x.Xabrk: a decorator to callbreakpoint()on logic errors such asAssertionErrorbreakpoint: a wrapper for the builtinbreakpointwhich attaches to/dev/ttypformat:pprint.pformatpprint:pprint.pprintprint:cs.upd.printr:cs.lex.rredirect_stdout:contextlib.redirect_stdouts:cs.lex.sstack_dump: dump currentThread's call stackthread_dumpdump the activeThreads with their call stackstrace: the@tracedecorator$CS_DEBUG_BUILTINScan also be set to"1"to install all of__all__in the builtins.
Short summary:
-
abrk: A decorator to intercept the specifiedexceptions(by defaultAssertionError,NameError,RuntimeError) and callbreakpoint(). The breakpoint frame contains: -func: the wrapper function -func_a,func_kw: the function positional and keyword arguments. -
breakpoint: Wrapper for buildins.breakpoint()which attaches/dev/ttyassys.stdinifsys.stdin` is not a tty. -
print_class: Calltabulate_class(cls)and pass tocs.lex.printt(). -
print_obj: Calltabulate_obj(obj,label=label)and pass tocs.lex.printt(). -
stack_dump: Dump a stack trace to a logger. -
tabulate_class: Construct a table reporting aboutclsand its subclasses. -
tabulate_obj: Tabulate the contents of an object for display viacs.lex.printt(). -
thread_dump: Write thread identifiers and stack traces to the filefp. -
TimingOutLock: ALockreplacement which times out, used for locating deadlock points. -
trace: Decorator to report the call and return of a function.
Functions
abrk(*da, **dkw)
A decorator to intercept the specified exceptions
(by default AssertionError, NameError, RuntimeError)
and call breakpoint().
The breakpoint frame contains:
func: the wrapper functionfunc_a,func_kw: the function positional and keyword arguments
Examples:
@abrk
def broken_function(......):
@property
@abrk(exceptions=AttributeError)
def broken_property(......):
breakpoint(*a, **kw)
Wrapper for buildins.breakpoint()which attaches/dev/ttyassys.stdinifsys.stdin` is not a tty.
print_class(cls)
Call tabulate_class(cls) and pass to cs.lex.printt().
print_obj(obj, label=None)
Call tabulate_obj(obj,label=label) and pass to cs.lex.printt().
stack_dump(stack=None, limit=None, logger=None, log_level=None)
Dump a stack trace to a logger.
Parameters:
stack: a stack list as returned bytraceback.extract_stack. If missing orNone, use the result oftraceback.extract_stack(). Ifstackhas a.tb_frameor.__traceback__attribute, extract the stack from that (this covers traceback objects and exceptions).limit: a limit to the number of stack entries to dump. If missing orNone, dump all entries.logger: alogger.Loggerducktype or the name of a logger. If missing orNone, obtain a logger fromlogging.getLogger().log_level: the logging level for the dump. If missing orNone, usecs.logutils.loginfo.level.
tabulate_class(cls)
Construct a table reporting about cls and its subclasses.
tabulate_obj(obj, label=None, *, seen=None)
Tabulate the contents of an object for display via cs.lex.printt().
thread_dump(Ts=None, fp=None)
Write thread identifiers and stack traces to the file fp.
Parameters:
Ts: theThreads to dump; if unspecified usethreading.enumerate().fp: the file to which to write; if unspecified usesys.stderr.
trace(*da, **dkw)
Decorator to report the call and return of a function.
Decorator parameters:
call: trace the call, defaultTrueretval: trace the return, defaultFalseexception: trace raised exceptions, defaultTrueuse_pformat: present the return value usingpformatinstead ofrepr, defaultFalsewith_caller: include the caller if this function, defaultTruewith_pfx: include the currentPfxprefix, defaultFalse
Classes
class TimingOutLock
A Lock replacement which times out, used for locating deadlock points.
TimingOutLock.__dict__
Read-only proxy of a mapping.
TimingOutLock.__firstlineno__
int([x]) -> integer int(x, base=10) -> integer
Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.int(). For floating-point numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.
int('0b100', base=0) 4
TimingOutLock.__static_attributes__
Built-in immutable sequence.
If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.
If the argument is a tuple, the return value is the same object.
Release Log
Release 20260912:
- New tabulate_class and print_class.
- Get Trace from cs.trace.
Release 20260602: Remove some stray debug.
Release 20260526:
- tabulate_obj: recursion checks, proper label default, several more changes.
- New breakpoint wrapper to use /dev/tty if stdin is not a tty.
- Move the builtins monkey patching to the bottom, drop the exclusion of breakpoint.
Release 20260403:
- @trace: include the return type when printing the return value.
- Drop trace_caller(), @trace does this already.
- @trace: new verbose=False and breakpoint=False optional parameters providing a fuller CALL recitation and a breakpoint respectively.
- New tabulate_obj() and print_obj() functions for nice printout of arbirary objects.
Release 20250728: @trace: several updates/improvements to the trace output.
Release 20250325:
- stack_dump: stack may also be a traceback object or an exception.
- stack_dump: move the logic to obtain the stack into cs.py.stack.frames().
Release 20241005:
- New log_via_print(msg, *args[, file=stdout]) function to use cs.upd.print as a logging call.
- @trace: new $CS_DEBUG_TRACE envvar which may be "print" or "warning" or "X".
- New @abrk decorator to intercept AssertionError, NameError and RuntimeError and call breakpoint.
Release 20240630: Assorted updates.
Release 20240519: trace_caller: access frame.name instead of frame.funcname.
Release 20240423:
- Support "import *" by populating all with X, r, s, TimingOutLock, thread_dump, stack_dump, trace.
- @trace: include the elapsed time on the return/exception log message.
Release 20230613.1: Bugfix builtins monkey patch.
Release 20230613: Honour $CS_DEBUG_BUILTINS envvar to monkey patch the builtins module, constraints via a white list.
Release 20230610:
- DebuggingRLock fixes.
- Move @trace from cs.py.func to cs.debug.
- Drop Lock and RLock alias factories - importers should just use the debugging lock classes directly.
- Rename threading.Thread to threading_Thread.
- Simplify the debugging lock classes.
Release 20221118: stack_dump: cope when cs.logutils.setup_logging not run yet.
Release 20211208: @trace moved to cs.pyfunc, other minor changes.
Release 20200318: Remove use of cs.obj.O, universally supplanted by types.SimpleNamespace.
Release 20181231:
- New TimingOutLock for locating deadlock points, grew from debugging cs.vt.index.
- Other minor changes.
Release 20171231:
- Update imports for recentchanges.
- New context manager TraceSuite to trace start and end of a code suite.
Release 20160918: selftest(): fix parameter ordering to match unittest.
Release 20160828: Update metadata with "install_requires" instead of "requires".
Release 20160827:
- New openfiles() to return selected pathnames of open files via lsof(8).
- New selftest() to invoke unittests with benefits.
- DebugShell, a cmd.Cmd subclass for debugging - current use case calls this with self.dict in a test case tearDwon.
- debug_object_shell: convenience wrapper for DebugShell to call it on an object's attributes.
Release 20150116: PyPI prep.
Release files for cs-debug 20260912
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cs_debug-20260912.tar.gz | 15.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cs_debug-20260912-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 30.2 kB
Release files / cs_debug-20260912.tar.gz
| Download URL | cs_debug-20260912.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
363f723ffd476cea4eabfefb79a3668c01843e0df3932531f63a58156e1e02d2
|
|
BLAKE2b-256 checksum How to use checksums |
74f42d568c7abe21daba1bb29cc8711e198f7ffe4162216f2016ad6af147bacc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / cs_debug-20260912-py2.py3-none-any.whl
| Download URL | cs_debug-20260912-py2.py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
24be71ae7013fa69632bf687bfa9fd2bbc53da4281b8c025fe7a2f57910e6f1e
|
|
BLAKE2b-256 checksum How to use checksums |
122545ed872b8ca45368d0f538e8fdf9606d80ea981f3b79cbc4689d0151e0d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|