Skip to content

Commit 7d06a0a

Browse files
authored
gh-120037: Disable user site packages when a ._pth file is used (GH-137428)
1 parent e39255e commit 7d06a0a

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

Doc/c-api/init_config.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,6 +2258,7 @@ If a ``._pth`` file is present:
22582258
* Set :c:member:`~PyConfig.isolated` to ``1``.
22592259
* Set :c:member:`~PyConfig.use_environment` to ``0``.
22602260
* Set :c:member:`~PyConfig.site_import` to ``0``.
2261+
* Set :c:member:`~PyConfig.user_site_directory` to ``0`` (since 3.15).
22612262
* Set :c:member:`~PyConfig.safe_path` to ``1``.
22622263
22632264
If :c:member:`~PyConfig.home` is not set and a ``pyvenv.cfg`` file is present in
@@ -2278,6 +2279,12 @@ The ``__PYVENV_LAUNCHER__`` environment variable is used to set
22782279
therefore affected by :option:`-S`.
22792280
22802281
2282+
.. versionchanged:: 3.15
2283+
2284+
:c:member:`~PyConfig.user_site_directory` is now set to ``0`` when a
2285+
``._pth`` file is present.
2286+
2287+
22812288
Py_GetArgcArgv()
22822289
================
22832290

Lib/test/test_site.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,15 @@ def test_underpth_dll_file(self):
804804
)], env=env)
805805
self.assertTrue(rc, "sys.path is incorrect")
806806

807+
@support.requires_subprocess()
808+
def test_underpth_no_user_site(self):
809+
pth_lines = [test.support.STDLIB_DIR, 'import site']
810+
exe_file = self._create_underpth_exe(pth_lines)
811+
p = subprocess.run([exe_file, '-X', 'utf8', '-c',
812+
'import sys; '
813+
'sys.exit(not sys.flags.no_user_site)'])
814+
self.assertEqual(p.returncode, 0, "sys.flags.no_user_site was 0")
815+
807816

808817
class CommandLineTests(unittest.TestCase):
809818
def exists(self, path):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Disable user site packages directory when a ``._pth`` file is used, even if
2+
it contains ``import site``.

Modules/getpath.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ def search_up(prefix, *landmarks, test=isfile):
790790
config['isolated'] = 1
791791
config['use_environment'] = 0
792792
config['site_import'] = 0
793+
config['user_site_directory'] = 0
793794
config['safe_path'] = 1
794795
pythonpath = []
795796
for line in pth:

0 commit comments

Comments
 (0)