Message191602
On Fri, Jun 21, 2013 at 11:11 AM, Antoine Pitrou <report@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. |
|
Date |
User |
Action |
Args |
2013-06-21 17:51:14 | brett.cannon | set | recipients:
+ brett.cannon, pitrou, benjamin.peterson |
2013-06-21 17:51:13 | brett.cannon | link | issue18275 messages |
2013-06-21 17:51:13 | brett.cannon | create | |
|