Redefining the behaviour of the assignment operator in Python to do a little more
For my final year project I'm building a debugging framework. One of the tasks I'm doing is to provide a framework that will allow the programmer to define Handlers that will execute to deal with its corresponding Exception. One of the tools available is to examine the series of types and values that an object has taken through its lifetime up until the exception. I what to copy whatever gets evaluated on the r.h.s of the assignment into my environment, index l.h.s. e.g. a = 3
this is write 3 into the memory address of a as well as my_environment["a"] (obviously scopes will complicate the structure of my_environment but thats another matter).
There a few ways of doing I can do this.
the function passed to sys.settrace can parse whats going on using a tool like PLY or whatever, detect its an assignment, split the tokens, take the 0th string as the l.h.s. It will exec the line and afterwards copy the value into my environment.
I could parse the source code myself and insert a开发者_如何学运维ll of the assignments into my environment after an assignment in the original source code.
I could convert each file into an AST and change assignment nodes into what I need it to do.
Or I could just hijack whatever low level function or whatever is responsible for generating code for assignments into doing its behaviour plus mine.
I want to do the last choice since it's the only one that does what I need without having to do any more parsing or other complicated stuff. Would this require me to write in the C part of the interpreter or could I keep it in Python?
精彩评论