patchy
patchy copied to clipboard
Store patched source in linecache
This enables ipdb and friends to work properly if breakpoint() is added to the mutated function.
Curious what you think of this approach. I can clean it up if you like it!
I'm not nearly as familiar with py internals as you, so curious what I'm missing too.
Yes, ipdb fails to show source when debugging. Haven't checked with pdb, but I believe it uses the exact same mechanism (ipdb is an extension of pdb, so probably the same exact source code).
I'll tidy this up!
I tried to finish this off but found several scenarios that don't work with the current approach.
- Unpatching a function doesn't restore its original source.
- Patching two functions leaves the source for the last one in
linecache, only. This is because the storage currently saves source for the whole<patchy>virtual module.
I don't have time to dig into these details right now, but maybe you'd be interested? No pressure!
Ah, the unpatching case is interesting. What if we stored the original source for the function in a variable on the function original_linecache that we can pull from if unpatched?
I think it's harder than that. linecache is per-file, not per function.
Also, patchy should not add attributes to functions, but instead use a WeakKeyDictionary, as is already done for the source.