From 865af6682c1c244e636588afc1330005dbbd989d Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Thu, 1 Oct 2020 20:37:04 +0300 Subject: [PATCH] Process pth files even if $PYTHONPATH points to site-packages/ (#1960) --- docs/changelog/1960.bugfix.rst | 1 + .../via_global_ref/builtin/python2/site.py | 3 +-- tests/unit/create/test_creator.py | 27 +++++++++++++++++++ 3 files changed, 29 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..97ab88c84 100644 --- a/tests/unit/create/test_creator.py +++ b/tests/unit/create/test_creator.py @@ -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"