77 points · by gaogaotiantian · 5y ago · 15 comments
from watchpoints import watch
a = []
watch(a)
a.append(1) # Trigger
a = {} # Trigger
HOW?! I demand an explanation.How does the second trigger fire? Deterministically, without relying on GC tricks? It’s watching a variable name, somehow.
Does it hook into globals()? But how would it know it needs to?
It then uses sys.settrace (which is intended as an interface for debuggers) to step through the code and check whether the variable has been changed. Documentation on sys.settrace: https://docs.python.org/3/library/sys.html#sys.settrace
Python exposes most of its guts as part of the standard library, making clever hacks like this possible.
Not that you’d want to, but it’s a fun thought experiment.
And it's a little more than inspect and frames. Note that it is much easier to do watch("a") than watch(a), but it's slightly less intuitive. So I also did some AST hack to make watch(a) possible :)
I've been working on a tool for automatically suggesting performance fixes to python apps based on static analysis and dynamic profiling. This is a nice method adding additional dynamic information that you need for certain optimizations. (E.g. if you want to suggest replacing a list with a set then you need to know how that list is used which sometimes can be hard with static analysis.)
Also I already implemented the pdb hook so this could work like breakpoint() in built-in library.