classification
Title: IDLE: warn if save-as name matches stdlib name
Type: enhancement Stage: needs patch
Components: IDLE Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: irdb, lac, markroseman, terry.reedy
Priority: normal Keywords:

Created on 2015-10-31 05:41 by terry.reedy, last changed 2017-06-19 19:07 by terry.reedy.

Messages (4)
msg253775 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-31 05:41
When users 'saveas', warn if name matches that of a stdlib modules. Note that shadowing is opposite for lib and binary (builtin) modules, so message needs to be different.  I am not worrying about 3rd party modules on the search path (ie, site-packages), which would be shadowed like lib modules. Rough outline of code to add to IOBinding.

from os,path import dirname
import sys

# this part on initiallzation or first saveas call
def libmodules():
    with open(dirname(dirname(__file__))) as lib:
        for f in lib:
            if <directory containing __init__.py#> or f.endswith(.py):
                yield f
# test.test__all__ has this code

islibmodule = frozenset(libmodules()).__contains__
isbinmodule = frozenset(sys.builtin_module_names).__contains__

# this part after saveas dialog returns
if islibmodule(name):
    go = warn("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?".)
elif isbinmodule(name):
    go = warn("This name matches a builtin stdlib name.  You will not be able to import this file.  Continue?".)
else:
    go = True
if go:
    <save as requested>
else:
   (get name again>  # or put in while not go loop
msg253777 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2015-10-31 05:56
The lib messages should say "Neither you nor python will be able to import the stdlib module".  Example of python-imported lib names that a beginner might write are abc, io, os, nt, and stat.
msg253829 - (view) Author: Laura Creighton (lac) Date: 2015-11-01 07:26
I'm not sure about "Neither you nor Python will be able to import 
the stdlib."  Depending on what the file is, you may be able to import
it later.  I'd prefer:

"If you run Python in this directory, your version of <stdlibname>
will be used instead of the one in the standard library.  This may cause Idle to behave strangely, or possibly not run at all."
msg253830 - (view) Author: Laura Creighton (lac) Date: 2015-11-01 07:36
Do we need a full path name here as well?  Probably not.
History
Date User Action Args
2017-06-19 19:07:50terry.reedysetassignee: terry.reedy
components: + IDLE
versions: + Python 3.7, - Python 2.7, Python 3.4, Python 3.5
2015-11-28 01:15:20irdbsetnosy: + irdb
2015-11-01 07:36:57lacsetmessages: + msg253830
2015-11-01 07:26:49lacsetmessages: + msg253829
2015-10-31 05:56:24terry.reedysetmessages: + msg253777
2015-10-31 05:41:42terry.reedycreate