Package: patchy
Patch the inner source of python functions at runtime.A quick example, making a function that returns 1 instead return 9001:>>>defsample():...return1...>>>patchy.patch(...sample,..."""\...@@ -1,2 +1,2 @@...def sample():...-    return 1...+    return 9001...""",...)>>>sample()9001Patchy works by replacing the code attribute of the function, leaving the
function object itself the same. It’s thus more versatile than monkey patching,
since if the function has been imported in multiple places they’ll also call
the new behaviour.InstallationUsepip:python-mpipinstallpatchyPython 3.8 to 3.12 supported.Hacking on a Django project?Check out my bookBoost Your Django DXwhich covers many ways to improve your development experience.Why?If you’re monkey-patching an external library to add or fix some functionality,
you will probably forget to check the monkey patch when you upgrade it. By
using a patch against its source code, you can specify some context that you
expect to remain the same in the function that will be checked before the
source is applied.I found this with some small but important patches to Django for a project.
Since it takes a lot of energy to maintain a fork, writing monkey patches was
the chosen quick solution, but then writing actual patches would be better.The patches are applied with the standardpatchcommandline utility.Why not?There are of course a lot of reasons against:It’s (relatively) slow (since it writes the source to disk and calls thepatchcommand)If you have a patch file, why not just fork the library and apply it?At least with monkey-patching you know what end up with, rather than having
the changes being done at runtime to source that may have changed.All are valid arguments. However once in a while this might be the right
solution.How?The standard library functioninspect.getsource()is used to retrieve the
source code of the function, the patch is applied with the commandline utilitypatch, the code is recompiled, and the function’s code object is replaced
the new one. Because nothing tends to poke around at code objects apart from
dodgy hacks like this, you don’t need to worry about chasing any references
that may exist to the function, unlikemock.patch.A little special treatment is given toinstancemethod,classmethod, andstaticmethodobjects to make sure the underlying function is what gets
patched and that you don’t have to worry about the details.APIpatch(func, patch_text)Apply the patchpatch_textto the source of functionfunc.funcmay
be either a function, or a string providing the dotted path to import a
function.If the patch is invalid, for example the context lines don’t match,ValueErrorwill be raised, with a message that includes all the output from
thepatchutility.Note thatpatch_textwill betextwrap.dedent()’ed, but leading
whitespace will not be removed. Therefore the correct way to include the patch
is with a triple-quoted string with a backslash -"""\- which starts the
string and avoids including the first newline. A final newline is not required
and will be automatically added if not present.Example:importpatchydefsample():return1patchy.patch(sample,"""\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""",)print(sample())# prints 2mc_patchface(func, patch_text)An alias forpatch, so you can meme it up by callingpatchy.mc_patchface().unpatch(func, patch_text)Unapply the patchpatch_textfrom the source of functionfunc. This is
the reverse ofpatch()ing it, and callspatch--reverse.The same error and formatting rules apply as inpatch().Example:importpatchydefsample():return2patchy.unpatch(sample,"""\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""",)print(sample())# prints 1temp_patch(func, patch_text)Takes the same arguments aspatch. Usable as a context manager or function
decorator to wrap code with a call topatchbefore andunpatchafter.Context manager example:defsample():return1234patch_text="""\@@ -1,2 +1,2 @@
     def sample():
    -    return 1234
    +    return 5678
    """withpatchy.temp_patch(sample,patch_text):print(sample())# prints 5678Decorator example, using the samesampleandpatch_text:@patchy.temp_patch(sample,patch_text)defmy_func():returnsample()==5678print(my_func())# prints Truereplace(func, expected_source, new_source)Check that function or dotted path to functionfunchas an AST matchingexpected_source, then replace its inner code object with source compiled
fromnew_source. If the AST check fails,ValueErrorwill be raised with
current/expected source code in the message. In the author’s opinion it’s
preferable to callpatch()so your call makes it clear to see what is being
changed aboutfunc, but usingreplace()is simpler as you don’t have to
make a patch and there is no subprocess call to thepatchutility.Note bothexpected_sourceandnew_sourcewill betextwrap.dedent()’ed, so the best way to include their source is with a
triple quoted string with a backslash escape on the first line, as per the
example below.If you want, you can passexpected_source=Noneto avoid the guard against
your target changing, but this is highly unrecommended as it means if the
original function changes, the call toreplace()will continue to silently
succeed.Example:importpatchydefsample():return1patchy.replace(sample,"""\
    def sample():
        return 1
    ""","""\
    def sample():
        return 42
    """,)print(sample())# prints 42How to Create a PatchSave the source of the function of interest (and nothing else) in a.pyfile, e.g.before.py:deffoo():print("Change me")Make sure you dedent it so there is no whitespace before thedef, i.e.dis the first character in the file. For example if you wanted to patch
thebar()method below:classFoo:defbar(self,x):returnx*2…you would put just the method in a file like so:defbar(self,x):returnx*2However we’ll continue with the first examplebefore.pysince it’s
simpler.Copy that.pyfile, to e.g.after.py, and make the changes you
want, such as:deffoo():print("Changed")Rundiff, e.g.diff-ubefore.py after.py. You will get output like:diff --git a/Users/chainz/tmp/before.py b/Users/chainz/tmp/after.pyindex e6b32c6..31fe8d9 100644--- a/Users/chainz/tmp/before.py+++ b/Users/chainz/tmp/after.py@@ -1,2 +1,2 @@def foo():-    print("Change me")+    print("Changed")The filenames are not necessary forpatchyto work. Take only from the
first@@line onwards into the multiline string you pass topatchy.patch():patchy.patch(foo,"""\
    @@ -1,2 +1,2 @@
     def foo():
    -    print("Change me")
    +    print("Changed")
    """,)