[Pyobjc-dev] Re: 1.0?
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-06-18 18:22:26
|
On Wednesday, Jun 18, 2003, at 20:00 Europe/Amsterdam, Just van Rossum wrote: > Ronald Oussoren wrote: > >> I think this is related to finding all classes for the frameworks when >> loading. I'm going to test if this is really true. It it is true we >> need to add a module-like object to sys.modules when someone imports >> Foundation/AppKit/.... That object should initialize classes only when >> someone tries to use them, instead of always initializing all classes. >> E.g. it will have a __getattr__ like this: >> >> def __getattr__(self, name): >> cls = objc.lookUpClass(name) >> if definedInBundle(self.__bundle, cls): >> setattr(self, name, cls) >> return cls >> else: >> raise ValueError, "No such attribute" >> >> Additional work is needed because of 'from AppKit import *', if >> someone accesses __all__ we still have to find all classes at once. > > Quick idea: why not use a static list of class names, if that can save > us time? That is a bit risky, if the __all__ contains names that are not available you'll get an import-error when doing an 'import *'. Maybe we can find an easy way to detect what classes are available, it will probably suffice to do something like this: CLASSNAMES=( # 10.1 ( 'NSObject', ... ), # 10.2 ( ... ), # ... ) __all__ = [] for nameList in CLASSNAMES: try: objc.lookUpClass(nameList[0]) all.extend(nameList) except objc.error: pass E.g. probe for one of the classes added in a release and then add all classes defined in that release. That should be a lot cheaper than what we currently do. > > Something in between: make __all__ a static list, but have the > __getattr__ do a dynamic lookup anyway if the name isn't in the list. > > I noticed that for even minorly complex apps "from AppKit import *" is > indispensible, so I don't think a solution that only works if you > _don't_ do that is out of the question. > > To what extent could class objects be lazy? Ideal would be if > > from AppKit import NSSomeClass > > was really cheap, and that the class was only fully initialized if it > was used (subclasses or instantiated). We already build minimally initialized classes, the problem is that [NSBundle bundleForClass:] seems to be very expensive. That is at least what I remembered from the previous time this came up, I think bundleForClass did a lot of I/O for every class we look at. At the time there were more important issues, but I think this is the major technical issue with the bridge at the moment. Ronald |