I have (had ;-) a project file ~template.py with common boilerplate.
To start a new project file, I want to open the above and save as xyz.py. I can edit and 'save' the template to update it just fine. But trying to do a 'save as' to a new name, in Idle editor, on all three current versions, I get (when starting Idle from command window)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Programs\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Programs\Python33\lib\idlelib\IOBinding.py", line 347, in save_as
filename = self.asksavefile()
File "C:\Programs\Python33\lib\idlelib\IOBinding.py", line 514, in asksavefile
filename = self.savedialog.show(initialdir=dir, initialfile=base)
File "C:\Programs\Python33\lib\tkinter\commondialog.py", line 48, in show
s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: user "template.py" doesn't exist
When starting Idle normally, this causes it to silently close. When run from the cp interpreter, the edit window blinks but continues while the trackback is printed in the cp window.
Just saving does not bring up a dialog; hence no problem. And I understand that '~' has special meaning on *nix, but I am running on Windows.
I changed '~' to '@' and the file still sorts at the top (though above __init__.py rather than below) and save as now works as it should. I know that save as worked for this file last October, but while I think it had the same ~name then, I cannot be sure. Maybe I added '~' after the last time I successfully used the file.
As near as I can tell, asksavefile() passes initialfile = base = '~template.py' to savedialog.show which passes it unchanged and unexamined to tk.call. So the special casing of ~ seems to happen in tcl/tk out of our control. I am not sure if doing this on Windows is a tcl/tk bug or not.
For Windows, just removing '~' or even blanking the name before the .show() might be sensible, but maybe not for *nix.
Instead, we could catch the error instead of letting it crash Idle and put up a message box to the effect that one cannot 'save as' when editing a file named '~xxxx'. More specifically, in IOBinding.py, line 514 or so, replace
filename = self.savedialog.show(initialdir=dir, initialfile=base)
with
try:
filename = self.savedialog.show(initialdir=dir, initialfile=base)
except TclError as e:
if e.args[0].startswith('user'):
xxx display message, click ok
filename = None
I am assuming the None is the return from canceled dialogs. Certainly, None caused the file save code back up in saveas() (346) to skip the save.
The message could be something like "tcl/tk gives special treatment to file names starting with '~' and returned this error message {}".format(e.args[0])
We could also add "else: message('unanticipated tclerror prevented save as dialog <message>)"
|