Fork me on GitHub

Reference: aspectlib.debug

aspectlib.debug.log Decorates func to have logging.
aspectlib.debug.format_stack Returns a one-line string with the current callstack.
aspectlib.debug.frame_iterator Yields frames till there are no more.
aspectlib.debug.strip_non_ascii Convert to string (using str) and replace non-ascii characters with a dot (.).
aspectlib.debug.format_stack(skip=0, length=6, _sep='/')[source]

Returns a one-line string with the current callstack.

aspectlib.debug.frame_iterator(frame)[source]

Yields frames till there are no more.

aspectlib.debug.log(func=None, stacktrace=10, stacktrace_align=60, attributes=(), module=True, call=True, call_args=True, call_args_repr=<built-in function repr>, result=True, exception=True, exception_repr=<built-in function repr>, result_repr=<function strip_non_ascii at 0x7f01140787d0>, use_logging='CRITICAL', print_to=None)[source]

Decorates func to have logging.

Parameters:
  • func (function) – Function to decorate. If missing log returns a partial which you can use as a decorator.
  • stacktrace (int) – Number of frames to show.
  • stacktrace_align (int) – Column to align the framelist to.
  • attributes (list) – List of instance attributes to show, in case the function is a instance method.
  • module (bool) – Show the module.
  • call (bool) – If True, then show calls. If False only show the call details on exceptions (if exception is enabled) (default: True)
  • call_args (bool) – If True, then show call arguments. (default: True)
  • call_args_repr (bool) – Function to convert one argument to a string. (default: repr)
  • result (bool) – If True, then show result. (default: True)
  • exception (bool) – If True, then show exceptions. (default: True)
  • exception_repr (function) – Function to convert an exception to a string. (default: repr)
  • result_repr (function) – Function to convert the result object to a string. (default: strip_non_ascii - like str but nonascii characters are replaced with dots.)
  • use_logging (string) – Emit log messages with the given loglevel. (default: "CRITICAL")
  • print_to (fileobject) – File object to write to, in case you don’t want to use logging module. (default: None - printing is disabled)
Returns:

A decorator or a wrapper.

Example

>>> @log(print_to=sys.stdout)
... def a(weird=False):
...     if weird:
...         raise RuntimeError('BOOM!')
>>> a()
a()                                                           <<< ...
a => None
>>> try:
...     a(weird=True)
... except Exception:
...     pass # naughty code !
a(weird=True)                                                 <<< ...
a ~ raised RuntimeError('BOOM!',)

You can conveniently use this to logs just errors, or just results, example:

>>> import aspectlib
>>> with aspectlib.weave(float, log(call=False, result=False, print_to=sys.stdout)):
...     try:
...         float('invalid')
...     except Exception as e:
...         pass # naughty code !
float('invalid')                                              <<< ...
float ~ raised ValueError(...float...invalid...)

This makes debugging naughty code easier.

PS: Without the weaving it looks like this:

>>> try:
...     log(call=False, result=False, print_to=sys.stdout)(float)('invalid')
... except Exception:
...     pass # naughty code !
float('invalid')                                              <<< ...
float ~ raised ValueError(...float...invalid...)

Changed in version 0.5.0: Renamed arguments to call_args. Renamed arguments_repr to call_args_repr. Added call option.

aspectlib.debug.strip_non_ascii(val)[source]

Convert to string (using str) and replace non-ascii characters with a dot (.).