From 55ec5307d927c6b36d0ecfb30eeab101cec0e857 Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Thu, 1 Oct 2020 17:52:30 +0300 Subject: [PATCH 1/2] Process pth files even if $PYTHONPATH points to site-packages/ Fixes https://github.com/pypa/virtualenv/issues/1959 --- docs/changelog/1960.bugfix.rst | 1 + .../via_global_ref/builtin/python2/site.py | 3 +-- tests/unit/create/test_creator.py | 22 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/1960.bugfix.rst diff --git a/docs/changelog/1960.bugfix.rst b/docs/changelog/1960.bugfix.rst new file mode 100644 index 000000000..fcd217083 --- /dev/null +++ b/docs/changelog/1960.bugfix.rst @@ -0,0 +1 @@ +pth files were not processed on CPython2 if $PYTHONPATH was pointing to site-packages/ - by :user:`navytux`. (`#1959 `_) diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/site.py b/src/virtualenv/create/via_global_ref/builtin/python2/site.py index 366908e77..85eee842a 100644 --- a/src/virtualenv/create/via_global_ref/builtin/python2/site.py +++ b/src/virtualenv/create/via_global_ref/builtin/python2/site.py @@ -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 diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py index 68d64b01e..18219fc05 100644 --- a/tests/unit/create/test_creator.py +++ b/tests/unit/create/test_creator.py @@ -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" From 414ae22deabb7e31066b5cfaea24ac083c880da6 Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Thu, 1 Oct 2020 18:17:40 +0300 Subject: [PATCH 2/2] fixup! Process pth files even if $PYTHONPATH points to site-packages/ Try to fix Windows failures (e.g. https://github.com/pypa/virtualenv/runs/1194084193?check_suite_focus=true) --- tests/unit/create/test_creator.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py index 18219fc05..97ab88c84 100644 --- a/tests/unit/create/test_creator.py +++ b/tests/unit/create/test_creator.py @@ -595,9 +595,14 @@ def test_pth_in_site_vs_PYTHONPATH(tmp_path): ) 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={"PYTHONPATH": site_packages}, + env=env, ) assert out == "ok\n"