diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index faa6002e630834..33d0975bda8eaa 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -467,40 +467,26 @@ def test_sitecustomize_executed(self): @support.requires_subprocess() def test_customization_modules_on_startup(self): - # Check that sitecustomize and or usercustomize are executed on startup - mod_info = [ - # func to get directory, file base name - ('getsitepackages', 'sitecustomize') + mod_names = [ + 'sitecustomize' ] if site.ENABLE_USER_SITE: - mod_info.append(('getusersitepackages', 'usercustomize'),) + mod_names.append('usercustomize') - for func_name, module_name in mod_info: - # getusersitepackages returns a string.. getsitepackages returns a list.. - # handle either way. - base_path = getattr(site, func_name)() - if not isinstance(base_path, str): - base_path = base_path[0] + temp_dir = tempfile.mkdtemp() + self.addCleanup(os_helper.rmtree, temp_dir) - try: - os.makedirs(base_path, exist_ok=True) - except PermissionError: - # Can't modify system site packages depending on the system configuration - continue + with EnvironmentVarGuard() as environ: + environ['PYTHONPATH'] = temp_dir - customize_path = os.path.join(base_path, f'{module_name}.py') - if os.path.exists(customize_path): - # backup old sitecustomize.py - oldcustomize_path = customize_path + '.old' - if os.path.exists(oldcustomize_path): - os.remove(oldcustomize_path) - os.rename(customize_path, oldcustomize_path) - self.addCleanup(os.rename, oldcustomize_path, customize_path) + for module_name in mod_names: + os_helper.rmtree(temp_dir) + os.mkdir(temp_dir) - eyecatcher = f'EXECUTED_{module_name}' + customize_path = os.path.join(temp_dir, f'{module_name}.py') + eyecatcher = f'EXECUTED_{module_name}' - try: with open(customize_path, 'w') as f: f.write(f'print("{eyecatcher}")') @@ -515,8 +501,7 @@ def test_customization_modules_on_startup(self): if 'usercustomize' == module_name: output = subprocess.check_output([sys.executable, '-s', '-c', '""']) self.assertNotIn(eyecatcher, output.decode('utf-8')) - finally: - os.remove(customize_path) + @unittest.skipUnless(hasattr(urllib.request, "HTTPSHandler"), 'need SSL support to download license')