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 toFalse
.Type: bool
-
-
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 attributeobj
. 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: - destination (object) – See the
destination
attribute. - name (str) – See the
name
attribute. - obj (object) – See the
obj
attribute. - settings (gorilla.Settings) – See the
settings
attribute.
- destination (object) – See the
-
-
gorilla.
apply
(patch, id='default')[source]¶ Apply a patch.
The patch’s
obj
attribute is injected into the patch’sdestination
under the patch’sname
.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 settingSettings.allow_hit
is set toTrue
.Note
If both the attributes
Settings.allow_hit
andSettings.store_hit
areTrue
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 toTrue
when applying the patch and overriding an existing attribute.