hunter

Functions

hunter.trace Starts tracing.
hunter.stop Stop tracing.
hunter.Q Handles situations where hunter.Query objects (or other callables) are passed in as positional arguments.

Predicates

hunter.Query A query class.
hunter.When Runs actions when condition(event) is True.
hunter.And And predicate. Exits at the first sub-predicate that returns False.
hunter.Or Or predicate. Exits at first sub-predicate that returns True.

Actions

hunter.CodePrinter An action that just prints the code being executed.
hunter.Debugger An action that starts pdb.
hunter.VarsPrinter An action that prints local variables and optionally global variables visible from the current executing frame.

Objects

hunter.Event Event wrapper for frame, kind, arg (the arguments the settrace function gets).
hunter.trace(*predicates, **options)

Starts tracing. Can be used as a context manager (with slightly incorrect semantics - it starts tracing before __enter__ is called).

Parameters:
  • predicates (hunter.Q instances) – Runs actions if any of the given predicates match.
  • options – Keyword arguments that are passed to hunter.Q, for convenience.
hunter.stop()

Stop tracing. Restores previous tracer (if any).

class hunter.Q[source]

Handles situations where hunter.Query objects (or other callables) are passed in as positional arguments. Conveniently converts that to an hunter.Or predicate.

class hunter.Query(**query)[source]

A query class.

See hunter.Event for fields that can be filtered on.

__and__(other)[source]

Convenience API so you can do Q() & Q(). It converts that to And(Q(), Q()).

__call__(event)[source]

Handles event. Returns True if all criteria matched.

__init__(**query)[source]
Parameters:query

criteria to match on.

Accepted arguments: arg, code, filename, frame, fullsource, function, globals, kind, lineno, locals, module, source, stdlib, tracer.

__or__(other)[source]

Convenience API so you can do Q() | Q(). It converts that to Or(Q(), Q()).

class hunter.When(condition, *actions)[source]

Runs actions when condition(event) is True.

Actions take a single event argument.

__call__(event)[source]

Handles the event.

class hunter.And(*predicates)[source]

And predicate. Exits at the first sub-predicate that returns False.

__call__(event)[source]

Handles the event.

class hunter.Or(*predicates)[source]

Or predicate. Exits at first sub-predicate that returns True.

__call__(event)[source]

Handles the event.

class hunter.CodePrinter(stream=<open file '<stderr>', mode 'w'>, force_colors=False, filename_alignment=40)[source]

An action that just prints the code being executed.

Parameters:
  • stream (file-like) – Stream to write to. Default: sys.stderr.
  • filename_alignment (int) – Default size for the filename column (files are right-aligned). Default: 40.
__call__(event, sep='/', join=<function join>)[source]

Handle event and print filename, line number and source code. If event.kind is a return or exception also prints values.

class hunter.Debugger(klass=<class pdb.Pdb>, **kwargs)[source]

An action that starts pdb.

__call__(event)[source]

Runs a pdb.set_trace at the matching frame.

class hunter.VarsPrinter(*names, **options)[source]

An action that prints local variables and optionally global variables visible from the current executing frame.

Parameters:
  • *names (strings) – Names to evaluate. Expressions can be used (will only try to evaluate if all the variables are present on the frame.
  • stream (file-like) – Stream to write to. Default: sys.stderr.
  • filename_alignment (int) – Default size for the filaneme column (files are right-aligned). Default: 40.
  • globals (bool) – Allow access to globals. Default: False (only looks at locals).
__call__(event)[source]

Handle event and print the specified variables.

static _iter_symbols(code)[source]

Iterate all the variable names in the given expression.

Example:

  • self.foobar yields self
  • self[foobar] yields self` and foobar
_safe_eval(code, event)[source]

Try to evaluate the given code on the given frame. If failure occurs, returns some ugly string with exception.

class hunter.Event(frame, kind, arg, tracer)[source]

Event wrapper for frame, kind, arg (the arguments the settrace function gets).

Provides few convenience properties.

code[source]

A code object (not a string).

filename[source]

A string with absolute path to file.

fullsource[source]

A string with the sourcecode for the current statement (from linecache - failures are ignored).

May include multiple lines if it’s a class/function definition (will include decorators).

function[source]

A string with function name.

globals[source]

A dict with global variables.

lineno[source]

An integer with line number in file.

locals[source]

A dict with local variables.

module[source]

A string with module name (eg"foo.bar").

source[source]

A string with the sourcecode for the current line (from linecache - failures are ignored).

Fast but sometimes incomplete.

stdlib[source]

A boolean flag. True if frame is in stdlib.