Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ def coding_spec(data):
return name


def libmodules():
libdir = os.path.dirname(os.path.dirname(__file__))
for entry in os.listdir(libdir):
if entry.endswith(".py"):
yield entry[:-3]
fullpath = os.path.join(libdir, entry)
if (os.path.isdir(fullpath) and
os.path.exists(os.path.join(fullpath, "__init__.py"))):
yield entry


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


class IOBinding:
# One instance per editor Window so methods know which to save, close.
# Open returns focus to self.editwin if aborted.
Expand Down Expand Up @@ -512,15 +527,42 @@ def defaultfilename(self, mode="open"):
pwd = ""
return pwd, ""

def warnbadfilename(self, msg):
return tkMessageBox.askokcancel(
title="Bad File Name", message=msg,
default=tkMessageBox.CANCEL, parent=self.text)

def asksavefile(self):
dir, base = self.defaultfilename("save")
if not self.savedialog:
self.savedialog = tkFileDialog.SaveAs(
parent=self.text,
filetypes=self.filetypes,
defaultextension=self.defaultextension)
filename = self.savedialog.show(initialdir=dir, initialfile=base)
return filename
go = False
while not go:
filename = self.savedialog.show(initialdir=dir, initialfile=base)
if not filename:
return ''
name = os.path.splitext(os.path.basename(filename))[0]
stdlibmsg = ("Neither you nor python will be able to import the "
"stdlib module.")
if islibmodule(name):
go = self.warnbadfilename(
f"This name matches a stdlib name in the Lib "
f"directory.\n{stdlibmsg}")
elif isbinmodule(name):
go = self.warnbadfilename(
f"This name matches a builtin stdlib name.\n"
f"{stdlibmsg}")
elif not name.isidentifier():
go = self.warnbadfilename(
"This name is not a valid identifier.\n"
"You will not be able to import this file.")
else:
go = True
if go:
return filename

def updaterecentfileslist(self,filename):
"Update recent file list on all editor windows"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
IDLE now warns when a user attempts to save a file that shadows a module in
the standard library.