A rather dirty way to patch module code at runtime.
Created
October 1, 2014 13:50
-
-
Save adam-p/63aaae093a71a844150e to your computer and use it in GitHub Desktop.
Patching Python code at runtime
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 b | |
class A(object): | |
def call_to_b(self, s): | |
myb = b.B() | |
myb.fn(s) |
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
class B(object): | |
def fn(self, s): | |
"""This function needs to be patched. | |
""" | |
print s |
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 | |
import textwrap | |
# `A` uses `B` and we want `B` to be altered | |
import b | |
b_B_fn_source = inspect.getsource(b.B.fn) | |
# Alternatively, can use getsourcelines to get an array of lines | |
b_B_fn_source = textwrap.dedent(b_B_fn_source) | |
# Make sure the source looks as we expect it to | |
assert('print s' in b_B_fn_source) | |
# Patch the code | |
b_B_fn_source = b_B_fn_source.replace('def fn(', 'def patched_fn(') | |
b_B_fn_source = b_B_fn_source.replace('print s', 'print "patched", s') | |
# This creates the function `patched_fn` | |
exec(b_B_fn_source) | |
# Replace the original function | |
b.B.fn = patched_fn | |
# Now module `a` will use the patched code | |
import a | |
my_a = a.A() | |
my_a.call_to_b('ohhi') | |
# expect: >>> patched ohhi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this won't work with magic methods (methods that start with
__
)