This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Idle/tkinter ~x.py 'save as' fails. closes idle
Type: behavior Stage: patch review
Components: IDLE, Tkinter Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: loewis, roger.serwy, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2012-07-15 23:17 by terry.reedy, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
tilde_escape.patch roger.serwy, 2012-07-16 00:07 review
@tilde.patch terry.reedy, 2014-10-12 08:54 review
Messages (7)
msg165553 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-07-15 23:17
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>)"
msg165556 - (view) Author: Roger Serwy (roger.serwy) * (Python committer) Date: 2012-07-16 00:07
IDLE silently closing is due to issue13582.

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.
msg166379 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2012-07-25 09:54
I think this should apply to all systems, and I think the proper escaping is with "./", see TclpNativeSplitPath (in Tcl's source code).
msg227755 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-09-28 09:19
Agree with Martin. And I think we should check for starting tilde not base name, but full name.
msg229130 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-10-12 08:54
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.
msg229134 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-12 09:52
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.
msg229135 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-10-12 09:53
I meant "save as ~x.py", without the "./" prefix.
History
Date User Action Args
2022-04-11 14:57:32adminsetgithub: 59568
2019-09-19 18:50:18terry.reedysetversions: + Python 3.9, - Python 2.7, Python 3.4, Python 3.5
2014-10-12 09:53:41serhiy.storchakasetmessages: + msg229135
2014-10-12 09:52:54serhiy.storchakasetmessages: + msg229134
2014-10-12 08:54:18terry.reedysetfiles: + @tilde.patch

messages: + msg229130
stage: needs patch -> patch review
2014-09-28 09:19:41serhiy.storchakasetversions: + Python 3.5, - Python 3.3
nosy: + serhiy.storchaka

messages: + msg227755

stage: patch review -> needs patch
2013-09-15 16:03:50serhiy.storchakasetcomponents: + Tkinter
2013-06-15 19:06:19terry.reedysetversions: + Python 3.4, - Python 3.2
2012-07-25 09:54:50loewissetnosy: + loewis
messages: + msg166379
2012-07-20 16:58:11asvetlovsetstage: needs patch -> patch review
2012-07-16 00:07:47roger.serwysetfiles: + tilde_escape.patch
keywords: + patch
messages: + msg165556
2012-07-15 23:17:23terry.reedycreate