Re: [Pyobjc-dev] [ pyobjc-Bugs-836247 ] NSWindow.contentRectForFrameRect_styleMask_ not a class meth
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2003-11-05 11:25:29
|
On Nov 5, 2003, at 5:00 AM, Ronald Oussoren wrote: > Ideally one would be able to do NSObject.description().. which means > that the descriptor has to know about these "class or instance" > methods. It's also, as far as I know, not possible to override the > class implementation for a selector if an instance implementation > exists (from Python). > > Note that it is currently not possible to know of > 'NSObject.description' refers to the class or instance method due to > the way types are implemented in Python: > > obj.description() > > is basically implemented as: > > obj.__class__.description(obj) > > And furthermore Cocoa classes should 'feel' as much as Python classes > as possible, it should therefore be possible to call > 'NSObject.description(obj)'. Well the current semantics for instance method only and class method only are perfectly fine.. it's the occasions when a particular selector is implemented both ways where things start to get hairy. I'd rather have NSObject.description(obj) not work, if it meant that NSObject.description() did work. Even still, I think enough information is available to make it possible. What about something like this (sorta pseudocode, not in tp_descr_get C API). METH_CLASS = 1 << 0 METH_INST = 1 << 1 class Selector(object): EXPECTED_ARG_LEN = 0 def __init__(self, name, flags, boundklass=None, boundinst=None): self.name = name self.flags = flags self.boundklass = boundklass self.boundinst = boundinst def __get__(self, inst, klass): if inst is None: return Selector(self.name, self.flags, boundklass=klass) elif not (self.flags & METH_INST): # trying to get a pure class method from an instance, don't do that raise AttributeError, "%s is a class method" % (self.name,) # it's been acquired from an instance return Selector(self.name, self.flags, boundinst=inst, boundklass=klass) def __call__(self, *args): if self.boundklass is None and self.boundinst is None: raise ValueError, "Selector not initialized" elif self.boundinst is not None: # acquired from an instance.. return execute(self.boundinst, args) elif self.flags & METH_INST and len(args) > self.EXPECTED_ARG_LEN: # must be using class.instanceMethod(instance, ....) # so what if this breaks down on varargs? how are varargs wrapped anyway (if at all)? # those people can use a workaround or just not use klass.instanceMethod(instance, ...) return execute(args[0], args[1:]) else: return execute(self.boundklass, args) |