You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IDLE: warn if save-as name matches stdlib name #69708
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 calldeflibmodules():
withopen(dirname(dirname(__file__))) aslib:
forfinlib:
if<directorycontaining__init__.py#> or f.endswith(.py):yieldf# test.test__all__ has this codeislibmodule=frozenset(libmodules()).__contains__isbinmodule=frozenset(sys.builtin_module_names).__contains__# this part after saveas dialog returnsifislibmodule(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?".)
elifisbinmodule(name):
go=warn("This name matches a builtin stdlib name. You will not be able to import this file. Continue?".)
else:
go=Trueifgo:
<saveasrequested>else:
(getnameagain># or put in while not go loop
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.
lac commented on Nov 1, 2015
lacmannequin
Mannequin
More actions
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."
lac commented on Nov 1, 2015
lacmannequin
Mannequin
More actions
Do we need a full path name here as well? Probably not.
Many beginners don't write files in order to import them. For them, these name-checking is simply adding noise. If you want to do something in this area, I think a much more useful (and difficult) course of action would be to check on the opposite end. If an AttributeError "module blah has no attribute foo" is raised, then check whether a file in the current directory (or simply the current file) is named blah, and there is also a stdlib blah that has fool.
Beginners import stdlib files such as random. And they save scripts with such name, and accidentally import the script when not desired.
Beginners should learn how to test a script by running a test file provided by an instructor or written themselves. In either case, they *will* have to import the script.
Activity
terryjreedy commentedon Oct 31, 2015
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
terryjreedy commentedon Oct 31, 2015
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.
lac commentedon Nov 1, 2015
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."
lac commentedon Nov 1, 2015
Do we need a full path name here as well? Probably not.
terryjreedy commentedon Jan 18, 2019
Also check if the name is not an identifier and therefore could not be imported. Example:
vedgar commentedon Jan 18, 2019
Many beginners don't write files in order to import them. For them, these name-checking is simply adding noise. If you want to do something in this area, I think a much more useful (and difficult) course of action would be to check on the opposite end. If an AttributeError "module blah has no attribute foo" is raised, then check whether a file in the current directory (or simply the current file) is named blah, and there is also a stdlib blah that has fool.
terryjreedy commentedon Jan 24, 2019
Beginners import stdlib files such as random. And they save scripts with such name, and accidentally import the script when not desired.
Beginners should learn how to test a script by running a test file provided by an instructor or written themselves. In either case, they *will* have to import the script.
ZackerySpytz commentedon Nov 5, 2019
I have created a pull request for this issue.