Open terryjreedy opened 12 years ago
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>)"
IDLE silently closing is due to bpo-13582.
On Linux (Ubuntu 11.04, Tk8.5) I can not specify "\~template.py" for a filename for opening or saving a file. Clicking on either open or save produces no action. However, I can specify "\~/template.py".
On Win7 I see there error Terry describes. This is due to "Tilde Substitution" described here: http://www.tcl.tk/man/tcl/TclCmd/filename.htm#M20
Attached is a patch against 3.3 that escapes the initial tilde on Windows. The patch should be identical against 3.2 and 2.7.
I think this should apply to all systems, and I think the proper escaping is with "./", see TclpNativeSplitPath (in Tcl's source code).
Agree with Martin. And I think we should check for starting tilde not base name, but full name.
According to the tcl/tk manual, tcl does ~ expansion on non-'old' Windows. However, I cannot tell in what context that could be useful. Trying to save to '\~/x.py' fails. "The filename is not valid." This might be because there is no HOME since Windows sometimes uses USERPROFILE instead. But it seems that any name with a path separator is invalid. At least on Windows, one must specify the directory my moving to it in the directory part of the dialog.
If dirname (passed to tk_getSaveFile as initialdir) is not "" after the split of self.filename (and I believe it never is), it seems to me possibly a bug that tk_getSaveFile looks for a leading ~ for base (passed as initialfile). But we cannot affect that. Serhiy, your suggesting to check self.filename (full name) instead of base would be correct if tk did not check base after it is split off. But it does, buggy or not. The full name of the project file that failed was something like F:/python/~templete.py.
Martin and Serhiy. If I understand correctly, you are both suggesting that '\~x.py' should become './~x.py' instead of '\~x.py'. This seems to work equally well. It seems that after ~ processing, the basename gets changed back to and displayed as '\~x.py' and saving as such works.
You also suggest that at least your version of the escaping should not be limited to Windows. It seems like this might help, and should not hurt, but I would like the latter tested on Linux before I commit. Attached is my patch with both changes.
Interesting, that "save as" ./~x.py works when the "\~x.py" file exists and doesn't work (press the "Save" button has no any effect at all, even doesn't close a dialog) when it does not exist on Linux.
I meant "save as \~x.py", without the "./" prefix.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at = None created_at =
labels = ['expert-IDLE', 'type-bug', 'expert-tkinter', '3.9']
title = "Idle/tkinter ~x.py 'save as' fails. closes idle"
updated_at =
user = 'https://github.com/terryjreedy'
```
bugs.python.org fields:
```python
activity =
actor = 'terry.reedy'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['IDLE', 'Tkinter']
creation =
creator = 'terry.reedy'
dependencies = []
files = ['26390', '36887']
hgrepos = []
issue_num = 15363
keywords = ['patch']
message_count = 7.0
messages = ['165553', '165556', '166379', '227755', '229130', '229134', '229135']
nosy_count = 4.0
nosy_names = ['loewis', 'terry.reedy', 'roger.serwy', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue15363'
versions = ['Python 3.9']
```