Skip to content

Commit 601d2d9

Browse files
[3.14] gh-59568: Fix IDLE file dialogs for names starting with a tilde (GH-157624) (#158055)
gh-59568: Fix IDLE file dialogs for names starting with a tilde (GH-157624) Tcl before version 9, not on macOS Aqua systems, substitutes a leading tilde in a file name. "Save As" can fail on Windows and X11 and may crash IDLE on Windows. (cherry picked from commit fc9df96) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 74ded2a commit 601d2d9

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎Lib/idlelib/idle_test/test_iomenu.py‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"Test , coverage 17%."
22

33
from idlelib import iomenu
4+
import os
45
import unittest
6+
from unittest import mock
57
from test.support import requires
6-
from tkinter import Tk
8+
from tkinter import Tk, TclVersion
79
from idlelib.editor import EditorWindow
8-
from idlelib import util
10+
from idlelib import macosx, util
911
from idlelib.idle_test.mock_idle import Func
1012

1113
# Fail if either tokenize.open and t.detect_encoding does not exist.
@@ -38,6 +40,17 @@ def tearDownClass(cls):
3840
def test_init(self):
3941
self.assertIs(self.io.editwin, self.editwin)
4042

43+
def test_defaultfilename_tilde(self):
44+
# gh-59568: escape a leading tilde for Tcl older than 9.
45+
io = self.io
46+
with mock.patch.object(io, 'filename', os.path.join('dir', '~file.py')):
47+
dirname, base = io.defaultfilename()
48+
self.assertEqual(dirname, 'dir')
49+
if TclVersion < 9 and not macosx.isAquaTk():
50+
self.assertEqual(base, os.path.join(os.curdir, '~file.py'))
51+
else:
52+
self.assertEqual(base, '~file.py')
53+
4154
def test_fixnewlines_end(self):
4255
eq = self.assertEqual
4356
io = self.io

‎Lib/idlelib/iomenu.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import tempfile
66
import tokenize
77

8+
from tkinter import TclVersion
89
from tkinter import filedialog
910
from tkinter import messagebox
1011
from tkinter.simpledialog import askstring # loadfile encoding.
1112

1213
from idlelib.config import idleConf
14+
from idlelib import macosx
1315
from idlelib.util import py_extensions
1416

1517
py_extensions = ' '.join("*"+ext for ext in py_extensions)
@@ -405,7 +407,12 @@ def askopenfile(self):
405407

406408
def defaultfilename(self, mode="open"):
407409
if self.filename:
408-
return os.path.split(self.filename)
410+
dirname, base = os.path.split(self.filename)
411+
if base[:1] == '~' and TclVersion < 9 and not macosx.isAquaTk():
412+
# Tcl before version 9 substitutes a leading tilde in
413+
# a file name (gh-59568). The macOS dialog does not.
414+
base = os.path.join(os.curdir, base)
415+
return dirname, base
409416
elif self.dirname:
410417
return self.dirname, ""
411418
else:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix "Save As" and "Open" dialogs in IDLE for a file whose name starts with
2+
``~`` on Windows and X11 with Tcl/Tk older than 9.

0 commit comments

Comments
 (0)