Settings Define the patching behaviour.
Patch Describe all the information required to apply a patch.
apply Apply a patch.

class gorilla.Settings(**kwargs)[source]

Define the patching behaviour.


bool – A hit occurs when an attribute at the destination already exists with the name given by the patch. If False, the patch process won’t allow setting a new value for the attribute by raising an exception. Defaults to False.


bool – If True and allow_hit is also set to True, then any attribute at the destination that is hit is stored under a different name before being overwritten by the patch. Defaults to True.



Parameters:kwargs – Keyword arguments, see the attributes.

class gorilla.Patch(destination, name, obj, settings=None)[source]

Describe all the information required to apply a patch.


obj – Patch destination.


str – Name of the attribute at the destination.


obj – Attribute value.


gorilla.Settings or None – Settings. If None, the default settings are used.


It is highly recommended to use the output of the function get_attribute() for setting the attribute obj. This will ensure that the descriptor protocol is bypassed instead of possibly retrieving attributes invalid for patching, such as bound methods.

__init__(destination, name, obj, settings=None)[source]




Apply a patch.

The patch’s obj attribute is injected into the patch’s destination under the patch’s name.

This is a wrapper around calling setattr(patch.destination,, patch.obj).

Parameters:patch (gorilla.Patch) – Patch.
Raises:RuntimeError – Overwriting an existing attribute is not allowed when the setting Settings.allow_hit is set to True.


If both the attributes Settings.allow_hit and Settings.store_hit are True but that the target attribute seems to have already been stored, then it won’t be stored again to avoid losing the original attribute that was stored the first time around.