Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/+glob-literal-parent.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Treat the parent directory literally in ``Path.glob()`` and ``Path.iglob()`` when its name contains wildcard characters.
4 changes: 2 additions & 2 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ def glob(self, pattern: str) -> list[Self]:
:func:`walkdirs` or :func:`walkfiles`.
"""
cls = self._next_class
return [cls(s) for s in glob.glob(self / pattern)]
return [cls(s) for s in glob.glob(cls(glob.escape(self)) / pattern)]

def iglob(self, pattern: str) -> Iterator[Self]:
"""Return an iterator of Path objects that match the pattern.
Expand All @@ -665,7 +665,7 @@ def iglob(self, pattern: str) -> Iterator[Self]:
:func:`walkdirs` or :func:`walkfiles`.
"""
cls = self._next_class
return (cls(s) for s in glob.iglob(self / pattern))
return (cls(s) for s in glob.iglob(cls(glob.escape(self)) / pattern))

#
# --- Reading or writing an entire file at once.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ class TestScratchDir:
Tests that run in a temporary directory (does not test TempDir class)
"""

@pytest.mark.parametrize('method', ['glob', 'iglob'])
def test_glob_literal_parent(self, tmp_path, method):
directory = Path(tmp_path / '[reports]').mkdir()
expected = (directory / 'result.txt').touch()
(directory / 'ignored.csv').touch()
decoy = Path(tmp_path / 'r').mkdir()
(decoy / 'wrong.txt').touch()
assert list(getattr(directory, method)('*.txt')) == [expected]

def test_context_manager(self, tmpdir):
"""Can be used as context manager for chdir."""
d = Path(tmpdir)
Expand Down