[Import-SIG] PEP proposal: Per-Module Import Path
Nick Coghlan
ncoghlan at gmail.com
Fri Aug 2 02:56:41 CEST 2013
On 2 Aug 2013 00:44, "Eric Snow" <ericsnowcurrently at gmail.com> wrote:
>
>
> On Aug 1, 2013 8:36 AM, "Brett Cannon" <brett at python.org> wrote:
> > If you're going to do that, why stop at types.SimpleNamespace and not
move all the way to a module object? Then you can simply start moving to
APIs which take the module object to be operated on and the various methods
in the loader, etc. and just fill in details as necessary; that's what I
would do if I got to redesign the loader API today since it would simplify
load_module() and almost everything would just become a static method which
set the attribute on the module (e.g.
ExecutionLoader.get_filename('some.module') would become
ExecutionLoader.filename(module) or even ExecutionLoader.__file__(module)
which gets really meta as you can then have a decorator which checks for a
non-None value for that attribute on the module and then returns it as a
short-circuit instead of calling the method). Only drawback I see is it not
being easy to tell if a module has been initialized or not, but I don't
view that as a critical issue. IOW introduce new_module()/fresh_module().
> >
> > Even if types.SimpleNamespace is kept I do like the idea. Loaders could
shift to working only off of the object and have their __init__ method
standardized to take a single argument so what import is told about and
what loaders work with is the same. Basically it becomes a caching
mechanism of what finders can infer so that loaders can save themselves the
hassle without complicated init call signatures.
>
> This is pretty much exactly what I've been thinking about since PyCon.
The only difference is that I have a distinct ModuleSpec class and modules
would get a new __spec__ attribute.
And we can quit adding ever more magic attributes directly to the module
namespace. I like it.
With that model, things might look vaguely like:
1. Finders would optionally offer "get_module_spec" (although a better name
would be nice!)
2. Specs would have a load() method for the import system to call that
optionally accepted an existing module object (this would then cover
reload).
3. The responsibility for checking the sys.modules cache would move to the
import system.
4. We'd create a "SpecLoader" to offer backwards compatibility in the old
__loader__ attribute.
Slight(!) tangent from the original problem, but a worthwhile refactoring
issue to tackle, I think :)
Cheers,
Nick.
>
> -eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/import-sig/attachments/20130802/7cd7238b/attachment-0001.html>
More information about the Import-SIG
mailing list