Skip to content

Commit

Permalink
Merge pull request #193 from pradyunsg/fix-path-pollution-properly
Browse files Browse the repository at this point in the history
Fix path pollution again
  • Loading branch information
takluyver authored Apr 30, 2024
2 parents 60a72a0 + f71a8f5 commit 7e0dbe4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/pyproject_hooks/_in_process/_in_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ def main():
control_dir = sys.argv[2]
if hook_name not in HOOK_NAMES:
sys.exit("Unknown hook: %s" % hook_name)

# Remove the parent directory from sys.path to avoid polluting the backend
# import namespace with this directory.
here = os.path.dirname(__file__)
if here in sys.path:
sys.path.remove(here)

hook = globals()[hook_name]

hook_input = read_json(pjoin(control_dir, "input.json"))
Expand Down
6 changes: 3 additions & 3 deletions tests/samples/path-pollution/setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
import sys
from os import environ, listdir, path
from os import environ, path

from setuptools import setup

children = listdir(sys.path[0])
captured_sys_path = sys.path
out = path.join(environ["TEST_POLLUTION_OUTDIR"], "out.json")
with open(out, "w") as f:
json.dump(children, f)
json.dump(captured_sys_path, f)

setup()
14 changes: 6 additions & 8 deletions tests/test_call_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
UnsupportedOperation,
default_subprocess_runner,
)
from pyproject_hooks._in_process import _in_proc_script_path as in_proc_script_path
from tests.compat import tomllib

SAMPLES_DIR = pjoin(dirname(abspath(__file__)), "samples")
Expand Down Expand Up @@ -196,14 +197,11 @@ def test_path_pollution():
):
hooks.get_requires_for_build_wheel({})
with open(pjoin(outdir, "out.json")) as f:
children = json.load(f)
assert set(children) <= {
"__init__.py",
"__init__.pyc",
"_in_process.py",
"_in_process.pyc",
"__pycache__",
}
captured_sys_path = json.load(f)

with in_proc_script_path() as path:
assert os.path.dirname(path) not in captured_sys_path
assert captured_sys_path[0] == BUILDSYS_PKGS


def test_setup_py():
Expand Down

0 comments on commit 7e0dbe4

Please sign in to comment.