How do I check if a python function changed (in live code)?

前端 未结 2 1034
萌比男神i
萌比男神i 2021-02-04 04:27

If I have a reference to a function I can check it\'s code object f.__code__, get a signature, then perform later checks against this signature to see if the code c

2条回答
  •  囚心锁ツ
    2021-02-04 05:06

    I'm just poking around at the __code__ object here, so I can't say that this would work for sure, but it looks to me like you could use the co_names tuple to (recursively) traverse the call graph rooted at a particular function, in order to build up some sort of hash of the transitive closure of the functions that could possible be called. (I don't think it'd be possible to include only the functions that will be called for a particular input, but you could be conservative and include every possible call.)

    To do this you'd need to maintain some sort of symbol table to be able to look up the names of the functions that get called. But once you start going down this path, it seems like you're basically going to build up your own equivalent of the AST. So, why not just use the AST to start with ?

提交回复
热议问题