Skip to content

Commit f99148d

Browse files
committed
gh-150162: Fix sysconfig cross-compile impermanence
Fixes issue #150162 by improving the code introduced by 7015485 (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.
1 parent d948eaa commit f99148d

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Lib/sysconfig/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ def joinuser(*args):
182182
_CONFIG_VARS = None
183183
# True iff _CONFIG_VARS has been fully initialized.
184184
_CONFIG_VARS_INITIALIZED = False
185+
_config_vars_cached_prefix = None
186+
_config_vars_cached_exec_prefix = None
185187
_USER_BASE = None
186188

187189

@@ -597,16 +599,20 @@ def get_config_vars(*args):
597599
each argument in the configuration variable dictionary.
598600
"""
599601
global _CONFIG_VARS_INITIALIZED
602+
global _config_vars_cached_prefix
603+
global _config_vars_cached_exec_prefix
600604

601605
# Avoid claiming the lock once initialization is complete.
606+
prefix = os.path.normpath(sys.prefix)
607+
exec_prefix = os.path.normpath(sys.exec_prefix)
602608
if _CONFIG_VARS_INITIALIZED:
603609
# GH-126789: If sys.prefix or sys.exec_prefix were updated, invalidate the cache.
604-
prefix = os.path.normpath(sys.prefix)
605-
exec_prefix = os.path.normpath(sys.exec_prefix)
606-
if _CONFIG_VARS['prefix'] != prefix or _CONFIG_VARS['exec_prefix'] != exec_prefix:
610+
if _config_vars_cached_prefix != prefix or _config_vars_cached_exec_prefix != exec_prefix:
607611
with _CONFIG_VARS_LOCK:
608612
_CONFIG_VARS_INITIALIZED = False
609613
_init_config_vars()
614+
_config_vars_cached_prefix = prefix
615+
_config_vars_cached_exec_prefix = exec_prefix
610616
else:
611617
# Initialize the config_vars cache.
612618
with _CONFIG_VARS_LOCK:
@@ -616,6 +622,8 @@ def get_config_vars(*args):
616622
# don't re-enter init_config_vars().
617623
if _CONFIG_VARS is None:
618624
_init_config_vars()
625+
_config_vars_cached_prefix = prefix
626+
_config_vars_cached_exec_prefix = exec_prefix
619627

620628
if args:
621629
vals = []

0 commit comments

Comments
 (0)