由于Plone是基于python的,对Plone打补丁事实上是对Python模块打补丁,这种打补丁的方法我们称之为Monkey Patch,简写为MP。
MP的标准定义为:dynamic modifications of a class or module at runtime
使用MP的原则:
- 首先 MP不是一种推荐的方式
- MP只有在不得以的情况下使用
- MP应小心使用,充分测试
MP在Python的应用实例:
Original (foo.py)Patched version (my_foo.py)
class Foo:
def bar(self):
return 42
def my_bar(self):
return 43
from foo import Foo
Foo.bar = my_bar
# or (needed in some situations)
Foo.bar.func_code = my_bar.func_code
MP在Plone应用实例:
有一个专门的三方模块,collective.monkeypatcher
[table=98%]
ZCML-level configuration of patching information:
module-level patches
class-level patches
<configure...>
<include package="collective.monkeypatcher" />
<monkey:patch
description="ISE-42: OFS.Image.tag()"
class="Products.CMFCore.FSImage.FSImage"
original="tag"
replacement=".patches.tag"
/>
</configure>