• Stars
    star
    769
  • Rank 56,706 (Top 2 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hunter is a flexible code tracing toolkit.

Overview

docs Documentation Status
tests
GitHub Actions Build Status
Coverage Status
package
PyPI Package latest release PyPI Wheel Supported versions Supported implementations
Commits since latest release

Hunter is a flexible code tracing toolkit, not for measuring coverage, but for debugging, logging, inspection and other nefarious purposes. It has a simple Python API, a convenient terminal API and a CLI tool to attach to processes.

  • Free software: BSD 2-Clause License

Installation

pip install hunter

Documentation

https://python-hunter.readthedocs.io/

Getting started

Basic use involves passing various filters to the trace option. An example:

python

import hunter hunter.trace(module='posixpath', action=hunter.CallPrinter)

import os os.path.join('a', 'b')

That would result in:

pycon

>>> os.path.join('a', 'b')

/usr/lib/python3.6/posixpath.py:75 call => join(a='a') /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call => _get_sep(path='a') /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return <= _get_sep: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return <= join: 'a/b'

'a/b'

In a terminal it would look like:

image

Another useful scenario is to ignore all standard modules and force colors to make them stay even if the output is redirected to a file.

python

import hunter hunter.trace(stdlib=False, action=hunter.CallPrinter(force_colors=True))

Actions

Output format can be controlled with "actions". There's an alternative CodePrinter action that doesn't handle nesting (it was the default action until Hunter 2.0).

If filters match then action will be run. Example:

python

import hunter hunter.trace(module='posixpath', action=hunter.CodePrinter)

import os os.path.join('a', 'b')

That would result in:

pycon

>>> os.path.join('a', 'b')

/usr/lib/python3.6/posixpath.py:75 call def join(a, *p): /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call def _get_sep(path): /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return return '/' ... return value: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return return path ... return value: 'a/b'

'a/b'

  • or in a terminal:

image


Another useful action is the VarsPrinter:

python

import hunter # note that this kind of invocation will also use the default CallPrinter action hunter.trace(hunter.Q(module='posixpath', action=hunter.VarsPrinter('path')))

import os os.path.join('a', 'b')

That would result in:

pycon

>>> os.path.join('a', 'b')

/usr/lib/python3.6/posixpath.py:75 call => join(a='a') /usr/lib/python3.6/posixpath.py:80 line a = os.fspath(a) /usr/lib/python3.6/posixpath.py:81 line sep = _get_sep(a) /usr/lib/python3.6/posixpath.py:41 call [path => 'a'] /usr/lib/python3.6/posixpath.py:41 call => _get_sep(path='a') /usr/lib/python3.6/posixpath.py:42 line [path => 'a'] /usr/lib/python3.6/posixpath.py:42 line if isinstance(path, bytes): /usr/lib/python3.6/posixpath.py:45 line [path => 'a'] /usr/lib/python3.6/posixpath.py:45 line return '/' /usr/lib/python3.6/posixpath.py:45 return [path => 'a'] /usr/lib/python3.6/posixpath.py:45 return <= _get_sep: '/' /usr/lib/python3.6/posixpath.py:82 line path = a /usr/lib/python3.6/posixpath.py:83 line [path => 'a'] /usr/lib/python3.6/posixpath.py:83 line try: /usr/lib/python3.6/posixpath.py:84 line [path => 'a'] /usr/lib/python3.6/posixpath.py:84 line if not p: /usr/lib/python3.6/posixpath.py:86 line [path => 'a'] /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:87 line [path => 'a'] /usr/lib/python3.6/posixpath.py:87 line if b.startswith(sep): /usr/lib/python3.6/posixpath.py:89 line [path => 'a'] /usr/lib/python3.6/posixpath.py:89 line elif not path or path.endswith(sep): /usr/lib/python3.6/posixpath.py:92 line [path => 'a'] /usr/lib/python3.6/posixpath.py:92 line path += sep + b /usr/lib/python3.6/posixpath.py:86 line [path => 'a/b'] /usr/lib/python3.6/posixpath.py:86 line for b in map(os.fspath, p): /usr/lib/python3.6/posixpath.py:96 line [path => 'a/b'] /usr/lib/python3.6/posixpath.py:96 line return path /usr/lib/python3.6/posixpath.py:96 return [path => 'a/b'] /usr/lib/python3.6/posixpath.py:96 return <= join: 'a/b'

'a/b'

In a terminal it would look like:

image


You can give it a tree-like configuration where you can optionally configure specific actions for parts of the tree (like dumping variables or a pdb set_trace):

python

from hunter import trace, Q, Debugger from pdb import Pdb

trace(

# drop into a Pdb session if foo.bar() is called Q(module="foo", function="bar", kind="call", action=Debugger(klass=Pdb)) | # or Q( # show code that contains "mumbo.jumbo" on the current line lambda event: event.locals.get("mumbo") == "jumbo", # and it's not in Python's stdlib stdlib=False, # and it contains "mumbo" on the current line source__contains="mumbo" )

)

import foo foo.func()

With a foo.py like this:

python

def bar():

execution_will_get_stopped # cause we get a Pdb session here

def func():

mumbo = 1 mumbo = "jumbo" print("not shown in trace") print(mumbo) mumbo = 2 print(mumbo) # not shown in trace bar()

We get:

pycon

>>> foo.func() not shown in trace /home/ionel/osp/python-hunter/foo.py:8 line print(mumbo) jumbo /home/ionel/osp/python-hunter/foo.py:9 line mumbo = 2 2 /home/ionel/osp/python-hunter/foo.py:1 call def bar(): > /home/ionel/osp/python-hunter/foo.py(2)bar() -> execution_will_get_stopped # cause we get a Pdb session here (Pdb)

In a terminal it would look like:

image

Tracing processes

In similar fashion to strace Hunter can trace other processes, eg:

hunter-trace --gdb -p 123

If you wanna play it safe (no messy GDB) then add this in your code:

from hunter import remote
remote.install()

Then you can do:

hunter-trace -p 123

See docs on the remote feature.

Note: Windows ain't supported.

Environment variable activation

For your convenience environment variable activation is available. Just run your app like this:

PYTHONHUNTER="module='os.path'" python yourapp.py

On Windows you'd do something like:

set PYTHONHUNTER=module='os.path'
python yourapp.py

The activation works with a clever .pth file that checks for that env var presence and before your app runs does something like this:

from hunter import *
trace(<whatever-you-had-in-the-PYTHONHUNTER-env-var>)

Note that Hunter is activated even if the env var is empty, eg: PYTHONHUNTER="".

Environment variable configuration

Sometimes you always use the same options (like stdlib=False or force_colors=True). To save typing you can set something like this in your environment:

PYTHONHUNTERCONFIG="stdlib=False,force_colors=True"

This is the same as PYTHONHUNTER="stdlib=False,action=CallPrinter(force_colors=True)".

Notes:

  • Setting PYTHONHUNTERCONFIG alone doesn't activate hunter.
  • All the options for the builtin actions are supported.
  • Although using predicates is supported it can be problematic. Example of setup that won't trace anything:

    PYTHONHUNTERCONFIG="Q(module_startswith='django')"
    PYTHONHUNTER="Q(module_startswith='celery')"

    which is the equivalent of:

    PYTHONHUNTER="Q(module_startswith='django'),Q(module_startswith='celery')"

    which is the equivalent of:

    PYTHONHUNTER="Q(module_startswith='django')&Q(module_startswith='celery')"

Filtering DSL

Hunter supports a flexible query DSL, see the introduction.

Development

To run the all tests run:

tox

Design notes

Hunter doesn't do everything. As a design goal of this library some things are made intentionally austere and verbose (to avoid complexity, confusion and inconsistency). This has few consequences:

  • There are Operators but there's no negation operator. Instead you're expected to negate a Query object, eg: ~Q(module='re').
  • There are no specialized operators or filters - all filters behave exactly the same. For example:
    • No filter for packages. You're expected to filter by module with an operator.
    • No filter for arguments, return values or variables. You're expected to write your own filter function and deal with the problems of poking into objects.
  • Layering is minimal. There's are some helpers that do some argument processing and conversions to save you some typing but that's about it.
  • The library doesn't try to hide the mechanics of tracing in Python - it's 1:1 regarding what Python sends to a trace function if you'd be using sys.settrace.
  • Doesn't have any storage. You are expected to redirect output to a file.

You should look at it like it's a tool to help you understand and debug big applications, or a framework ridding you of the boring parts of settrace, not something that helps you learn Python.

FAQ

Why not Smiley?

There's some obvious overlap with smiley but there are few fundamental differences:

  • Complexity. Smiley is simply over-engineered:

    • It uses IPC and a SQL database.
    • It has a webserver. Lots of dependencies.
    • It uses threads. Side-effects and subtle bugs are introduced in your code.
    • It records everything. Tries to dump any variable. Often fails and stops working.

    Why do you need all that just to debug some stuff in a terminal? Simply put, it's a nice idea but the design choices work against you when you're already neck-deep into debugging your own code. In my experience Smiley has been very buggy and unreliable. Your mileage may vary of course.

  • Tracing long running code. This will make Smiley record lots of data, making it unusable.

    Now because Smiley records everything, you'd think it's better suited for short programs. But alas, if your program runs quickly then it's pointless to record the execution. You can just run it again.

    It seems there's only one situation where it's reasonable to use Smiley: tracing io-bound apps remotely. Those apps don't execute lots of code, they just wait on network so Smiley's storage won't blow out of proportion and tracing overhead might be acceptable.

  • Use-cases. It seems to me Smiley's purpose is not really debugging code, but more of a "non interactive monitoring" tool.

In contrast, Hunter is very simple:

  • Few dependencies.
  • Low overhead (tracing/filtering code has an optional Cython extension).
  • No storage. This simplifies lots of things.

    The only cost is that you might need to run the code multiple times to get the filtering/actions right. This means Hunter is not really suited for "post-mortem" debugging. If you can't reproduce the problem anymore then Hunter won't be of much help.

Why not pytrace?

Pytrace is another tracer tool. It seems quite similar to Smiley - it uses a sqlite database for the events, threads and IPC, thus it's reasonable to expect the same kind of problems.

Why not PySnooper or snoop?

snoop is a refined version of PySnooper. Both are more suited to tracing small programs or functions as the output is more verbose and less suited to the needs of tracing a big application where Hunter provides more flexible setup, filtering capabilities, speed and brevity.

Why not coverage?

For purposes of debugging coverage is a great tool but only as far as "debugging by looking at what code is (not) run". Checking branch coverage is good but it will only get you as far.

From the other perspective, you'd be wondering if you could use Hunter to measure coverage-like things. You could do it but for that purpose Hunter is very "rough": it has no builtin storage. You'd have to implement your own storage. You can do it but it wouldn't give you any advantage over making your own tracer if you don't need to "pre-filter" whatever you're recording.

In other words, filtering events is the main selling point of Hunter - it's fast (cython implementation) and the query API is flexible enough.

Projects using Hunter

Noteworthy usages or Hunter (submit a PR with your project if you built a tool that relies on hunter):

More projects using it at https://github.com/ionelmc/python-hunter/network/dependents

More Repositories

1

cookiecutter-pylibrary

Enhanced cookiecutter template for Python libraries.
Python
1,200
star
2

pytest-benchmark

py.test fixture for benchmarking code
Python
1,159
star
3

python-redis-lock

Lock context manager implemented via redis SET NX EX and BLPOP.
Python
514
star
4

python-manhole

Debugging manhole for python applications.
Python
356
star
5

django-redisboard

Redis monitoring and inspection tool in django admin.
Python
265
star
6

python-remote-pdb

Remote vanilla PDB (over TCP sockets).
Python
252
star
7

python-lazy-object-proxy

A fast and thorough lazy object proxy.
Python
234
star
8

django-prefetch

Generic model related data prefetch framework for Django.
Python
153
star
9

python-tblib

Serialization library for Exceptions and Tracebacks.
Python
149
star
10

python-nameless

Sample project. Use https://github.com/ionelmc/cookiecutter-pylibrary to make your own project. The purpose of this repo is to test the CI configuration.
Python
144
star
11

python-fields

A totally different take on container boilerplate.
Python
137
star
12

jquery-gp-gallery

jQuery gallery plugin (ala google plus photo galeries)
CSS
126
star
13

python-aspectlib

An aspect-oriented programming, monkey-patch and decorators library. It is useful when changing behavior in existing code is desired. It includes tools for debugging and testing: simple mock/record and a complete capture/replay framework.
Python
108
star
14

django-monkey-team

Django middleware and userscript that displays debug tracebacks on production sites (where you would have DEBUG = False) only to developers.
Python
57
star
15

django-uwsgi-cache

uWSGI Django cache backend.
Python
38
star
16

django-admin-customizer

Django admin customizing interface
Python
36
star
17

python-holdup

A tool to wait for services and execute command. Useful in Docker containers.
Python
33
star
18

projectskel

Project skeleton for python 2.7 projects with fabric and virtualenv. It's intended for django projects but can be customized for other types of projects.
Python
28
star
19

sphinx-py3doc-enhanced-theme

A theme based on the theme of https://docs.python.org/3/ with some responsive enhancements.
JavaScript
25
star
20

django-admin-utils

Utility code for easier django admin development
Python
25
star
21

tox-wheel

A Tox plugin that builds and installs wheels instead of sdist. Note that this plugin is obsolte as tox 4.0 already has wheel support.
Python
23
star
22

docker-webdav

NGINX WebDAV container
Shell
23
star
23

python-cookiepatcher

Just a small shim around cookiecutter that alters a bit the CLI to work better when reapplying templates to existing projects.
Python
18
star
24

nose-htmloutput

Python
14
star
25

nose-timelimit

Nose plugin that allows you automatically skip tests that are too slow.
Python
13
star
26

cookiecutter-pylibrary-minimal

This has been merged into https://github.com/ionelmc/cookiecutter-pylibrary - use that instead!
Python
12
star
27

pypi-alias

A small utility to make alias distributions on PyPI.
Python
11
star
28

python-su

Python
9
star
29

python-process-tests

Testcase classes and assertions for testing processes.
Python
9
star
30

python-packaging-blunders

Python
8
star
31

polymer-select-box

Tagging widget implemented as a Polymer webcomponent
HTML
7
star
32

django-badbrowser

Browser detection (including browser upgrade notices) for Django
Python
7
star
33

python-mongoql-conv

Library to convert those MongoDB queries to something else, like a python expresion, a function or a django query (Q) object tree
Python
7
star
34

python-cogen

Automatically exported from https://code.google.com/p/cogen
Python
6
star
35

python-signalfd

CFFI bindings for signalfd.
Python
6
star
36

python-appengine-sdk

Un-official `pip install`-able AppEngine SDK.
Python
6
star
37

python-redis-throttled-queue

WIP
Python
6
star
38

python-unlzw

Python
6
star
39

python-stampede

Event-loop based, miniature job queue and worker that runs the task in a subprocess (via fork).
Python
6
star
40

django-easyfilters

Fork of https://bitbucket.org/evildmp/django-easyfilters/
Python
5
star
41

pytest-cover

Merged into https://github.com/schlamar/pytest-cov - use that instead!
Python
5
star
42

javascript-userscripts

Automatically exported from code.google.com/p/webmonkey-userscripts
JavaScript
4
star
43

python-mongosizeof

Python
4
star
44

pylint-fields

Pylint plugin for python-fields
Python
4
star
45

python-pygaljs

Python package providing assets from https://github.com/Kozea/pygal.js
Python
4
star
46

python-tax

2021 update: use tox-direct instead. This was a variant of Tox that didn't use virtualenvs at all - just installed everything in the current environment.
Python
4
star
47

django-secdownload-storage

Django storage backend that can be used to serve files via lighttpd's mod_secdownload module.
Python
4
star
48

docker-in-docker

An actually usable DIND. Includes a bunch of debug tools and docker-compose.
3
star
49

django-customfields

Couple of custom model fields for django: CachedManyToManyField and InheritedField
Python
3
star
50

python-pth

Simple and brief path traversal and filesystem access library.
Python
3
star
51

python-nameless-minimal

Python
3
star
52

python-ftpd-example

Python
2
star
53

dotfiles

My zsh setup
Shell
2
star
54

docker-manylinux

https://hub.docker.com/r/ionelmc/manylinux
Shell
2
star
55

python-matrix

Python
2
star
56

docker-buildpack-deps

Just buildpack-deps with some extras
Dockerfile
2
star
57

polymer-query-box

Query editor widget implemented as a Polymer webcomponent
JavaScript
2
star
58

polymer-json-box

Simple json edit widget implemented as a Polymer webcomponent
JavaScript
2
star
59

docker-fakebuntu

Ubuntu Xenial image running minimal services: systemd, journald, sshd, dind (docker in docker)
C
2
star
60

python-css-sprite

Python
1
star
61

django-image-editor

Allows to edit images in the browser
JavaScript
1
star
62

ppa-socat

Shell
1
star
63

pytest-benchmark-elasticsearch

Elasticseach storage backend for pytest-benchmark.
Python
1
star
64

t.ionelmc.ro

Google analytics to __utm.gif redirector.
Python
1
star
65

dockerskel

Abandoned. Check out https://github.com/evozon/django-docker
Shell
1
star