From: hetmankp <728670+hetmankp@users.noreply.github.com> Date: Mon, 22 Jun 2026 15:59:03 +0000 (+1000) Subject: gh-150162: Fix sysconfig cross-compile impermanence (#150164) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0bd1024f87a55a18340e06837e83713888f218a7;p=thirdparty%2FPython%2Fcpython.git gh-150162: Fix sysconfig cross-compile impermanence (#150164) Fixes issue #150162 by improving the code introduced by 70154855cf69 (GH-127729) while retaining the original documented intent. The aforementioned code has a side effect when used in a virtual environment context, on posix platforms, with the cross-compiling environment variable _PYTHON_PROJECT_BASE present. In this case, every single sysconfig.get_config_vars() and sysconfig.get_config_var() call, forces the _CONFIG_VARS dictionary to be reinitialised from scratch. This is inefficient, but also means no changes to the dictionary returned by sysconfig.get_config_vars() persist, which can be useful in certain situations. This commit tracks changes to sys.prefix and sys.exec_prefix more directly rather than relying on a misalignment with the corresponding sysconfig variables. --- diff --git a/Lib/sysconfig/__init__.py b/Lib/sysconfig/__init__.py index 719b306b02b6..fe55f48647e9 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -182,6 +182,8 @@ _CONFIG_VARS_LOCK = threading.RLock() _CONFIG_VARS = None # True iff _CONFIG_VARS has been fully initialized. _CONFIG_VARS_INITIALIZED = False +_config_vars_cached_prefix = None +_config_vars_cached_exec_prefix = None _USER_BASE = None @@ -600,16 +602,20 @@ def get_config_vars(*args): each argument in the configuration variable dictionary. """ global _CONFIG_VARS_INITIALIZED + global _config_vars_cached_prefix + global _config_vars_cached_exec_prefix # Avoid claiming the lock once initialization is complete. + prefix = os.path.normpath(sys.prefix) + exec_prefix = os.path.normpath(sys.exec_prefix) if _CONFIG_VARS_INITIALIZED: # GH-126789: If sys.prefix or sys.exec_prefix were updated, invalidate the cache. - prefix = os.path.normpath(sys.prefix) - exec_prefix = os.path.normpath(sys.exec_prefix) - if _CONFIG_VARS['prefix'] != prefix or _CONFIG_VARS['exec_prefix'] != exec_prefix: + if _config_vars_cached_prefix != prefix or _config_vars_cached_exec_prefix != exec_prefix: with _CONFIG_VARS_LOCK: _CONFIG_VARS_INITIALIZED = False _init_config_vars() + _config_vars_cached_prefix = prefix + _config_vars_cached_exec_prefix = exec_prefix else: # Initialize the config_vars cache. with _CONFIG_VARS_LOCK: @@ -619,6 +625,8 @@ def get_config_vars(*args): # don't re-enter init_config_vars(). if _CONFIG_VARS is None: _init_config_vars() + _config_vars_cached_prefix = prefix + _config_vars_cached_exec_prefix = exec_prefix if args: vals = []