Author brett.cannon
Recipients barry, brett.cannon, eric.smith
Date 2013-06-21.22:35:08
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1371854108.78.0.634044723991.issue17621@psf.upfronthosting.co.za>
In-reply-to
Content
While seing if it was worth making isinstance work with super(), I came up with this at Antoine's suggestion of using a proxy instead of a mixin:


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)
History
Date User Action Args
2013-06-21 22:35:08brett.cannonsetrecipients: + brett.cannon, barry, eric.smith
2013-06-21 22:35:08brett.cannonsetmessageid: <1371854108.78.0.634044723991.issue17621@psf.upfronthosting.co.za>
2013-06-21 22:35:08brett.cannonlinkissue17621 messages
2013-06-21 22:35:08brett.cannoncreate