Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process pth files even if $PYTHONPATH points to site-packages/ #1960

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/changelog/1960.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pth files were not processed on CPython2 if $PYTHONPATH was pointing to site-packages/ - by :user:`navytux`. (`#1959 <https://github.com/pypa/virtualenv/issues/1959>`_)
3 changes: 1 addition & 2 deletions src/virtualenv/create/via_global_ref/builtin/python2/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def load_host_site():
add_site_dir = sys.modules["site"].addsitedir
for path in json.loads(site_packages):
full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
if full_path not in sys.path:
add_site_dir(full_path)
add_site_dir(full_path)
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved


sep = "\\" if sys.platform == "win32" else "/" # no os module here yet - poor mans version
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,30 @@ def test_no_preimport_threading(tmp_path, no_coverage):
)
imported = set(out.splitlines())
assert "threading" not in imported


# verify that .pth files in site-packages/ are always processed even if $PYTHONPATH points to it.
def test_pth_in_site_vs_PYTHONPATH(tmp_path):
session = cli_run([ensure_text(str(tmp_path))])
site_packages = str(session.creator.purelib)
# install test.pth that sets sys.testpth='ok'
with open(os.path.join(site_packages, "test.pth"), "w") as f:
f.write('import sys; sys.testpth="ok"\n')
# verify that test.pth is activated when interpreter is run
out = subprocess.check_output(
[str(session.creator.exe), "-c", r"import sys; print(sys.testpth)"],
universal_newlines=True,
)
assert out == "ok\n"
# same with $PYTHONPATH pointing to site_packages
env = os.environ.copy()
path = [site_packages]
if "PYTHONPATH" in env:
path.append(env["PYTHONPATH"])
env["PYTHONPATH"] = os.pathsep.join(path)
out = subprocess.check_output(
[str(session.creator.exe), "-c", r"import sys; print(sys.testpth)"],
universal_newlines=True,
env=env,
)
assert out == "ok\n"