<div class="HOEnZb"><div class="h5">On Sat, Apr 14, 2012 at 2:03 PM, Brett Cannon <<a href="mailto:brett@python.org">brett@python.org</a>> wrote:<br>
> To start off, what I am about to propose was brought up at the PyCon<br>
> language summit and the whole room agreed with what I want to do here, so I<br>
> honestly don't expect much of an argument (famous last words).<br>
><br>
> In the "ancient" import.c days, a lot of import's stuff was hidden deep in<br>
> the C code and in no way exposed to the user. But with importlib finishing<br>
> PEP 302's phase 2 plans of getting imoprt to be properly refactored to use<br>
> importers, path hooks, etc., this need no longer be the case.<br>
><br>
> So what I propose to do is stop having import have any kind of implicit<br>
> machinery. This means sys.meta_path gets a path finder that does the heavy<br>
> lifting for import and sys.path_hooks gets a hook which provides a default<br>
> finder. As of right now those two pieces of machinery are entirely implicit<br>
> in importlib and can't be modified, stopped, etc.<br>
><br>
> If this happens, what changes? First, more of importlib will get publicly<br>
> exposed (e.g. the meta path finder would become public instead of private<br>
> like it is along with everything else that is publicly exposed). Second,<br>
> import itself technically becomes much simpler since it really then is about<br>
> resolving module names, traversing sys.meta_path, and then handling fromlist<br>
> w/ everything else coming from how the path finder and path hook work.<br>
><br>
> What also changes is that sys.meta_path and sys.path_hooks cannot be blindly<br>
> reset w/o blowing out import. I doubt anyone is even touching those<br>
> attributes in the common case, and the few that do can easily just stop<br>
> wiping out those two lists. If people really care we can do a warning in 3.3<br>
> if they are found to be empty and then fall back to old semantics, but I<br>
> honestly don't see this being an issue as backwards-compatibility would just<br>
> require being more careful of what you delete (which I have been warning<br>
> people to do for years now) which is a minor code change which falls in line<br>
> with what goes along with any new Python version.<br>
><br>
> And lastly, sticking None in sys.path_importer_cache would no longer mean<br>
> "do the implicit thing" and instead would mean the same as NullImporter does<br>
> now (which also means import can put None into sys.path_importer_cache<br>
> instead of NullImporter): no finder is available for an entry on sys.path<br>
> when None is found. Once again, I don't see anyone explicitly sticking None<br>
> into sys.path_importer_cache, and if they are they can easily stick what<br>
> will be the newly exposed finder in there instead. The more common case<br>
> would be people wiping out all entries of NullImporter so as to have a new<br>
> sys.path_hook entry take effect. That code would instead need to clear out<br>
> None on top of NullImporter as well (in Python 3.2 and earlier this would<br>
> just be a performance loss, not a semantic change). So this too could change<br>
> in Python 3.3 as long as people update their code like they do with any<br>
> other new version of Python.<br>
><br>
> In summary, I want no more magic "behind the curtain" for Python 3.3 and<br>
> import: sys.meta_path and sys.path_hooks contain what they should and if<br>
> they are emptied then imports will fail and None in sys.path_importer_cache<br>
> means "no finder" instead of "use magical, implicit stuff".<br>
<br>
</div></div>This is great, Brett. About sys.meta_path and sys.path_hooks, I see<br>
only one potential backwards-compatibility problem.<br>
<br>
Those implicit hooks were fallbacks, effectively always at the end of<br>
the list, no matter how you manipulated the them. Code that appended<br>
onto those lists would now have to insert the importers/finders in the<br>
right way. Otherwise the default hooks would be tried first, which<br>
has a good chance of being the wrong thing.<br>
<br>
That concern aside, I'm a big +1 on your proposal.</blockquote><div><br></div><div>Once again, it's just code that needs updating to run on Python 3.3 so I don't view it as a concern. Going from list.append() to list.insert() (even if its ``list.insert(hook, len(list)-2)``) is not exactly difficult.</div>