patchy icon indicating copy to clipboard operation
patchy copied to clipboard

Store patched source in linecache

Open iloveitaly opened this issue 1 year ago • 4 comments

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.

iloveitaly avatar Aug 09 '24 13:08 iloveitaly

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!

iloveitaly avatar Aug 09 '24 15:08 iloveitaly

I tried to finish this off but found several scenarios that don't work with the current approach.

  1. Unpatching a function doesn't restore its original source.
  2. 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!

adamchainz avatar Oct 25 '24 15:10 adamchainz

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?

iloveitaly avatar Oct 27 '24 16:10 iloveitaly

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.

adamchainz avatar Oct 27 '24 21:10 adamchainz