8000 bpo-38183: Test_idle ignores user config directory GH-16198) · python/cpython@0048afc · GitHub
[go: up one dir, main page]

Skip to content

Commit 0048afc

Browse files
authored
bpo-38183: Test_idle ignores user config directory GH-16198)
It no longer tries to create or access .idlerc or any files within. Users must run IDLE to discover problems with saving settings.
1 parent 81528ba commit 0048afc

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

Lib/idlelib/config.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def Save(self):
130130
to disk. Otherwise, remove the file from disk if it exists.
131131
"""
132132
fname = self.file
133-
if fname:
133+
if fname and fname[0] != '#':
134134
if not self.IsEmpty():
135135
try:
136136
cfgFile = open(fname, 'w')
@@ -166,12 +166,12 @@ def __init__(self, _utest=False):
166166
def CreateConfigHandlers(self):
167167
"Populate default and user config parser dictionaries."
168168
idledir = os.path.dirname(__file__)
169-
self.userdir = userdir = self.GetUserCfgDir()
169+
self.userdir = userdir = '' if idlelib.testing else self.GetUserCfgDir()
170170
for cfg_type in self.config_types:
171171
self.defaultCfg[cfg_type] = IdleConfParser(
172172
os.path.join(idledir, f'config-{cfg_type}.def'))
173173
self.userCfg[cfg_type] = IdleUserConfParser(
174-
os.path.join(userdir, f'config-{cfg_type}.cfg'))
174+
os.path.join(userdir or '#', f'config-{cfg_type}.cfg'))
175175

176176
def GetUserCfgDir(self):
177177
"""Return a filesystem directory for storing user config files.
@@ -182,12 +182,13 @@ def GetUserCfgDir(self):
182182
userDir = os.path.expanduser('~')
183183
if userDir != '~': # expanduser() found user home dir
184184
if not os.path.exists(userDir):
185-
warn = ('\n Warning: os.path.expanduser("~") points to\n ' +
186-
userDir + ',\n but the path does not exist.')
187-
try:
188-
print(warn, file=sys.stderr)
189-
except OSError:
190-
pass
185+
if not idlelib.testing:
186+
warn = ('\n Warning: os.path.expanduser("~") points to\n ' +
187+
userDir + ',\n but the path does not exist.')
188+
try:
189+
print(warn, file=sys.stderr)
190+
except OSError:
191+
pass
191192
userDir = '~'
192193
if userDir == "~": # still no path to home!
193194
# traditionally IDLE has defaulted to os.getcwd(), is this adequate?
@@ -197,10 +198,13 @@ def GetUserCfgDir(self):
197198
try:
198199
os.mkdir(userDir)
199200
except OSError:
200-
warn = ('\n Warning: unable to create user config directory\n' +
201-
userDir + '\n Check path and permissions.\n Exiting!\n')
202201
if not idlelib.testing:
203-
print(warn, file=sys.stderr)
202+
warn = ('\n Warning: unable to create user config directory\n' +
203+
userDir + '\n Check path and permissions.\n Exiting!\n')
204+
try:
205+
print(warn, file=sys.stderr)
206+
except OSError:
207+
pass
204208
raise SystemExit
205209
# TODO continue without userDIr instead of exit
206210
return userDir

Lib/idlelib/editor.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __init__(self, flist=None, filename=None, key=None, root=None):
116116
self.tkinter_vars = {} # keys: Tkinter event names
117117
# values: Tkinter variable instances
118118
self.top.instance_dict = {}
119-
self.recent_files_path = os.path.join(
119+
self.recent_files_path = idleConf.userdir and os.path.join(
120120
idleConf.userdir, 'recent-files.lst')
121121

122122
self.prompt_last_line = '' # Override in PyShell
@@ -924,9 +924,11 @@ def display_extra_help(helpfile=helpfile):
924924

925925
def update_recent_files_list(self, new_file=None):
926926
"Load and update the recent files list and menus"
927+
# TODO: move to iomenu.
927928
rf_list = []
928-
if os.path.exists(self.recent_files_path):
929-
with open(self.recent_files_path, 'r',
929+
file_path = self.recent_files_path
930+
if file_path and os.path.exists(file_path):
931+
with open(file_path, 'r',
930932
encoding='utf_8', errors='replace') as rf_list_file:
931933
rf_list = rf_list_file.readlines()
932934
if new_file:
@@ -942,19 +944,19 @@ def update_recent_files_list(self, new_file=None):
942944
rf_list = [path for path in rf_list if path not in bad_paths]
943945
ulchars = "1234567890ABCDEFGHIJK"
944946
rf_list = rf_list[0:len(ulchars)]
945-
try:
946-
with open(self.recent_files_path, 'w',
947-
encoding='utf_8', errors='replace') as rf_file:
948-
rf_file.writelines(rf_list)
949-
except OSError as err:
950-
if not getattr(self.root, "recentfilelist_error_displayed", False):
951-
self.root.recentfilelist_error_displayed = True
952-
tkMessageBox.showwarning(title='IDLE Warning',
953-
message="Cannot update File menu Recent Files list. "
954-
"Your operating system says:\n%s\n"
955-
"Select OK and IDLE will continue without updating."
956-
% self._filename_to_unicode(str(err)),
957-
parent=self.text)
947+
if file_path:
948+
try:
949+
with open(file_path, 'w',
950+
encoding='utf_8', errors='replace') as rf_file:
951+
rf_file.writelines(rf_list)
952+
except OSError as err:
953+
if not getattr(self.root, "recentfiles_message", False):
954+
self.root.recentfiles_message = True
955+
tkMessageBox.showwarning(title='IDLE Warning',
956+
message="Cannot save Recent Files list to disk.\n"
957+
f" {err}\n"
958+
"Select OK to continue.",
959+
parent=self.text)
958960
# for each edit window instance, construct the recent files menu
959961
for instance in self.top.instance_dict:
960962
menu = instance.recent_files_menu

Lib/idlelib/idle_test/test_config.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def mock_config(self):
220220

221221
@unittest.skipIf(sys.platform.startswith('win'), 'this is test for unix system')
222222
def test_get_user_cfg_dir_unix(self):
223-
"Test to get user config directory under unix"
223+
# Test to get user config directory under unix.
224224
conf = self.new_config(_utest=True)
225225

226226
# Check normal way should success
@@ -243,7 +243,7 @@ def test_get_user_cfg_dir_unix(self):
243243

244244
@unittest.skipIf(not sys.platform.startswith('win'), 'this is test for Windows system')
245245
def test_get_user_cfg_dir_windows(self):
246-
"Test to get user config directory under Windows"
246+
# Test to get user config directory under Windows.
247247
conf = self.new_config(_utest=True)
248248

249249
# Check normal way should success
@@ -284,12 +284,12 @@ def test_create_config_handlers(self):
284284
self.assertIsInstance(user_parser, config.IdleUserConfParser)
285285

286286
# Check config path are correct
287-
for config_type, parser in conf.defaultCfg.items():
287+
for cfg_type, parser in conf.defaultCfg.items():
288288
self.assertEqual(parser.file,
289-
os.path.join(idle_dir, 'config-%s.def' % config_type))
290-
for config_type, parser in conf.userCfg.items():
289+
os.path.join(idle_dir, f'config-{cfg_type}.def'))
290+
for cfg_type, parser in conf.userCfg.items():
291291
self.assertEqual(parser.file,
292-
os.path.join(conf.userdir, 'config-%s.cfg' % config_type))
292+
os.path.join(conf.userdir or '#', f'config-{cfg_type}.cfg'))
293293

294294
def test_load_cfg_files(self):
295295
conf = self.new_config(_utest=True)
@@ -373,7 +373,7 @@ def test_get_highlight(self):
373373
'background': '#171717'})
374374

375375
def test_get_theme_dict(self):
376-
"XXX: NOT YET DONE"
376+
# TODO: finish.
377377
conf = self.mock_config()
378378

379379
# These two should be the same

Lib/idlelib/pyshell.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def __init__(self, *args):
133133
self.text.bind("<<clear-breakpoint-here>>", self.clear_breakpoint_here)
134134
self.text.bind("<<open-python-shell>>", self.flist.open_shell)
135135

136+
#TODO: don't read/write this from/to .idlerc when testing
136137
self.breakpointPath = os.path.join(
137138
idleConf.userdir, 'breakpoints.lst')
138139
# whenever a file is changed, restore breakpoints
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To avoid problems, test_idle ignores the user config directory.
2+
It no longer tries to create or access .idlerc or any files within.
3+
Users must run IDLE to discover problems with saving settings.

0 commit comments

Comments
 (0)
0