Filtering¶
A list of all the keyword filters that hunter.trace
or hunter.Q
accept:
arg
- you probably don’t care about this - it may have a value for return/exception eventsbuiltin
(bool) -True
if function is a builtin functioncalls
(int) - a call counter, you can use it to limit output by using alt
operatordepth
(int) - call depth, starts from 0, increases for call events and decreases for returnsfilename
(str)fullsource
(str) - sourcecode for the executed lines (may be multiple lines in some situations)function
(str) - function nameglobals
(dict) - global variablesinstruction
(int or str, depending on Python version) - current executed bytecode, see Silenced exception runtime analysis for example usagekind
(str) - one of ‘call’, ‘exception’, ‘line’ or ‘return’lineno
(int)locals
(dict) - local variablesmodule
(str) - dotted modulesource
(str) - sourcecode for the executed linestdlib
(bool) -True
if module is from stdlibthreadid
(int)threadname
(str) - whatever threading.Thread.name returns
You can append operators to the above filters. Note that some of of the filters won’t work well with the bool or int types.
contains
- works best with str, for examplemodule_contains='foobar'
translates to'foobar' in event.module
has
- alias forcontains
endswith
- works best with str, for examplemodule_endswith='foobar'
translates toevent.module.endswith('foobar')
. You can also pass in a iterable, examplemodule_endswith=('foo', 'bar')
is acceptableew
- alias forendswith
gt
- works best with int, for examplelineno_gt=100
translates toevent.lineno > 100
gte
- works best with int, for examplelineno_gte=100
translates toevent.lineno >= 100
in
- a membership test, for examplemodule_in=('foo', 'bar')
translates toevent.module in ('foo', 'bar')
. You can use any iterable, for examplemodule_in='foo bar'
translates toevent.module in 'foo bar'
, and that would probably have the same result as the first examplelt
- works best with int, for examplecalls_lt=100
translates toevent.calls < 100
lte
- works best with int, for exampledepth_lte=100
translates toevent.depth <= 100
regex
- works best with str, for examplemodule_regex=r'(test|test.*)\b'
translates tore.match(r'(test|test.*)\b', event.module)
rx
- alias forregex
startswith
- works best with str, for examplemodule_startswith='foobar'
translates toevent.module.startswith('foobar')
. You can also pass in a iterable, examplemodule_startswith=('foo', 'bar')
is acceptablesw
- alias forstartswith
Notes:
- you can also use double underscore (if you’re too used to Django query lookups), eg:
module__has='foobar'
is acceptable - there’s nothing smart going on for the dots in module names so sometimes you might need to account for said dots:
module_sw='foo'
will match"foo.bar"
and"foobar"
- if you want to avoid matchin the later you could do either of:Q(module='foo')|Q(module_sw='foo.')
Q(module_rx=r'foo($|\.)')
- but this might cost you in speedQ(filename_sw='/path/to/foo/')
- probably the fastestQ(filename_has='/foo/')
- avoids putting in the full path but might match unwanted paths