什么是猴子补丁?

2024-04-18 22:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

不,这不像那些东西。它只是在运行时动态替换属性。

例如,考虑一个具有方法get_data的类。此方法执行外部查找(例如,在数据库或web API上),类中的各种其他方法调用它。但是,在单元测试中,您不希望依赖于外部数据源,因此您可以动态地用返回一些固定数据的存根替换get_data方法。

因为Python类是可变的,而方法只是类的属性,所以您可以随心所欲地执行此操作—事实上,您甚至可以用完全相同的方式替换模块中的类和函数。

但是,正如commenter所指出的,在进行修补时要小心:

  1. 如果除了您的测试逻辑之外,还有其他任何东西也调用get_data,那么它也将调用您的monkey-patched替换,而不是原来的——这可能是好的,也可能是坏的。小心点。

  2. 如果存在在替换时也指向get_data函数的某个变量或属性,则此别名不会更改其含义,并将继续指向原始get_data。(为什么?Python只是将类中的名称get_data重新绑定到其他函数对象;其他名称绑定根本不受影响。

What is a monkey patch?

简单地说,monkey patching就是在程序运行时对模块或类进行更改。

使用示例

熊猫文档中有一个猴子修补的例子:

import pandas as pd
def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class
df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

要分解此问题,首先导入模块:

import pandas as pd

接下来,我们创建一个方法定义,它在任何类定义的范围之外都是未绑定和自由的(因为函数和未绑定方法之间的区别是毫无意义的,所以Python 3取消了未绑定方法):

def just_foo_cols(self):
    """Get a list of column names containing the string 'foo'

    """
    return [x for x in self.columns if 'foo' in x]

接下来,我们只需将该方法附加到要在其上使用的类:

pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class

然后我们可以在类的实例上使用该方法,完成后删除该方法:

df = pd.DataFrame([list(range(4))], columns=["A","foo","foozball","bar"])
df.just_foo_cols()
del pd.DataFrame.just_foo_cols # you can also remove the new method

名称损坏警告

如果您使用名称管理(在属性前面加上双下划线,这会更改名称,我不建议这样做),那么如果您这样做,就必须手动命名管理。既然我不推荐使用mangling这个名字,我就不在这里演示了。

测试示例

例如,我们如何在测试中使用这些知识?

假设我们需要模拟对外部数据源的数据检索调用,该调用会导致错误,因为我们希望在这种情况下确保正确的行为。我们可以对数据结构进行猴子修补以确保这种行为。(因此使用丹尼尔·罗斯曼建议的类似方法名:)

import datasource

def get_data(self):
    '''monkey patch datasource.Structure with this to simulate error'''
    raise datasource.DataRetrievalError

datasource.Structure.get_data = get_data

当我们测试它的行为依赖于这个方法引发错误时,如果正确实现,我们将在测试结果中得到这个行为。

执行上述操作将在流程的生命周期内更改Structure对象,因此您将希望在unittests中使用设置和拆卸来避免执行此操作,例如:

def setUp(self):
    # retain a pointer to the actual real method:
    self.real_get_data = datasource.Structure.get_data
    # monkey patch it:
    datasource.Structure.get_data = get_data

def tearDown(self):
    # give the real method back to the Structure object:
    datasource.Structure.get_data = self.real_get_data

(虽然上面的方法很好,但是使用mock库修补代码可能是一个更好的主意。mock的{}decorator比执行上述操作更不容易出错,这将需要更多的代码行,从而有更多的机会引入错误。我还没有审查mock中的代码,但我想它也会以类似的方式使用monkey补丁。)

A MonkeyPatch is a piece of Python code which extends or modifies other code at runtime (typically at startup).

一个简单的例子如下:

from SomeOtherProduct.SomeModule import SomeClass

def speak(self):
    return "ook ook eee eee eee!"

SomeClass.speak = speak

来源:Zope wiki上的{a1}页面。

相关问题 更多 >