From 75b3561a8d2b0fc6e4fa4fdf9036aef62499d20c Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 13 Aug 2026 18:17:12 +0300 Subject: [PATCH] gh-155726: Restore the sysconfig prefix cache in test_sysconfig test_sysconfig_config_vars_no_prefix_cache changes sys.prefix and calls get_config_vars(), which caches the new prefix in module globals. tearDown() restored sysconfig._CONFIG_VARS, but not those globals, so the next get_config_vars() call found the cache stale and replaced _CONFIG_VARS with a new dict. --- Lib/test/test_sysconfig.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 9bb3e326ff3e95..b40d1a23e13df9 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -64,6 +64,8 @@ def setUp(self): self.isabs = os.path.isabs self.splitdrive = os.path.splitdrive self._config_vars = sysconfig._CONFIG_VARS, copy(sysconfig._CONFIG_VARS) + self._cached_prefixes = (sysconfig._config_vars_cached_prefix, + sysconfig._config_vars_cached_exec_prefix) self._added_envvars = [] self._changed_envvars = [] for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'): @@ -92,6 +94,11 @@ def tearDown(self): sysconfig._CONFIG_VARS = self._config_vars[0] sysconfig._CONFIG_VARS.clear() sysconfig._CONFIG_VARS.update(self._config_vars[1]) + # Restoring _CONFIG_VARS is not enough: if the cached prefixes are + # left over from the test, the next get_config_vars() call finds + # the cache stale and replaces _CONFIG_VARS with a new dict. + (sysconfig._config_vars_cached_prefix, + sysconfig._config_vars_cached_exec_prefix) = self._cached_prefixes for var, value in self._changed_envvars: os.environ[var] = value for var in self._added_envvars: