Skip to main content

Patch the inner source of python functions at runtime.

Project description

A patchy pirate.

Patch the inner source of python functions at runtime.

A quick example, making a function that returns 1 instead return 9001:

>>> def sample():
...    return 1
>>> patchy.patch(sample, """\
...     @@ -1,2 +1,2 @@
...      def sample():
...     -    return 1
...     +    return 9001
...     """)
>>> sample()

Patchy 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.


Use pip:

pip install patchy

Tested on Python 2.7 and 3.6.


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 standard patch commandline 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 the patch command)
  • 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.


The standard library function inspect.getsource() is used to retrieve the source code of the function, the patch is applied with the commandline utility patch, 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, unlike mock.patch.

A little special treatment is given to instancemethod, classmethod, and staticmethod objects to make sure the underlying function is what gets patched and that you don’t have to worry about the details.


patch(func, patch_text)

Apply the patch patch_text to the source of function func.

If the patch is invalid, for example the context lines don’t match, ValueError will be raised, with a message that includes all the output from the patch utility.

Note that patch_text will be textwrap.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.


import patchy

def sample():
    return 1

patchy.patch(sample, """\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""")

print(sample())  # prints 2

mc_patchface(func, patch_text)

An alias for patch, so you can meme it up by calling patchy.mc_patchface().

unpatch(func, patch_text)

Unapply the patch patch_text from the source of function func. This is the reverse of patch()ing it, and calls patch --reverse.

The same error and formatting rules apply as in patch().


import patchy

def sample():
    return 2

patchy.unpatch(sample, """\
    @@ -2,2 +2,2 @@
    -    return 1
    +    return 2""")

print(sample())  # prints 1

temp_patch(func, patch_text)

Usable as a context manager or function decorator to wrap code with a call to patch before and unpatch after.

Context manager example:

def sample():
    return 1234

patch_text = """\
    @@ -1,2 +1,2 @@
     def sample():
    -    return 1234
    +    return 5678

with patchy.temp_patch(sample, patch_text):
    print(sample())  # prints 5678

Decorator example, using the same sample and patch_text:

@patchy.temp_patch(sample, patch_text)
def my_func():
    return sample() == 5678

print(my_func())  # prints True

How to Create a Patch

  1. Save the source of the function of interest (and nothing else) in a .py file, e.g.

    def foo():
        print("Change me")

    Make sure you dedent it so there is no whitespace before the def, i.e. d is the first character in the file. For example if you wanted to patch the bar() method below:

    class Foo():
        def bar(self, x):
            return x * 2

    …you would put just the method in a file like so:

    def bar(self, x):
        return x * 2

    However we’ll continue with the first example since it’s simpler.

  2. Copy that .py file, to e.g., and make the changes you want, such as:

    def foo():
  3. Run diff, e.g. diff You will get output like:

    diff --git a/Users/chainz/tmp/ b/Users/chainz/tmp/
    index e6b32c6..31fe8d9 100644
    --- a/Users/chainz/tmp/
    +++ b/Users/chainz/tmp/
    @@ -1,2 +1,2 @@
     def foo():
    -    print("Change me")
    +    print("Changed")
  4. The filenames are not necessary for patchy to work. Take only from the first @@ line onwards into the multiline string you pass to patchy.patch():

    patchy.patch(foo, """\
        @@ -1,2 +1,2 @@
         def foo():
        -    print("Change me")
        +    print("Changed")


Pending Release

  • (Insert new release notes below this line)

1.3.2 (2017-03-13)

  • Support freevars and methods that call mangled methods.

1.3.1 (2016-05-24)

  • Fixed to not fix the version of six required.
  • Fixed install instruction in README.
  • Added patchy.mc_patchface() as an alias for patchy.patch() because memes.

1.3.0 (2015-12-09)

  • Remove dependency on pylru by using a simpler caching strategy

1.2.0 (2015-07-23)

  • Pirate mascot!
  • Patching caches the patched and unpatched versions, so unpatching and repeat patching/unpatching are both faster
  • Patching doesn’t attach an attribute to the function object any more

1.1.0 (2015-06-16)

  • Fixed code compilation to use the __future__ flags from the function that is being patched
  • Added unpatch method
  • Added temp_patch context manager/decorator

1.0.0 (2015-06-09)

  • First release on PyPI, featuring patch function.

Project details

Release history Release notifications

History Node


This version
History Node


History Node


History Node


History Node


History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
patchy-1.3.2-py2.py3-none-any.whl (11.6 kB) Copy SHA256 hash SHA256 Wheel 2.7 Mar 13, 2017
patchy-1.3.2.tar.gz (14.0 kB) Copy SHA256 hash SHA256 Source None Mar 13, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page