Brett Cannon added the comment: On Fri, Jun 21, 2013 at 11:11 AM, Antoine Pitrou <rep...@bugs.python.org>wrote:
> > Antoine Pitrou added the comment: > > > I'm attracted to the idea of making a loader lazy by simply doing > > something > > like ``class LazySourceFileLoader(LazyMixin, SourceFileLoader): > > ...``. > > But then you have to make a specific Lazy subclass for each delegated > loader implementation. With a proxy class you would simply proxy each > loader instance: > > existing_loader = SourceFileLoader(...) > lazy_loader = LazyLoader(existing_loader) > ... But the point I tried to make earlier is that approach doesn't work directly when you are creating the loader instances dynamically within a finder. E.g. FileFinder ( http://docs.python.org/3/library/importlib.html#importlib.machinery.FileFinder) instantiates the loader for you. Now you could tweak things to make this work, but it isn't quite as straightforward as you suggest because of this need to have a callable for finders to use to create new loaders: class LazyProxy(importlib.abc.Loader): def __init__(self, loader): self.loader = loader def __call__(self, *args, **kwargs): self.args = args self.kwargs = kwargs return self def load_module(self, fullname): # XXX ignoring sys.modules details, e.g. if module already existed. lazy_module = LazyModule(fullname, proxy=self, name=fullname) sys.modules[fullname] = lazy_module return lazy_module class LazyModule(types.ModuleType): def __init__(*args, proxy, name, **kwargs): self.__proxy = proxy self.__name = name super().__init__(*args, **kwargs) def __getattribute__(self, attr): self.__class__ = Module state = self.__dict__.copy() loader = self.__proxy.loader(*self.proxy.args, **self.proxy.kwargs) # XXX ignoring sys.modules details, e.g. removing module on load failure. loader.load_module(self.__name) self.__dict__.update(state) return getattr(module, attr) That's all totally untested (not even verified to compile) but I think that would do what you are suggesting. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue18275> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com