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) -Trueif function is a builtin functioncalls(int) - a call counter, you can use it to limit output by using altoperatordepth(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) -Trueif 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.modulehas- alias forcontainsendswith- 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 forendswithgt- works best with int, for examplelineno_gt=100translates toevent.lineno > 100gte- works best with int, for examplelineno_gte=100translates toevent.lineno >= 100in- 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=100translates toevent.calls < 100lte- works best with int, for exampledepth_lte=100translates toevent.depth <= 100regex- works best with str, for examplemodule_regex=r'(test|test.*)\b'translates tore.match(r'(test|test.*)\b', event.module)rx- alias forregexstartswith- 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 acceptablethere’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