[Pyobjc-dev] method definition oddity
Brought to you by:
ronaldoussoren
From: Just v. R. <ju...@le...> - 2003-01-17 22:21:57
|
Weird thing: >>> from Foundation import NSObject >>> class MyClass(NSObject): ... def foo(self): ... pass ... bar = foo ... >>> f = MyClass.alloc().init() >>> f.foo <selector foo of <MyClass objective-c instance 0xbe5280>> >>> f.bar <selector foo of <MyClass objective-c instance 0xbe5280>> >>> f.respondsToSelector_("foo") 1 >>> f.respondsToSelector_("bar") 0 >>> Are the selectors built from func.func_name instead of the key in the method dict passed to the meta class at class-build-time? [ ...doing some research... ] Ah, yes, that's it. This patch fixes it: Index: class-builder.m =================================================================== RCS file: /cvsroot/pyobjc/pyobjc/Modules/objc/class-builder.m,v retrieving revision 1.11 diff -c -r1.11 class-builder.m *** class-builder.m 6 Jan 2003 21:15:17 -0000 1.11 --- class-builder.m 17 Jan 2003 22:13:45 -0000 *************** *** 440,446 **** METHOD meth; int is_class_method = 0; ! pyname = PyObject_GetAttrString(value, "__name__"); if (pyname == NULL) continue; name = PyString_AS_STRING(pyname); --- 440,446 ---- METHOD meth; int is_class_method = 0; ! pyname = key; if (pyname == NULL) continue; name = PyString_AS_STRING(pyname); This also fixes a memory leak: pyname was never decref'd. I'd gladly check this in if ok. Although: the pyname variable could then go away altogether. Btw. it would be more efficient to not build a key list, but use the PyDict_Next API to iterate over the dict items (see http://www.python.org/doc/current/api/dictObjects.html). I doubt this part of the code is crucial for the startup time, so I don't think this optimization should be a priority... (It would clean up the code somewhat, though.) Just |