Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent accidental partial name matching in editable hooks #3562

Merged
merged 3 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3561.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix accidental name matching in editable hooks.
2 changes: 1 addition & 1 deletion setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ class _EditableFinder: # MetaPathFinder
@classmethod
def find_spec(cls, fullname, path=None, target=None):
for pkg, pkg_path in reversed(list(MAPPING.items())):
if fullname.startswith(pkg):
if fullname == pkg or fullname.startswith(f"{{pkg}}."):
rest = fullname.replace(pkg, "", 1).strip(".").split(".")
return cls._find_spec(fullname, Path(pkg_path, *rest))

Expand Down
24 changes: 24 additions & 0 deletions setuptools/tests/test_editable_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,30 @@ def test_no_recursion(self, tmp_path):
with pytest.raises(ImportError, match="pkg"):
import_module("pkg")

def test_similar_name(self, tmp_path):
files = {
"foo": {
"__init__.py": "",
"bar": {
"__init__.py": "",
}
},
}
jaraco.path.build(files, prefix=tmp_path)

mapping = {
"foo": str(tmp_path / "foo"),
}
template = _finder_template(str(uuid4()), mapping, {})

with contexts.save_paths(), contexts.save_sys_modules():
sys.modules.pop("foo", None)
sys.modules.pop("foo.bar", None)

self.install_finder(template)
with pytest.raises(ImportError, match="foobar"):
import_module("foobar")


def test_pkg_roots(tmp_path):
"""This test focus in getting a particular implementation detail right.
Expand Down