|
12 | 12 | from test.support import socket_helper
|
13 | 13 | from test.support import captured_stderr
|
14 | 14 | from test.support.os_helper import TESTFN, EnvironmentVarGuard
|
| 15 | +from test.support.venv import VirtualEnvironmentMixin |
15 | 16 | import ast
|
16 | 17 | import builtins
|
17 | 18 | import glob
|
18 | 19 | import io
|
| 20 | +import json |
19 | 21 | import os
|
20 | 22 | import re
|
21 | 23 | import shutil
|
|
24 | 26 | import sys
|
25 | 27 | import sysconfig
|
26 | 28 | import tempfile
|
| 29 | +import textwrap |
27 | 30 | import urllib.error
|
28 | 31 | import urllib.request
|
29 | 32 | from unittest import mock
|
@@ -456,7 +459,7 @@ def cleanup(self, prep=False):
|
456 | 459 | if os.path.exists(self.bad_dir_path):
|
457 | 460 | os.rmdir(self.bad_dir_path)
|
458 | 461 |
|
459 |
| -class ImportSideEffectTests(unittest.TestCase): |
| 462 | +class ImportSideEffectTests(unittest.TestCase, VirtualEnvironmentMixin): |
460 | 463 | """Test side-effects from importing 'site'."""
|
461 | 464 |
|
462 | 465 | def setUp(self):
|
@@ -576,6 +579,31 @@ def test_license_exists_at_url(self):
|
576 | 579 | code = e.code
|
577 | 580 | self.assertEqual(code, 200, msg="Can't find " + url)
|
578 | 581 |
|
| 582 | + @support.requires_subprocess() |
| 583 | + def test_system_site_packages(self): |
| 584 | + script = textwrap.dedent(""" |
| 585 | + import sys, json |
| 586 | +
|
| 587 | + print(json.dumps( |
| 588 | + sys.path, |
| 589 | + indent=2, |
| 590 | + )) |
| 591 | + """) |
| 592 | + |
| 593 | + # Use _get_preferred_schemes to find the system scheme, in case we are in a virtual environment |
| 594 | + system_scheme_name = sysconfig._get_preferred_schemes()['prefix'] |
| 595 | + system_paths = sysconfig.get_paths(system_scheme_name) |
| 596 | + |
| 597 | + with self.venv(system_site_packages=False) as venv: |
| 598 | + sys_path = json.loads(venv.run('-c', script).stdout) |
| 599 | + assert system_paths['purelib'] not in sys_path, sys_path |
| 600 | + assert system_paths['platlib'] not in sys_path, sys_path |
| 601 | + |
| 602 | + with self.venv(system_site_packages=True) as venv: |
| 603 | + sys_path = json.loads(venv.run('-c', script).stdout) |
| 604 | + assert system_paths['purelib'] in sys_path, sys_path |
| 605 | + assert system_paths['platlib'] in sys_path, sys_path |
| 606 | + |
579 | 607 |
|
580 | 608 | class StartupImportTests(unittest.TestCase):
|
581 | 609 |
|
|
0 commit comments