First of all, thank you everyone for putting so much work into the project and keeping it going with the newest releases of OS X! I'd like to share my latest experiences on using decorators with PyObjC and I hope that this contributes to the project.
After porting an old project to the newest releases of Python and PyObjC I ran into some problems with using decorators. It appears that methods in classes that inherit from PyObjC classes can’t have
**kwargs as arguments (which would be necessary to write a method decorator).
You can reproduce it with the following code sample:
import AppKit import objc class RandomNSObject(AppKit.NSObject): def init(self): self = objc.super(RandomNSObject, self).init() return self def randomFunction_args_(*args, **kwargs): print(*args, **kwargs) t = RandomNSObject.alloc().init() t.randomFunction_args_("foo")
This code fails with the following error message:
TypeError: Objective-C callable methods must take at least one argument. What I would expect from the above code sample (and the print statement inside
randomFunction_args_) is to print out
self and the string
Now you may be wondering why it would be useful to be able to define functions with
**kwargs. The reason for that are Python decorators. If arguments and keyword arguments are not passed through dynamically, we are not able to write a wrapper around a PyObjC method that takes any arguments and passes there right through to the original method. With the newest version of PyObjC, I'm not able to use decorators anymore unless I specify the arguments in the decorator function explicitly (instead of using args and kwargs to make the decorator work with different kinds of functions). It used to work fine in the past (many years ago), so I believe the way how functions are translated into Obj-C must have changed somewhere on the way.
I am submitting this as a bug although I'm not sure if this really is a bug or maybe expected or even desired behavior. However, it doesn't allow to write code in a pythonic way if we are unable to use decorators.