ionelmc / python-hunter

Hunter is a flexible code tracing toolkit.
https://python-hunter.readthedocs.io/
BSD 2-Clause "Simplified" License
794 stars 46 forks source link

Idea: a more powerful trace client #110

Open vitrun opened 1 year ago

vitrun commented 1 year ago

Current trace client is a simple wrapper of eval

eval('hunter.trace({})'.format(options))

It works for simple or medium complex situations, such as:

However, it comes short when import or customization is required before hunter.trace.

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

trace(
    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",
        action=Debugger(klass=Pdb)
    )
)

With due consideration of safety, it should be allowed to eval a whole snippet of codes.

ionelmc commented 1 year ago

Try hunter-trace -p 123123 'action=hunter.Debugger(klass=__import__("pdb").Pdb)'.

Note that Pdb won't work as you expect anyway (won't be attached to the terminal of hunter-trace - a different issue).

vitrun commented 1 year ago

Wouldn't it be nice to support hunter-trace -p 123123 -f CUSTOM_FILTER_ACTION.py?

vitrun commented 1 year ago

won't be attached to the terminal of hunter-trace - a different issue

Can we make it attached to hunter-trace, say, by using rpdb?

ionelmc commented 1 year ago

It could be possible, both ideas. Just need time to implement.