From f8a6df0f9f1155b0036d21ce87bfd1dae8e44914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Thu, 30 Jan 2025 03:05:10 +0000 Subject: [PATCH 1/3] GH-128779: add test for venv --system-site-packages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe LaĆ­ns --- Lib/test/test_site.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index fc5e6369eacaada..c256db8d29f4c70 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -14,11 +14,13 @@ from test.support import captured_stderr from test.support.os_helper import TESTFN, EnvironmentVarGuard from test.support.script_helper import spawn_python, kill_python +from test.support.venv import VirtualEnvironmentMixin import ast import builtins import contextlib import glob import io +import json import os import re import shutil @@ -29,6 +31,7 @@ import tempfile from textwrap import dedent from types import SimpleNamespace +import textwrap import urllib.error import urllib.request from unittest import mock @@ -485,7 +488,7 @@ def cleanup(self, prep=False): os.rmdir(self.bad_dir_path) -class ImportSideEffectTests(unittest.TestCase): +class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin): """Test side-effects from importing 'site'.""" def setUp(self): @@ -604,6 +607,31 @@ def test_lazy_imports(self): "textwrap", ]) + @support.requires_subprocess() + def test_system_site_packages(self): + script = textwrap.dedent(""" + import sys, json + + print(json.dumps( + sys.path, + indent=2, + )) + """) + + # Use _get_preferred_schemes to find the system scheme, in case we are in a virtual environment + system_scheme_name = sysconfig._get_preferred_schemes()['prefix'] + system_paths = sysconfig.get_paths(system_scheme_name) + + with self.venv(system_site_packages=False) as venv: + sys_path = json.loads(venv.run('-c', script).stdout) + assert system_paths['purelib'] not in sys_path, sys_path + assert system_paths['platlib'] not in sys_path, sys_path + + with self.venv(system_site_packages=True) as venv: + sys_path = json.loads(venv.run('-c', script).stdout) + assert system_paths['purelib'] in sys_path, sys_path + assert system_paths['platlib'] in sys_path, sys_path + class StartupImportTests(unittest.TestCase): From cd29bdfcf4ec9931572be080d22ea588e4cf77c8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 30 Jan 2025 11:05:41 +0100 Subject: [PATCH 2/3] Update Lib/test/test_site.py --- Lib/test/test_site.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index c256db8d29f4c70..4b5149f1751b6ad 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -624,13 +624,13 @@ def test_system_site_packages(self): with self.venv(system_site_packages=False) as venv: sys_path = json.loads(venv.run('-c', script).stdout) - assert system_paths['purelib'] not in sys_path, sys_path - assert system_paths['platlib'] not in sys_path, sys_path + self.assertNotIn(system_paths['purelib'], sys_path) + self.assertNotIn(system_paths['platlib'], sys_path) with self.venv(system_site_packages=True) as venv: sys_path = json.loads(venv.run('-c', script).stdout) - assert system_paths['purelib'] in sys_path, sys_path - assert system_paths['platlib'] in sys_path, sys_path + self.assertIn(system_paths['purelib'], sys_path) + self.assertIn(system_paths['platlib'], sys_path) class StartupImportTests(unittest.TestCase): From 23f65650dba534724aeb2a5921ce5c82f5714cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Wed, 21 Oct 2026 03:37:09 +0200 Subject: [PATCH 3/3] Fix CI: skip system-site assertion on in-tree builds (gh-128779) site.py only adds site directories that exist on disk, so the system-site-packages assertion can only pass when the nominal install directories exist (e.g. with an installed Python). Skip that half on in-tree builds; the system_site_packages=False half is unaffected. Co-authored-by: Muse Code (Meta Muse Spark) --- Lib/test/test_site.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 4b5149f1751b6ad..69e5ec25ca247ef 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -627,6 +627,19 @@ def test_system_site_packages(self): self.assertNotIn(system_paths['purelib'], sys_path) self.assertNotIn(system_paths['platlib'], sys_path) + # site.py only adds site directories that exist on disk + # (addsitedir guards on os.path.isdir), so the system + # site-packages can only be detected when the directories exist, + # e.g. with an installed Python. Skip on in-tree builds where the + # nominal install directories were never created (gh-128779). + system_site_packages = {system_paths['purelib'], + system_paths['platlib']} + if not all(map(os.path.isdir, system_site_packages)): + self.skipTest( + f"system site-packages directories do not exist: " + f"{sorted(system_site_packages)}" + ) + with self.venv(system_site_packages=True) as venv: sys_path = json.loads(venv.run('-c', script).stdout) self.assertIn(system_paths['purelib'], sys_path)