(original) (raw)
On May 23, 2012 9:02 AM, "Nick Coghlan" <ncoghlan@gmail.com> wrote:
>
> On Wed, May 23, 2012 at 10:31 PM, Eric V. Smith <eric@trueblade.com> wrote:
> > On 05/22/2012 09:49 PM, PJ Eby wrote:
> >> It shouldn't - all you should need is to use
> >> getattr(sys.modules\[self.modname\], self.attr) instead of referencing a
> >> parent path object directly.
> >
> > The problem isn't the lookup, it's coming up with self.modname and
> > self.attr. As it currently stands, PathFinder.find\_module is given the
> > parent path, not the module name and attribute name used to look up the
> > parent path using sys.modules and getattr.
>
> Right, that's what PJE and I were discussing. Instead of passing in
> the path object directly, you can instead pass an object that \*lazily\*
> retrieves the path object in its \_\_iter\_\_ method:
>
> � �class LazyIterable:
> � � � �"""On iteration, retrieves a reference to a named iterable and
> returns an iterator over that iterable"""
> � � � �def \_\_init\_\_(self, modname, attribute):
> � � � � � �self.modname = modname
> � � � � � �self.attribute = attribute
> � � � �def \_\_iter\_\_(self):
> � � � � � �mod = import\_module(self.modname) # Will almost always get
> a hit directly in sys.modules
> � � � � � �return iter(getattr(mod, self.attribute)
>
> Where importlib currently passes None or sys.path as the path argument
> to find\_module(), instead pass "LazyIterable('sys', 'path')" and where
> it currently passes package.\_\_path\_\_, instead pass
> "LazyIterable(package.\_\_name\_\_, '\_\_path\_\_')".
>
> The existing for loop iteration and tuple() calls should then take
> care of the lazy lookup automatically.
>
> That way, the only code that needs to know the values of modname and
> attribute is the code that already has access to those values.
Perhaps calling it a ModulePath instead of a LazyIterable would be better?
Also, this is technically a change from PEP 302, which says the actual sys.path or \_\_path\_\_ are passed to find\_module().� I'm not sure whether any find\_module() code ever written actually \*cares\* about this, though.� (Especially if, as I believe I understand in this context, we're only talking about meta-importers.)