{{Short description|Technique to dynamically modify runtime code}} '''Monkey patch''' is the act of dynamically modifying the runtime code (not the source code) of a dynamic programming language, and it is the information (data/code) used to modify the runtime code. Monkey patching adds or replaces programming aspects like methods, classes, attributes, and functions in memory. Modifying the runtime code allows for modifying the behavior of third-party software without maintaining a modified version of the source code.
The term ''monkey patch'' seems to have come from an earlier term, ''guerrilla patch'', which referred to changing code sneakily – and possibly incompatibly with other such patches – at runtime. The word ''guerrilla'', nearly homophonous with ''gorilla'', became ''monkey'', possibly to make the patch sound less intimidating.<ref name="plone"> {{cite web | url=https://docs.plone.org/appendices/glossary.html#term-Monkey-patch | url-status=live | title=Glossary — Definition of 'Monkey patch' | website=Plone Content Management System | archive-url=https://web.archive.org/web/20210122092034/https://docs.plone.org/appendices/glossary.html#term-Monkey-patch | archive-date=2021-01-22 | access-date=2021-07-02 | quote=when someone created a guerrilla patch very carefully and tried to avoid any battles, they tried to make it sound less forceful by calling it a monkey patch }} </ref>
Despite the name's suggestion, a monkey patch is sometimes the official method of extending a program. For example, web browsers such as Firefox and Internet Explorer used to encourage this, although today browsers (including Firefox) support extension differently.<ref>{{Cite book |last1=Guha |first1=Arjun |last2=Fredrikson |first2=Matthew |last3=Livshits |first3=Benjamin |last4=Swamy |first4=Nikhil |chapter=Verified Security for Browser Extensions |date=22–25 May 2011 |title=2011 IEEE Symposium on Security and Privacy |pages=115–130 |doi=10.1109/SP.2011.36|isbn=978-1-4577-0147-4 }}</ref>
Monkey patch varies depending upon context. In Ruby,<ref>{{cite web |last1=Nutter |first1=Charles Oliver |title=Refining Ruby |url=http://blog.headius.com/2012/11/refining-ruby.html |website=Charles Oliver Nutter}}</ref> Python,<ref>{{cite web |last=Biswal |first=Bimal |title=Monkey Patching in Python |url=http://www.mindfiresolutions.com/Monkey-Patching-in-Python-1238.php |url-status=dead |accessdate=9 December 2013 |work=Software Technology Tips |publisher=Mindfire Solutions |archive-date=22 August 2012 |archive-url=https://web.archive.org/web/20120822051047/http://www.mindfiresolutions.com/Monkey-Patching-in-Python-1238.php }}</ref> and other languages, monkey patch refers only to dynamic modification of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired. Other forms of modifying classes at runtime have different names. For example, in Zope and Plone, security patches are often delivered using dynamic class modification, but they are called ''hot fixes''.{{Citation needed|date=June 2009}}
==Pitfalls== Some pitfalls of monkey patching:
; Incompatibility A new release of the patched software may break the patch. For this reason, a monkey patch may be conditional and thus only applied if appropriate.<ref>{{cite web |last1=Zakas |first1=Nicholas C. |title=Maintainable JavaScript: Don't modify objects you don't own - Human Who Codes |url=https://humanwhocodes.com/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/ |website=Human Who Codes |language=en |date=2 March 2010}}</ref>
; Overwriting If the same method is patched multiple times, then only the last one is used; the other patches have no effect, unless monkey patches are written with a pattern like ''alias_method_chain''.<ref>{{cite web |title=New in Rails: Module#alias_method_chain |url=https://rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain |website=Ruby on Rails |language=en}}</ref>
; Confusion A monkey patch creates a discrepancy between the source code and actual behavior that can confuse developers. For example, the Linux kernel detects proprietary and other third-party modules such as the Nvidia driver, which tamper with kernel structures, so that developers will not waste their time trying to debug a problem that they cannot fix.<ref>{{Cite web|title=Tainted kernels — The Linux Kernel documentation|url=https://www.kernel.org/doc/html/v4.15/admin-guide/tainted-kernels.html|access-date=2020-07-12|website=www.kernel.org}}</ref>
; Chaos A monkey patch can contain malicious code that attacks the program, or other patches. For example, in 2009, Giorgio Maone, developer of NoScript, attacked the Adblock Plus extension for Firefox, adding exceptions so that advertisements on his websites would work. The offending code also made sure that if the user attempted to remove the exceptions, they would be added again. An escalating war ensued with new adblock rules pushed to users, followed by Maone sabotaging them, which eventually led to Mozilla stepping in to change policies regarding add-ons.<ref>{{Cite web|last=Paul|first=Ryan|date=2009-05-04|title=Mozilla ponders policy change after Firefox extension battle|url=https://arstechnica.com/information-technology/2009/05/mozilla-ponders-policy-change-after-firefox-extension-battle/|access-date=2020-07-12|website=Ars Technica|language=en-us}}</ref>
== Examples == The following monkey patches the value of pi in the standard Python math library to make it compliant with the Indiana pi bill.
<syntaxhighlight lang="pycon"> >>> import math >>> math.pi 3.141592653589793 >>> math.pi = 3.2 # monkey-patch the value of Pi in the math module >>> math.pi 3.2 </syntaxhighlight>
The next time Python is started, the value of pi will be what it was before the patch: {{code|3.141592653589793}}.
==See also== {{Sister project links|wikt=monkey patch|auto=yes}} * Advice (programming) * Aspect-oriented programming * Dynamic loading * Extension method * Objective-C category * Polyfill * Self-modifying code
==References== {{reflist}}
{{DEFAULTSORT:Monkey Patch}} Category:Object-oriented programming Category:Programming constructs