Skip to content

Commit

Permalink
Process pth files even if $PYTHONPATH points to site-packages/ (#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
navytux authored Oct 1, 2020
1 parent 4bdf0f1 commit 865af66
Show file tree
Hide file tree
Showing 3 changed files with 29 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
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"

0 comments on commit 865af66

Please sign in to comment.