Skip to content

Commit c0d9f43

Browse files
committed
Prevent accidental name matching in editable hooks (#3562)
2 parents af1ff35 + 52bf418 commit c0d9f43

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

changelog.d/3561.misc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix accidental name matching in editable hooks.

setuptools/command/editable_wheel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class _EditableFinder: # MetaPathFinder
758758
@classmethod
759759
def find_spec(cls, fullname, path=None, target=None):
760760
for pkg, pkg_path in reversed(list(MAPPING.items())):
761-
if fullname.startswith(pkg):
761+
if fullname == pkg or fullname.startswith(f"{{pkg}}."):
762762
rest = fullname.replace(pkg, "", 1).strip(".").split(".")
763763
return cls._find_spec(fullname, Path(pkg_path, *rest))
764764

setuptools/tests/test_editable_install.py

+24
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,30 @@ def test_no_recursion(self, tmp_path):
515515
with pytest.raises(ImportError, match="pkg"):
516516
import_module("pkg")
517517

518+
def test_similar_name(self, tmp_path):
519+
files = {
520+
"foo": {
521+
"__init__.py": "",
522+
"bar": {
523+
"__init__.py": "",
524+
}
525+
},
526+
}
527+
jaraco.path.build(files, prefix=tmp_path)
528+
529+
mapping = {
530+
"foo": str(tmp_path / "foo"),
531+
}
532+
template = _finder_template(str(uuid4()), mapping, {})
533+
534+
with contexts.save_paths(), contexts.save_sys_modules():
535+
sys.modules.pop("foo", None)
536+
sys.modules.pop("foo.bar", None)
537+
538+
self.install_finder(template)
539+
with pytest.raises(ImportError, match="foobar"):
540+
import_module("foobar")
541+
518542

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

0 commit comments

Comments
 (0)