Skip to content

Commit

Permalink
Process pth files even if $PYTHONPATH points to site-packages/
Browse files Browse the repository at this point in the history
Fixes pypa#1959
  • Loading branch information
navytux committed Oct 1, 2020
1 parent 4bdf0f1 commit 400f0b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
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)


sep = "\\" if sys.platform == "win32" else "/" # no os module here yet - poor mans version
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,25 @@ 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
out = subprocess.check_output(
[str(session.creator.exe), "-c", r"import sys; print(sys.testpth)"],
universal_newlines=True,
env={"PYTHONPATH": site_packages},
)
assert out == "ok\n"

0 comments on commit 400f0b7

Please sign in to comment.