Core

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.

allow_hit

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.

store_hit

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.

__init__(**kwargs)[source]

Constructor.

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.

destination

obj – Patch destination.

name

str – Name of the attribute at the destination.

obj

obj – Attribute value.

settings

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

Warning

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]

Constructor.

Parameters:

gorilla.apply(patch)[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.name, 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.

Note

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.