如何通过猴子修补来替换课程?

时间:2010-09-23 18:09:30

标签: python class monkeypatching

如何替换ORM类 - 所以它不应该导致递归!!!

问题
原始类具有超级调用,当它被替换时 - 它会导致自我继承并导致最大递归深度超过异常 即,orm正在调用super(orm,self)....并且orm已被另一个继承原始orm的类所取代....

包裹!

addons  __init__.py  osv  run_app.py

./addons:
__init__.py  test_app1.py  test.py

./osv:
__init__.py  orm.py

orm.py

的内容
class orm_template(object):
    def __init__(self, *args, **kw):
        super(orm_template, self).__init__()    
    def fields_get(self, fields):
        return fields    
    def browse(self, id):
        return id

class orm(orm_template):
    def __init__(self, *args, **kw):
        super(orm, self).__init__(*args, **kw)    
    def fields_get(self, fields, context = None):
        return super(orm, self).fields_get(fields)    
    def read(self, fields):
        return fields

插件的内容/ init .py

import test    
def main(app):
    print "Running..."
    __import__(app, globals(), locals())

addons / test.py

的内容
from osv import orm
import osv
class orm(orm.orm):
    def __init__(self, *args, **kw):
        super(orm, self).__init__(*args, **kw)    
    def fields_get(self, *args, **kw):
        print "my fields get................."
        return super(orm, self).fields_get(*args, **kw)    
osv.orm.orm = orm
print "replaced.........................."

test_app1.py

的内容
from osv.orm import orm    
class hello(orm):
    _name = 'hellos'    
    def __init__(self, *args, **kw):
        super(hello, self).__init__(*args, **kw)    
print hello('test').fields_get(['name'])

run_app.py

的内容
import addons
addons.main('test_app1')

输出

>>>python run_app.py

replaced..........................
Running...
...
...
super(orm, self).__init__(*args, **kw)
RuntimeError: maximum recursion depth exceeded

我见过类似的question

1 个答案:

答案 0 :(得分:5)

您的addons/test.py需要获取并保留对原始orm.orm的引用,并使用该代码而不是替换版本。即:

from osv import orm
import osv
original_orm = osv.orm
class orm(original_orm):
    def __init__(self, *args, **kw):
        super(orm, self).__init__(*args, **kw)    
    def fields_get(self, *args, **kw):
        print "my fields get................."
        return super(orm, self).fields_get(*args, **kw)    
osv.orm.orm = orm
print "replaced.........................."

所以monkeypatched-in类继承自原始而不是自身,就像你在设置中一样。顺便说一句,如果你可以通过更好地设计osv模块来避免猴子修补(例如用w / setter函数设置什么是orm)你会更快乐; - )。