From 61f7bfbf14cbb3f005d875de02a18eca30d63a8c 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/2] 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 035913cdd05f34..7ddd63fafddbf3 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -12,10 +12,12 @@ from test.support import socket_helper from test.support import captured_stderr from test.support.os_helper import TESTFN, EnvironmentVarGuard +from test.support.venv import VirtualEnvironmentMixin import ast import builtins import glob import io +import json import os import re import shutil @@ -24,6 +26,7 @@ import sys import sysconfig import tempfile +import textwrap import urllib.error import urllib.request from unittest import mock @@ -456,7 +459,7 @@ def cleanup(self, prep=False): if os.path.exists(self.bad_dir_path): os.rmdir(self.bad_dir_path) -class ImportSideEffectTests(unittest.TestCase): +class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin): """Test side-effects from importing 'site'.""" def setUp(self): @@ -576,6 +579,31 @@ def test_license_exists_at_url(self): code = e.code self.assertEqual(code, 200, msg="Can't find " + url) + @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 64165d60132cb459fd5d7858927bbfa3a7b0159a Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 30 Jan 2025 11:05:41 +0100 Subject: [PATCH 2/2] 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 7ddd63fafddbf3..811b6240eaa20b 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -596,13 +596,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):