Reference

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.And partial(func, *args, **keywords) - new function with partial application
hunter.Or partial(func, *args, **keywords) - new function with partial application

Actions

Objects

hunter.event.Event Event wrapper for frame, kind, arg (the arguments the settrace function gets).
hunter.trace(*predicates, clear_env_var=False, action=CodePrinter, actions=[])[source]

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

Parameters:

*predicates (callables) – Runs actions if any of the given predicates match.

Keyword Arguments:
 
  • clear_env_var – Disables tracing in subprocess. Default: False.
  • action – Action to run if all the predicates return True. Default: CodePrinter.
  • actions – Actions to run (in case you want more than 1).
hunter.stop()[source]

Stop tracing.

Notes

Restores previous tracer (if there was any).

hunter.Q(*predicates, **query)[source]

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

class hunter.Query

A query class.

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

__and__

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

__call__

Handles event. Returns True if all criteria matched.

__eq__

x.__eq__(y) <==> x==y

__ge__

x.__ge__(y) <==> x>=y

__gt__

x.__gt__(y) <==> x>y

__init__
Parameters:

query – criteria to match on.

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

__invert__

x.__invert__() <==> ~x

__le__

x.__le__(y) <==> x<=y

__lt__

x.__lt__(y) <==> x<y

__ne__

x.__ne__(y) <==> x!=y

__new__(S, ...) → a new object with type S, a subtype of T
__or__

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

__rand__

x.__rand__(y) <==> y&x

__repr__
__ror__

x.__ror__(y) <==> y|x

__str__
class hunter.When

Runs actions when condition(event) is True.

Actions take a single event argument.

__and__

x.__and__(y) <==> x&y

__call__

Handles the event.

__eq__

x.__eq__(y) <==> x==y

__ge__

x.__ge__(y) <==> x>=y

__gt__

x.__gt__(y) <==> x>y

__init__

x.__init__(...) initializes x; see help(type(x)) for signature

__le__

x.__le__(y) <==> x<=y

__lt__

x.__lt__(y) <==> x<y

__ne__

x.__ne__(y) <==> x!=y

__new__(S, ...) → a new object with type S, a subtype of T
__or__

x.__or__(y) <==> x|y

__rand__

x.__rand__(y) <==> y&x

__repr__
__ror__

x.__ror__(y) <==> y|x

__str__
hunter.And
hunter.Or
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 filename 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.

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

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

Provides few convenience properties.

code

A code object (not a string).

filename

A string with absolute path to file.

fullsource

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

A string with function name.

globals

A dict with global variables.

lineno

An integer with line number in file.

locals

A dict with local variables.

module

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

source

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

Fast but sometimes incomplete.

stdlib

A boolean flag. True if frame is in stdlib.