Metadata-Version: 2.4
Name: cs-debug
Version: 20260526
Summary: Assorted debugging facilities.
Keywords: python2,python3
Author-email: Cameron Simpson <cs@cskk.id.au>
Description-Content-Type: text/markdown
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Requires-Dist: cs.context>=20250528
Requires-Dist: cs.deco>=20260525
Requires-Dist: cs.fs>=20260526
Requires-Dist: cs.lex>=20260526
Requires-Dist: cs.logutils>=20250323
Requires-Dist: cs.obj>=20260526
Requires-Dist: cs.pfx>=20250914
Requires-Dist: cs.py.func>=20250914
Requires-Dist: cs.py.stack>=20250724
Requires-Dist: cs.py3>=20220523
Requires-Dist: cs.seq>=20260526
Requires-Dist: cs.threads>=20260526
Requires-Dist: cs.upd>=20260526
Requires-Dist: cs.x>=20240630
Project-URL: MonoRepo Commits, https://bitbucket.org/cameron_simpson/css/commits/branch/main
Project-URL: Monorepo Git Mirror, https://github.com/cameron-simpson/css
Project-URL: Monorepo Hg/Mercurial Mirror, https://hg.sr.ht/~cameron-simpson/css
Project-URL: Source, https://github.com/cameron-simpson/css/blob/main/lib/python/cs/debug.py

Assorted debugging facilities.

*Latest 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.

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.X`
* `abrk`: a decorator to call `breakpoint()` on logic errors such as `AssertionError`
* `breakpoint`: a wrapper for the builtin `breakpoint` which attaches to `/dev/tty`
* `pformat`: `pprint.pformat`
* `pprint`: `pprint.pprint`
* `print`: `cs.upd.print`
* `r`: `cs.lex.r`
* `redirect_stdout`: `contextlib.redirect_stdout`
* `s`: `cs.lex.s`
* `stack_dump`: dump current `Thread`'s call stack
* `thread_dump` dump the active `Thread`s with their call stacks
* `trace`: the `@trace` decorator
`$CS_DEBUG_BUILTINS` can also be set to `"1"` to install all of
`__all__` in the builtins.

Short summary:
* `abrk`: A decorator to intercept the specified `exceptions` (by default `AssertionError`, `NameError`, `RuntimeError`) and call `breakpoint()`. 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/tty` as `sys.stdin` if `sys.stdin` is not a tty.
* `print_obj`: Call `tabulate_obj(obj,label=label)` and pass to `cs.lex.printt()`.
* `stack_dump`: Dump a stack trace to a logger.
* `tabulate_obj`: Tabulate the contents of an object for display via `cs.lex.printt()`.
* `thread_dump`: Write thread identifiers and stack traces to the file `fp`.
* `TimingOutLock`: A `Lock` replacement which times out, used for locating deadlock points.
* `trace`: Decorator to report the call and return of a function.

Module contents:
- <a name="abrk"></a>`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 function
  - `func_a`, `func_kw`: the function positional and keyword arguments

  Examples:

      @abrk
      def broken_function(......):

      @property
      @abrk(exceptions=AttributeError)
      def broken_property(......):
- <a name="breakpoint"></a>`breakpoint(*a, **kw)`: Wrapper for buildins.breakpoint()` which attaches `/dev/tty`
  as `sys.stdin` if `sys.stdin` is not a tty.
- <a name="print_obj"></a>`print_obj(obj, label=None)`: Call `tabulate_obj(obj,label=label)` and pass to `cs.lex.printt()`.
- <a name="stack_dump"></a>`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 by `traceback.extract_stack`.
    If missing or `None`, use the result of `traceback.extract_stack()`.
    If `stack` has a `.tb_frame` or `.__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 or `None`, dump all entries.
  * `logger`: a `logger.Logger` ducktype or the name of a logger.
    If missing or `None`, obtain a logger from `logging.getLogger()`.
  * `log_level`: the logging level for the dump.
    If missing or `None`, use `cs.logutils.loginfo.level`.
- <a name="tabulate_obj"></a>`tabulate_obj(obj, label=None, *, seen=None)`: Tabulate the contents of an object for display via `cs.lex.printt()`.
- <a name="thread_dump"></a>`thread_dump(Ts=None, fp=None)`: Write thread identifiers and stack traces to the file `fp`.

  Parameters:
  * `Ts`: the `Thread`s to dump; if unspecified use `threading.enumerate()`.
  * `fp`: the file to which to write; if unspecified use `sys.stderr`.
- <a name="TimingOutLock"></a>`class TimingOutLock`: A `Lock` replacement which times out, used for locating deadlock points.
- <a name="trace"></a>`trace(*da, **dkw)`: Decorator to report the call and return of a function.

  Decorator parameters:
  * `call`: trace the call, default `True`
  * `retval`: trace the return, default `False`
  * `exception`: trace raised exceptions, default `True`
  * `use_pformat`: present the return value using
    `pformat` instead of `repr`, default `False`
  * `with_caller`: include the caller if this function, default `True`
  * `with_pfx`: include the current `Pfx` prefix, default `False`

# Release Log



*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.
