diff options
author | Filipe Laíns 🇵🇸 <lains@riseup.net> | 2024-11-26 13:46:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 13:46:33 (GMT) |
commit | 2b0e2b2893a821ca36cd65a204bed932741ac189 (patch) | |
tree | dcae7576064fcb8ec409f38d98b217f5cc815249 /Lib | |
parent | ab237ff81d2201c70aaacf51f0c033df334e5d07 (diff) | |
download | cpython-2b0e2b2893a821ca36cd65a204bed932741ac189.zip cpython-2b0e2b2893a821ca36cd65a204bed932741ac189.tar.gz cpython-2b0e2b2893a821ca36cd65a204bed932741ac189.tar.bz2 |
GH-126985: move pyvenv.cfg detection from site to getpath (#126987)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/site.py | 11 | ||||
-rw-r--r-- | Lib/sysconfig/__init__.py | 18 | ||||
-rw-r--r-- | Lib/test/test_embed.py | 4 | ||||
-rw-r--r-- | Lib/test/test_getpath.py | 32 | ||||
-rw-r--r-- | Lib/test/test_sysconfig.py | 67 |
5 files changed, 33 insertions, 99 deletions
diff --git a/Lib/site.py b/Lib/site.py index abf4b52..92bd1cc 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -94,6 +94,12 @@ def _trace(message): print(message, file=sys.stderr) +def _warn(*args, **kwargs): + import warnings + + warnings.warn(*args, **kwargs) + + def makepath(*paths): dir = os.path.join(*paths) try: @@ -619,7 +625,10 @@ def venv(known_paths): elif key == 'home': sys._home = value - sys.prefix = sys.exec_prefix = site_prefix + if sys.prefix != site_prefix: + _warn(f'Unexpected value in sys.prefix, expected {site_prefix}, got {sys.prefix}', RuntimeWarning) + if sys.exec_prefix != site_prefix: + _warn(f'Unexpected value in sys.exec_prefix, expected {site_prefix}, got {sys.exec_prefix}', RuntimeWarning) # Doing this here ensures venv takes precedence over user-site addsitepackages(known_paths, [sys.prefix]) diff --git a/Lib/sysconfig/__init__.py b/Lib/sysconfig/__init__.py index 67a0719..ee52700 100644 --- a/Lib/sysconfig/__init__.py +++ b/Lib/sysconfig/__init__.py @@ -173,7 +173,9 @@ _SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include', _PY_VERSION = sys.version.split()[0] _PY_VERSION_SHORT = f'{sys.version_info[0]}.{sys.version_info[1]}' _PY_VERSION_SHORT_NO_DOT = f'{sys.version_info[0]}{sys.version_info[1]}' +_PREFIX = os.path.normpath(sys.prefix) _BASE_PREFIX = os.path.normpath(sys.base_prefix) +_EXEC_PREFIX = os.path.normpath(sys.exec_prefix) _BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix) # Mutex guarding initialization of _CONFIG_VARS. _CONFIG_VARS_LOCK = threading.RLock() @@ -465,10 +467,8 @@ def _init_config_vars(): # Normalized versions of prefix and exec_prefix are handy to have; # in fact, these are the standard versions used most places in the # Distutils. - _PREFIX = os.path.normpath(sys.prefix) - _EXEC_PREFIX = os.path.normpath(sys.exec_prefix) - _CONFIG_VARS['prefix'] = _PREFIX # FIXME: This gets overwriten by _init_posix. - _CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX # FIXME: This gets overwriten by _init_posix. + _CONFIG_VARS['prefix'] = _PREFIX + _CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX _CONFIG_VARS['py_version'] = _PY_VERSION _CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT _CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT @@ -541,7 +541,6 @@ def get_config_vars(*args): With arguments, return a list of values that result from looking up each argument in the configuration variable dictionary. """ - global _CONFIG_VARS_INITIALIZED # Avoid claiming the lock once initialization is complete. if not _CONFIG_VARS_INITIALIZED: @@ -552,15 +551,6 @@ def get_config_vars(*args): # don't re-enter init_config_vars(). if _CONFIG_VARS is None: _init_config_vars() - else: - # If the site module initialization happened after _CONFIG_VARS was - # initialized, a virtual environment might have been activated, resulting in - # variables like sys.prefix changing their value, so we need to re-init the - # config vars (see GH-126789). - if _CONFIG_VARS['base'] != os.path.normpath(sys.prefix): - with _CONFIG_VARS_LOCK: - _CONFIG_VARS_INITIALIZED = False - _init_config_vars() if args: vals = [] diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py index bf861ef..5c38b28 100644 --- a/Lib/test/test_embed.py +++ b/Lib/test/test_embed.py @@ -1649,14 +1649,14 @@ class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase): config = { 'base_prefix': sysconfig.get_config_var("prefix"), 'base_exec_prefix': exec_prefix, - 'exec_prefix': exec_prefix, + 'exec_prefix': tmpdir, + 'prefix': tmpdir, 'base_executable': base_executable, 'executable': executable, 'module_search_paths': paths, } if MS_WINDOWS: config['base_prefix'] = pyvenv_home - config['prefix'] = pyvenv_home config['stdlib_dir'] = os.path.join(pyvenv_home, 'Lib') config['use_frozen_modules'] = bool(not support.Py_DEBUG) else: diff --git a/Lib/test/test_getpath.py b/Lib/test/test_getpath.py index d5dcdad..7e5c4a3 100644 --- a/Lib/test/test_getpath.py +++ b/Lib/test/test_getpath.py @@ -92,8 +92,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable=r"C:\venv\Scripts\python.exe", - prefix=r"C:\Python", - exec_prefix=r"C:\Python", + prefix=r"C:\venv", + exec_prefix=r"C:\venv", base_executable=r"C:\Python\python.exe", base_prefix=r"C:\Python", base_exec_prefix=r"C:\Python", @@ -339,8 +339,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable="/venv/bin/python", - prefix="/usr", - exec_prefix="/usr", + prefix="/venv", + exec_prefix="/venv", base_executable="/usr/bin/python", base_prefix="/usr", base_exec_prefix="/usr", @@ -371,8 +371,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable="/venv/bin/python", - prefix="/usr", - exec_prefix="/usr", + prefix="/venv", + exec_prefix="/venv", base_executable="/usr/bin/python3", base_prefix="/usr", base_exec_prefix="/usr", @@ -404,8 +404,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable="/venv/bin/python", - prefix="/path/to/non-installed", - exec_prefix="/path/to/non-installed", + prefix="/venv", + exec_prefix="/venv", base_executable="/path/to/non-installed/bin/python", base_prefix="/path/to/non-installed", base_exec_prefix="/path/to/non-installed", @@ -435,8 +435,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable="/venv/bin/python", - prefix="/usr", - exec_prefix="/usr", + prefix="/venv", + exec_prefix="/venv", base_executable="/usr/bin/python9", base_prefix="/usr", base_exec_prefix="/usr", @@ -652,8 +652,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable=f"{venv_path}/bin/python", - prefix="/Library/Frameworks/Python.framework/Versions/9.8", - exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8", + prefix=venv_path, + exec_prefix=venv_path, base_executable="/Library/Frameworks/Python.framework/Versions/9.8/bin/python9.8", base_prefix="/Library/Frameworks/Python.framework/Versions/9.8", base_exec_prefix="/Library/Frameworks/Python.framework/Versions/9.8", @@ -697,8 +697,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable=f"{venv_path}/bin/python", - prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", - exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", + prefix=venv_path, + exec_prefix=venv_path, base_executable="/Library/Frameworks/DebugPython.framework/Versions/9.8/bin/python9.8", base_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", base_exec_prefix="/Library/Frameworks/DebugPython.framework/Versions/9.8", @@ -734,8 +734,8 @@ class MockGetPathTests(unittest.TestCase): ]) expected = dict( executable="/framework/Python9.8/python", - prefix="/usr", - exec_prefix="/usr", + prefix="/framework/Python9.8", + exec_prefix="/framework/Python9.8", base_executable="/usr/bin/python", base_prefix="/usr", base_exec_prefix="/usr", diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 9bbf8d0..a705dd0 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -110,6 +110,7 @@ class TestSysConfig(unittest.TestCase): **venv_create_args, ) + def test_get_path_names(self): self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS) @@ -592,71 +593,6 @@ class TestSysConfig(unittest.TestCase): self.assertTrue(suffix.endswith('-darwin.so'), suffix) @requires_subprocess() - def test_config_vars_depend_on_site_initialization(self): - script = textwrap.dedent(""" - import sysconfig - - config_vars = sysconfig.get_config_vars() - - import json - print(json.dumps(config_vars, indent=2)) - """) - - with self.venv() as venv: - site_config_vars = json.loads(venv.run('-c', script).stdout) - no_site_config_vars = json.loads(venv.run('-S', '-c', script).stdout) - - self.assertNotEqual(site_config_vars, no_site_config_vars) - # With the site initialization, the virtual environment should be enabled. - self.assertEqual(site_config_vars['base'], venv.prefix) - self.assertEqual(site_config_vars['platbase'], venv.prefix) - #self.assertEqual(site_config_vars['prefix'], venv.prefix) # # FIXME: prefix gets overwriten by _init_posix - # Without the site initialization, the virtual environment should be disabled. - self.assertEqual(no_site_config_vars['base'], site_config_vars['installed_base']) - self.assertEqual(no_site_config_vars['platbase'], site_config_vars['installed_platbase']) - - @requires_subprocess() - def test_config_vars_recalculation_after_site_initialization(self): - script = textwrap.dedent(""" - import sysconfig - - before = sysconfig.get_config_vars() - - import site - site.main() - - after = sysconfig.get_config_vars() - - import json - print(json.dumps({'before': before, 'after': after}, indent=2)) - """) - - with self.venv() as venv: - config_vars = json.loads(venv.run('-S', '-c', script).stdout) - - self.assertNotEqual(config_vars['before'], config_vars['after']) - self.assertEqual(config_vars['after']['base'], venv.prefix) - #self.assertEqual(config_vars['after']['prefix'], venv.prefix) # FIXME: prefix gets overwriten by _init_posix - #self.assertEqual(config_vars['after']['exec_prefix'], venv.prefix) # FIXME: exec_prefix gets overwriten by _init_posix - - @requires_subprocess() - def test_paths_depend_on_site_initialization(self): - script = textwrap.dedent(""" - import sysconfig - - paths = sysconfig.get_paths() - - import json - print(json.dumps(paths, indent=2)) - """) - - with self.venv() as venv: - site_paths = json.loads(venv.run('-c', script).stdout) - no_site_paths = json.loads(venv.run('-S', '-c', script).stdout) - - self.assertNotEqual(site_paths, no_site_paths) - - @requires_subprocess() def test_makefile_overwrites_config_vars(self): script = textwrap.dedent(""" import sys, sysconfig @@ -689,7 +625,6 @@ class TestSysConfig(unittest.TestCase): self.assertNotEqual(data['prefix'], data['base_prefix']) self.assertNotEqual(data['exec_prefix'], data['base_exec_prefix']) - class MakefileTests(unittest.TestCase): @unittest.skipIf(sys.platform.startswith('win'), |