https://github.com/python/cpython/commit/3d5de99be1113a4d241ce640c6222b3884792643
commit: 3d5de99be1113a4d241ce640c6222b3884792643
branch: main
author: Filipe LaĆ­ns <[email protected]>
committer: FFY00 <[email protected]>
date: 2026-10-21T20:56:38Z
summary:

GH-128779: add test for venv --system-site-packages (#129462)

files:
M Lib/test/test_site.py

diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index fc5e6369eacaad..69e5ec25ca247e 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,44 @@ 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)
+        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)
+        self.assertIn(system_paths['platlib'], sys_path)
+
 
 class StartupImportTests(unittest.TestCase):
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to