[Pyobjc-dev] PyObjcC class method swizzling
Brought to you by:
ronaldoussoren
From: Lukas P. | D. V. <lu...@dr...> - 2009-09-19 12:02:57
|
Hi everyone! I've just recently started to explore the power of PyObjC by rewriting the GPGMail plugin and I'm more than impressed. A big compliment to the developers! Unfortunately at the moment I'm stuck with class method swizzling. I've found a very nice decorator which can be used to swizzle instance methods: def swizzle(*args): cls, SEL = args def decorator(func): oldIMP = cls.instanceMethodForSelector_(SEL) def wrapper(self, *args, **kwargs): return func(self, oldIMP, *args, **kwargs) newMethod = objc.selector(wrapper, selector = oldIMP.selector, signature = oldIMP.signature) objc.classAddMethod(cls, SEL, newMethod) return wrapper return decorator My poor attempt to convert this into a decorator which swizzles class methods looks like the one below, but unfortunately doesn't work. def swizzleClassMethod(*args): cls, SEL = args def decorator(func): oldIMP = cls.methodForSelector_(SEL) def wrapper(klass, *args, **kwargs): return func(klass, oldIMP, *args, **kwargs) newMethod = objc.selector(wrapper, selector = oldIMP.selector, signature = oldIMP.signature) objc.classAddMethod(cls, SEL, newMethod) return wrapper return decorator I'd really appreciate it, if someone could help me to find out how to do this. What I wanna do with that is replacing the + sharedPreferences class method of the NSPreferences class with my own implementation and call the original method from within there. Please anyone point me into the right direction, so I can finally finish the GPGMail plugin. Thanks in advance Lukas |