[Python-Dev] making the import machinery explicit

Brett Cannon brett at python.org
Sun Apr 15 18:31:46 CEST 2012

On Sun, Apr 15, 2012 at 07:26, Nick Coghlan <ncoghlan at gmail.com> wrote:

> Hooray for finally having this to the point where it has been pushed to
> trunk :)
> On Sun, Apr 15, 2012 at 8:16 AM, Brett Cannon <brett at python.org> wrote:
> > 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.
> I'm not sure you can so blithely wave away the "check this before the
> standard hooks" problem. If the recommended approach becomes to insert
> new hooks at the *start* of path_hooks and meta_path, then that should
> work fairly well, since the new additions will take precedence
> regardless of what other changes have already been made. However,
> trying to be clever and say "before the standard hooks, but after
> everything else" is fraught with peril, since there may be hooks
> present in the lists *after* the standard ones so naive counting
> wouldn't work.

Well, I personally say always insert to the front of sys.meta_path because
getting precedence right is always difficult when you are trying to insert
into the middle of a list. I mean this issue could happen in any
application that have multiple meta path finders and their is an explicit
order that is desired that does not simply fall through from the way code
is called.

> As far as the guidelines for managing the import state go, it may be
> worth having public "importlib.default_path_hooks" and
> "importlib.default_meta_path" attributes.

I don't think it is. ``importlib.default_meta_path =
[importlib.machinery.PathFinder]`` and ``importlib.default_path_hooks =
zipimport.whatever_its_called]`` is not exactly complicated and if people
are not reading the docs closely enough to realize that is what the
defaults are they are already asking for trouble when mucking around with

> Then "clearing" the hooks would just be a matter of resetting them
> back to defaults: "sys.path_hooks[:] = importlib.default_path_hooks".
> You could also locate them in the hooks list correctly by checking
> "for i, hook in enumerate(sys.path_hooks): if hook is
> importlib.default_path_hooks[0]: break"

You do realize people will forget the [:] and end up simply screwing up
that original list, right? =)

> Alternatively, it may be simpler to just expose a less granular
> "reset_import_hooks()" function that restores meta_path and path_hooks
> back to their default state (the defaults could then be private
> attributes rather than public ones) and invalidates all the caches.

What about sys.path_importer_cache: all of it or just NullImporter/None
entries (or should that be a boolean to this function)? And shouldn't it be
called reset_import() with the level of changes you are proposing the
function make?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120415/b325759c/attachment.html>

More information about the Python-Dev mailing list