Re: [Pyobjc-dev] PyObjcC class method swizzling
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2009-09-29 20:41:25
|
On 19 Sep, 2009, at 14:02, Lukas Pitschl | Dressy Vagabonds wrote: > 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 Have you tried adding "isClassMethod=True" to the argument list of objc.selector? Your current code creates an instance method. Ronald |