详解Python编程中对Monkey Patch猴子补丁开发方式的运用


Posted in Python onMay 27, 2016

Monkey patch就是在运行时对已有的代码进行修改,达到hot patch的目的。Eventlet中大量使用了该技巧,以替换标准库中的组件,比如socket。首先来看一下最简单的monkey patch的实现。

class Foo(object):
  def bar(self):
    print 'Foo.bar'

def bar(self):
  print 'Modified bar'

Foo().bar()

Foo.bar = bar

Foo().bar()

由于Python中的名字空间是开放,通过dict来实现,所以很容易就可以达到patch的目的。

Python namespace

Python有几个namespace,分别是

  • locals
  • globals
  • builtin

其中定义在函数内声明的变量属于locals,而模块内定义的函数属于globals。

Python module Import & Name Lookup

当我们import一个module时,python会做以下几件事情

  • 导入一个module
  • 将module对象加入到sys.modules,后续对该module的导入将直接从该dict中获得
  • 将module对象加入到globals dict中

当我们引用一个模块时,将会从globals中查找。这里如果要替换掉一个标准模块,我们得做以下两件事情

将我们自己的module加入到sys.modules中,替换掉原有的模块。如果被替换模块还没加载,那么我们得先对其进行加载,否则第一次加载时,还会加载标准模块。(这里有一个import hook可以用,不过这需要我们自己实现该hook,可能也可以使用该方法hook module import)
如果被替换模块引用了其他模块,那么我们也需要进行替换,但是这里我们可以修改globals dict,将我们的module加入到globals以hook这些被引用的模块。
Eventlet Patcher Implementation

现在我们先来看一下eventlet中的Patcher的调用代码吧,这段代码对标准的ftplib做monkey patch,将eventlet的GreenSocket替换标准的socket。

from eventlet import patcher

# *NOTE: there might be some funny business with the "SOCKS" module
# if it even still exists
from eventlet.green import socket

patcher.inject('ftplib', globals(), ('socket', socket))

del patcher

inject函数会将eventlet的socket模块注入标准的ftplib中,globals dict被传入以做适当的修改。

让我们接着来看一下inject的实现。

__exclude = set(('__builtins__', '__file__', '__name__'))

def inject(module_name, new_globals, *additional_modules):
  """Base method for "injecting" greened modules into an imported module. It
  imports the module specified in *module_name*, arranging things so
  that the already-imported modules in *additional_modules* are used when
  *module_name* makes its imports.

  *new_globals* is either None or a globals dictionary that gets populated
  with the contents of the *module_name* module. This is useful when creating
  a "green" version of some other module.

  *additional_modules* should be a collection of two-element tuples, of the
  form (, ). If it's not specified, a default selection of
  name/module pairs is used, which should cover all use cases but may be
  slower because there are inevitably redundant or unnecessary imports.
  """
  if not additional_modules:
    # supply some defaults
    additional_modules = (
      _green_os_modules() +
      _green_select_modules() +
      _green_socket_modules() +
      _green_thread_modules() +
      _green_time_modules())

  ## Put the specified modules in sys.modules for the duration of the import
  saved = {}
  for name, mod in additional_modules:
    saved[name] = sys.modules.get(name, None)
    sys.modules[name] = mod

  ## Remove the old module from sys.modules and reimport it while
  ## the specified modules are in place
  old_module = sys.modules.pop(module_name, None)
  try:
    module = __import__(module_name, {}, {}, module_name.split('.')[:-1])

    if new_globals is not None:
      ## Update the given globals dictionary with everything from this new module
      for name in dir(module):
        if name not in __exclude:
          new_globals[name] = getattr(module, name)

    ## Keep a reference to the new module to prevent it from dying
    sys.modules['__patched_module_' + module_name] = module
  finally:
    ## Put the original module back
    if old_module is not None:
      sys.modules[module_name] = old_module
    elif module_name in sys.modules:
      del sys.modules[module_name]

    ## Put all the saved modules back
    for name, mod in additional_modules:
      if saved[name] is not None:
        sys.modules[name] = saved[name]
      else:
        del sys.modules[name]

  return module

注释比较清楚的解释了代码的意图。代码还是比较容易理解的。这里有一个函数__import__,这个函数提供一个模块名(字符串),来加载一个模块。而我们import或者reload时提供的名字是对象。

if new_globals is not None:
  ## Update the given globals dictionary with everything from this new module
  for name in dir(module):
    if name not in __exclude:
      new_globals[name] = getattr(module, name)

这段代码的作用是将标准的ftplib中的对象加入到eventlet的ftplib模块中。因为我们在eventlet.ftplib中调用了inject,传入了globals,而inject中我们手动__import__了这个module,只得到了一个模块对象,所以模块中的对象不会被加入到globals中,需要手动添加。
这里为什么不用from ftplib import *的缘故,应该是因为这样无法做到完全替换ftplib的目的。因为from … import *会根据__init__.py中的__all__列表来导入public symbol,而这样对于下划线开头的private symbol将不会导入,无法做到完全patch。

Python 相关文章推荐
python中wx将图标显示在右下角的脚本代码
Mar 08 Python
python调用fortran模块
Apr 08 Python
Python处理JSON时的值报错及编码报错的两则解决实录
Jun 26 Python
python开发环境PyScripter中文乱码问题解决方案
Sep 11 Python
详解 Python 读写XML文件的实例
Aug 02 Python
python进阶_浅谈面向对象进阶
Aug 17 Python
django反向解析URL和URL命名空间的方法
Jun 05 Python
python学生信息管理系统(初级版)
Oct 17 Python
Pandas之groupby( )用法笔记小结
Jul 23 Python
Python运算符+与+=的方法实例
Feb 18 Python
pytorch中的 .view()函数的用法介绍
Mar 17 Python
Python万能模板案例之matplotlib绘制直方图的基本配置
Apr 13 Python
Python程序中的观察者模式结构编写示例
May 27 #Python
Windows下python2.7.8安装图文教程
May 26 #Python
Java Web开发过程中登陆模块的验证码的实现方式总结
May 25 #Python
剖析Python的Twisted框架的核心特性
May 25 #Python
实例解析Python的Twisted框架中Deferred对象的用法
May 25 #Python
详解Python的Twisted框架中reactor事件管理器的用法
May 25 #Python
使用Python的Twisted框架编写非阻塞程序的代码示例
May 25 #Python
You might like
php 中文字符串首字母的获取函数分享
2013/11/04 PHP
PHP编译安装时常见错误解决办法
2015/05/28 PHP
Yii框架上传图片用法总结
2016/03/28 PHP
php ActiveMQ的安装与使用方法图文教程
2020/02/23 PHP
ExtJS Window 最小化的一种方法
2009/11/18 Javascript
禁止ajax缓存获取程序最新数据的方法
2013/11/19 Javascript
JS实现仿百度输入框自动匹配功能的示例代码
2014/02/19 Javascript
Visual Studio中js调试的方法图解
2014/06/30 Javascript
深入理解JavaScript系列(17):面向对象编程之概论详细介绍
2015/03/04 Javascript
javascript实现控制文字大中小显示
2015/04/28 Javascript
JS遍历数组及打印数组实例分析
2016/01/21 Javascript
分享10个优化代码的CSS和JavaScript工具
2016/05/11 Javascript
js判断checkbox是否选中个数的方法(超简单)
2016/08/19 Javascript
jQuery实现的无限级下拉菜单功能示例
2016/09/12 Javascript
JS实现简易刻度时钟示例代码
2017/03/11 Javascript
js canvas实现QQ拨打电话特效
2017/05/10 Javascript
Angular中的$watch方法详解
2017/09/18 Javascript
用Vue.js在浏览器中实现裁剪图像功能
2019/06/18 Javascript
对layui中的onevent 和event的使用详解
2019/09/06 Javascript
vue父组件给子组件的组件传值provide inject的方法
2019/10/23 Javascript
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
Vue实现移动端拖拽交换位置
2020/07/29 Javascript
Vue 实现监听窗口关闭事件,并在窗口关闭前发送请求
2020/09/01 Javascript
使用PDB模式调试Python程序介绍
2015/04/05 Python
DataFrame中去除指定列为空的行方法
2018/04/08 Python
Selenium定时刷新网页的实现代码
2018/10/31 Python
基于YUV 数据格式详解及python实现方式
2019/12/09 Python
在求职信中如何凸显个人优势
2013/10/30 职场文书
室内设计专业学生的自我评价分享
2013/11/27 职场文书
本科生的职业生涯规划范文
2014/01/09 职场文书
幼儿教师思想汇报
2014/01/10 职场文书
卫校中专生的自我评价
2014/01/15 职场文书
入党自我鉴定
2014/03/25 职场文书
2014年变电站工作总结
2014/12/19 职场文书
食品药品安全责任书
2015/05/11 职场文书
MySQL的存储过程和相关函数
2022/04/26 MySQL