diff options
| author | Barney Gale <barney.gale@gmail.com> | 2024-01-20 02:10:25 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-20 02:10:25 (GMT) |
| commit | 6313cdde58f34648a430d2830357c9d2a5b67b87 (patch) | |
| tree | ab329b153fb95322934d83eaf62b9a6749d0f09c /Lib/test/test_pathlib/test_pathlib.py | |
| parent | 681e9e85a2c1f72576ddfbd766506e2d6db34862 (diff) | |
| download | cpython-6313cdde58f34648a430d2830357c9d2a5b67b87.zip cpython-6313cdde58f34648a430d2830357c9d2a5b67b87.tar.gz cpython-6313cdde58f34648a430d2830357c9d2a5b67b87.tar.bz2 | |
GH-79634: Accept path-like objects as pathlib glob patterns. (#114017)
Allow `os.PathLike` objects to be passed as patterns to `pathlib.Path.glob()` and `rglob()`. (It's already possible to use them in `PurePath.match()`)
While we're in the area:
- Allow empty glob patterns in `PathBase` (but not `Path`)
- Speed up globbing in `PathBase` by generating paths with trailing slashes only as a final step, rather than for every intermediate directory.
- Simplify and speed up handling of rare patterns involving both `**` and `..` segments.
Diffstat (limited to 'Lib/test/test_pathlib/test_pathlib.py')
| -rw-r--r-- | Lib/test/test_pathlib/test_pathlib.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 61d7939..bdbe923 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -1818,6 +1818,13 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest): list(base.walk()) list(base.walk(top_down=False)) + def test_glob_empty_pattern(self): + p = self.cls('') + with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): + list(p.glob('')) + with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): + list(p.glob('.')) + def test_glob_many_open_files(self): depth = 30 P = self.cls @@ -1860,6 +1867,22 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest): with self.assertWarns(FutureWarning): p.rglob('*/**') + def test_glob_pathlike(self): + P = self.cls + p = P(self.base) + pattern = "dir*/file*" + expect = {p / "dirB/fileB", p / "dirC/fileC"} + self.assertEqual(expect, set(p.glob(P(pattern)))) + self.assertEqual(expect, set(p.glob(FakePath(pattern)))) + + def test_rglob_pathlike(self): + P = self.cls + p = P(self.base, "dirC") + pattern = "**/file*" + expect = {p / "fileC", p / "dirD/fileD"} + self.assertEqual(expect, set(p.rglob(P(pattern)))) + self.assertEqual(expect, set(p.rglob(FakePath(pattern)))) + @only_posix class PosixPathTest(PathTest, PurePosixPathTest): |
