-
-
Notifications
You must be signed in to change notification settings - Fork 13
Store patched source in linecache #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, certainly seems worthwhile. Do you think you could write a test and changelog note?
And can you provide a command and example file to test this before/after? You didn't really describe the issue. I’m guessing that ipdb fails to show the source of patched functions... is it the same with pdb?
src/patchy/api.py
Outdated
@@ -216,9 +216,21 @@ def _compile( | |||
code: str | ast.Module, | |||
flags: int = 0, | |||
) -> CodeType | ast.Module: | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/patchy/api.py
Outdated
result: CodeType | ast.Module = compile( | ||
code, "<patchy>", "exec", flags=feature_flags | flags, dont_inherit=True | ||
) | ||
|
||
import linecache |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hoist to module level
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally, this was just a quick and dirty PR to see if you were interested in the idea
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! |
this enables ipdb and friends to work properly if `breakpoint()` is added to the mutated function
80ed316
to
4bf0bbe
Compare
I tried to finish this off but found several scenarios that don't work with the current approach.
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 |
I think it's harder than that. Also, patchy should not add attributes to functions, but instead use a |
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.