◐ Shell
clean mode source ↗

Message 253775 - Python tracker

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