Skip to content

Commit f71a8f5

Browse files
committed
Fix path pollution again
1 parent 60a72a0 commit f71a8f5

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

src/pyproject_hooks/_in_process/_in_process.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ def main():
348348
control_dir = sys.argv[2]
349349
if hook_name not in HOOK_NAMES:
350350
sys.exit("Unknown hook: %s" % hook_name)
351+
352+
# Remove the parent directory from sys.path to avoid polluting the backend
353+
# import namespace with this directory.
354+
here = os.path.dirname(__file__)
355+
if here in sys.path:
356+
sys.path.remove(here)
357+
351358
hook = globals()[hook_name]
352359

353360
hook_input = read_json(pjoin(control_dir, "input.json"))
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import json
22
import sys
3-
from os import environ, listdir, path
3+
from os import environ, path
44

55
from setuptools import setup
66

7-
children = listdir(sys.path[0])
7+
captured_sys_path = sys.path
88
out = path.join(environ["TEST_POLLUTION_OUTDIR"], "out.json")
99
with open(out, "w") as f:
10-
json.dump(children, f)
10+
json.dump(captured_sys_path, f)
1111

1212
setup()

tests/test_call_hooks.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
UnsupportedOperation,
1717
default_subprocess_runner,
1818
)
19+
from pyproject_hooks._in_process import _in_proc_script_path as in_proc_script_path
1920
from tests.compat import tomllib
2021

2122
SAMPLES_DIR = pjoin(dirname(abspath(__file__)), "samples")
@@ -196,14 +197,11 @@ def test_path_pollution():
196197
):
197198
hooks.get_requires_for_build_wheel({})
198199
with open(pjoin(outdir, "out.json")) as f:
199-
children = json.load(f)
200-
assert set(children) <= {
201-
"__init__.py",
202-
"__init__.pyc",
203-
"_in_process.py",
204-
"_in_process.pyc",
205-
"__pycache__",
206-
}
200+
captured_sys_path = json.load(f)
201+
202+
with in_proc_script_path() as path:
203+
assert os.path.dirname(path) not in captured_sys_path
204+
assert captured_sys_path[0] == BUILDSYS_PKGS
207205

208206

209207
def test_setup_py():

0 commit comments

Comments
 (0)