|
14 | 14 | from test.support import captured_stderr |
15 | 15 | from test.support.os_helper import TESTFN, EnvironmentVarGuard |
16 | 16 | from test.support.script_helper import spawn_python, kill_python |
| 17 | +from test.support.venv import VirtualEnvironmentMixin |
17 | 18 | import ast |
18 | 19 | import builtins |
19 | 20 | import contextlib |
20 | 21 | import glob |
21 | 22 | import io |
| 23 | +import json |
22 | 24 | import os |
23 | 25 | import re |
24 | 26 | import shutil |
|
29 | 31 | import tempfile |
30 | 32 | from textwrap import dedent |
31 | 33 | from types import SimpleNamespace |
| 34 | +import textwrap |
32 | 35 | import urllib.error |
33 | 36 | import urllib.request |
34 | 37 | from unittest import mock |
@@ -485,7 +488,7 @@ def cleanup(self, prep=False): |
485 | 488 | os.rmdir(self.bad_dir_path) |
486 | 489 |
|
487 | 490 |
|
488 | | -class ImportSideEffectTests(unittest.TestCase): |
| 491 | +class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin): |
489 | 492 | """Test side-effects from importing 'site'.""" |
490 | 493 |
|
491 | 494 | def setUp(self): |
@@ -604,6 +607,31 @@ def test_lazy_imports(self): |
604 | 607 | "textwrap", |
605 | 608 | ]) |
606 | 609 |
|
| 610 | + @support.requires_subprocess() |
| 611 | + def test_system_site_packages(self): |
| 612 | + script = textwrap.dedent(""" |
| 613 | + import sys, json |
| 614 | +
|
| 615 | + print(json.dumps( |
| 616 | + sys.path, |
| 617 | + indent=2, |
| 618 | + )) |
| 619 | + """) |
| 620 | + |
| 621 | + # Use _get_preferred_schemes to find the system scheme, in case we are in a virtual environment |
| 622 | + system_scheme_name = sysconfig._get_preferred_schemes()['prefix'] |
| 623 | + system_paths = sysconfig.get_paths(system_scheme_name) |
| 624 | + |
| 625 | + with self.venv(system_site_packages=False) as venv: |
| 626 | + sys_path = json.loads(venv.run('-c', script).stdout) |
| 627 | + assert system_paths['purelib'] not in sys_path, sys_path |
| 628 | + assert system_paths['platlib'] not in sys_path, sys_path |
| 629 | + |
| 630 | + with self.venv(system_site_packages=True) as venv: |
| 631 | + sys_path = json.loads(venv.run('-c', script).stdout) |
| 632 | + assert system_paths['purelib'] in sys_path, sys_path |
| 633 | + assert system_paths['platlib'] in sys_path, sys_path |
| 634 | + |
607 | 635 |
|
608 | 636 | class StartupImportTests(unittest.TestCase): |
609 | 637 |
|
|
0 commit comments