# shallowutil.py -- remotefilelog utilities # # Copyright 2014 Facebook, Inc. # # This software may be used and distributed according to the terms of the # GNU General Public License version 2 or any later version. def interposeclass(container, classname): '''Interpose a class into the hierarchies of all loaded subclasses. This function is intended for use as a decorator. import mymodule @replaceclass(mymodule, 'myclass') class mysubclass(mymodule.myclass): def foo(self): f = super(mysubclass, self).foo() return f + ' bar' Existing instances of the class being replaced will not have their __class__ modified, so call this function before creating any objects of the target type. Note that this doesn't actually replace the class in the module -- that can cause problems when using e.g. super() to call a method in the parent class. Instead, new instances should be created using a factory of some sort that this extension can override. ''' def wrap(cls): oldcls = getattr(container, classname) oldbases = (oldcls,) newbases = (cls,) for subcls in oldcls.__subclasses__(): if subcls is not cls: assert subcls.__bases__ == oldbases subcls.__bases__ = newbases return cls return wrap