Created
April 6, 2012 07:42
-
-
Save davisagli/2317969 to your computer and use it in GitHub Desktop.
Marmoset patching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
def marmoset_patch(func, s, r): | |
source = inspect.getsource(func).replace(s, r) | |
exec source in func.func_globals | |
func.func_code = func.func_globals[func.__name__].func_code | |
def foo(): | |
print 1 | |
print 2 | |
print 3 | |
foo() | |
# 1 | |
# 2 | |
# 3 | |
marmoset_patch(foo, '3', "'ZOMG'") | |
foo() | |
# 1 | |
# 2 | |
# ZOMG |
Have the python people heard of this?
This technique intrigues me. I look forward to apply it in many real-life scenarios.
This is so wrong, the right solution is to use AST rewriting!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
O M G