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

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.

Type:bool
store_hit

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.

Type:bool
__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

Patch destination.

Type:obj
name

Name of the attribute at the destination.

Type:str
obj

Attribute value.

Type:obj
settings

Settings. If None, the default settings are used.

Type:gorilla.Settings or None

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, id='default')[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.
  • id (str) – When applying a stack of patches on top of a same attribute, this identifier allows to pinpoint a specific original attribute if needed.
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.


gorilla.revert(patch)[source]

Revert a patch.

Parameters:patch (gorilla.Patch) – Patch.

Note

This is only possible if the attribute Settings.store_hit was set to True when applying the patch and overriding an existing attribute.