[Python-ideas] Add 'module' module, similar to 'keyword' module

Steve Dower steve.dower at python.org
Sat Oct 31 14:31:52 EDT 2015


Could we add a context manager to importlib (or perhaps site or sys) to temporarily disable imports from non-standard paths? I don't see any safe way to change the default behavior, but no reason we can't make it easy for applications to self-isolate.

Top-posted from my Windows Phone

-----Original Message-----
From: "Brett Cannon" <brett at python.org>
Sent: ‎10/‎31/‎2015 9:50
To: "Terry Reedy" <tjreedy at udel.edu>; "python-ideas at python.org" <python-ideas at python.org>
Subject: Re: [Python-ideas] Add 'module' module, similar to 'keyword' module




On Fri, 30 Oct 2015 at 22:57 Terry Reedy <tjreedy at udel.edu> wrote:

On 10/30/2015 9:19 PM, Guido van Rossum wrote:
> There's sys.builtin_module_names which returns the names of the
> hardcoded builtin modules.

Great.  With this solved, I opened an issue for IDLE.
https://bugs.python.org/issue25522

> Dynamically loaded modules can be found by
> searching sys.path in the usual way -- importlib shoul know. I wonder if
> just asking importlib whether it can locate a given module would be enough?

The default search order is stdlib builtins, local user files, /lib
files, so the shadowing issue the opposite for builtin and /lib modules.
  Hence a different message is needed.



Quick and dirty way to use importlib is to get the location of the stdlib (os.__file__ should work since it's hard-coded in the interpreter as representing where the stdlib is) and then use importlib.find_spec() for a module name to check if the file location in the spec has the same location prefix as os or not (make sure you use absolute paths since it isn't guaranteed if you don't execute site.py).


I've now seen this use case, the logging one, and the 2to3 module rename. I'm starting to wonder if there some general solution that should get added to the import machinery that  can serve these cases more easily than with importers or __import__ overrides that can be tricky to get right.


-Brett
 

> On Fri, Oct 30, 2015 at 6:09 PM, Terry Reedy
> <tjreedy at udel.edu
> <mailto:tjreedy at udel.edu>> wrote:
>
>     This idea results from issue of user files shadowing stdlib files on
>     import.  There was a thread on pydev about this yesterday.  There is
>     also an opposite issue of builtin modules shadowing user files.
>
>     The keyword module provides kwlist and iskeyword function.  One use
>     of kwlist is used in some other stdlib modules and can be used by
>     syntax highlighters (as in IDLE).  Kwlist is updated by the main
>     function.
>
>     A module module would have at least liblist and islibmodule
>     function. Liblist would contain all directories with __init__.py and
>     all .py files.  (I don't think files within package directories
>     should be included, as there is no direct shadowing problem.)  A
>     python oriented editor could then warn on save requests "This name
>     matches a stdlib name in /Lib. If you run python in this directory,
>     you will not be able to import the stdlib module.  Continue?".
>
>     The module should also have binlist and isbinmodule for builtin
>     modules.  (I do not know how to get such a list.  If necessary, an
>     api could be added.)  An editor could than warn "This name matches a
>     builtin stdlib name.  You will not be able to import this file.
>     Continue?".
>
>     --
>     Terry Jan Reedy
>
>     _______________________________________________
>     Python-ideas mailing list
>     Python-ideas at python.org
>     <mailto:Python-ideas at python.org>
>     https://mail.python.org/mailman/listinfo/python-ideas
>     Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
>
> --
> --Guido van Rossum (python.org/~guido <http://python.org/~guido>)
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


--
Terry Jan Reedy

_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151031/45ec6317/attachment-0001.html>


More information about the Python-ideas mailing list