Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):

Expand Down
Loading