Skip to content

Commit

Permalink
Fix site.getsitepackages() ignoring --system-site-packages on python2 (
Browse files Browse the repository at this point in the history
…#2107)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
freundTech and pre-commit-ci[bot] committed May 4, 2021
1 parent 24f9a7f commit cc91e3f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog/2106.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``site.getsitepackages()`` ignoring ``--system-site-packages`` on python2 - by :user:`freundTech`.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def add_global_site_package():
site.PREFIXES = [sys.base_prefix, sys.base_exec_prefix]
site.main()
finally:
site.PREFIXES = orig_prefixes
site.PREFIXES = orig_prefixes + site.PREFIXES


main()
32 changes: 32 additions & 0 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals

import ast
import difflib
import gc
import json
Expand Down Expand Up @@ -621,3 +622,34 @@ def test_pth_in_site_vs_PYTHONPATH(tmp_path):
env=env,
)
assert out == "ok\n"


def test_getsitepackages_system_site(tmp_path):
import site

old_prefixes = site.PREFIXES
site.PREFIXES = [sys.base_prefix, sys.base_exec_prefix]
system_site_packages = site.getsitepackages()
site.PREFIXES = old_prefixes

# Test without --system-site-packages
session = cli_run([ensure_text(str(tmp_path))])
out = subprocess.check_output(
[str(session.creator.exe), "-c", r"import site; print(site.getsitepackages())"],
universal_newlines=True,
)
site_packages = ast.literal_eval(out)

for system_site_package in system_site_packages:
assert system_site_package not in site_packages

# Test with --system-site-packages
session = cli_run([ensure_text(str(tmp_path)), "--system-site-packages"])
out = subprocess.check_output(
[str(session.creator.exe), "-c", r"import site; print(site.getsitepackages())"],
universal_newlines=True,
)
site_packages = ast.literal_eval(out)

for system_site_package in system_site_packages:
assert system_site_package in site_packages

0 comments on commit cc91e3f

Please sign in to comment.