Skip to content

Commit 2bbca69

Browse files
committed
pathlib: Fix glob/rglob to return Path objects rather than strings.
This now matches CPython behaviour. Signed-off-by: Damien George <damien@micropython.org>
1 parent 1b75a33 commit 2bbca69

3 files changed

Lines changed: 10 additions & 4 deletions

File tree

python-stdlib/pathlib/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
metadata(version="0.0.1")
1+
metadata(version="0.0.2")
22

33
module("pathlib.py")

python-stdlib/pathlib/pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _glob(self, path, pattern, recursive):
126126
for name, mode, *_ in os.ilistdir(path):
127127
full_path = path + _SEP + name
128128
if name.startswith(prefix) and name.endswith(suffix):
129-
yield full_path
129+
yield Path(full_path)
130130
if recursive and mode & 0x4000: # is_dir
131131
yield from self._glob(full_path, pattern, recursive=recursive)
132132

python-stdlib/pathlib/tests/test_pathlib.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ def test_glob(self):
164164
glob_gen = path.glob("*.txt")
165165
self.assertTrue(_isgenerator(glob_gen))
166166

167-
res = [str(x) for x in glob_gen]
167+
res = list(glob_gen)
168+
for p in res:
169+
self.assertIsInstance(p, Path)
170+
168171
self.assertTrue(len(res) == 2)
169172
self.assertTrue(foo_txt in res)
170173
self.assertTrue(bar_txt in res)
@@ -190,7 +193,10 @@ def test_rglob(self):
190193
glob_gen = path.rglob("*.txt")
191194
self.assertTrue(_isgenerator(glob_gen))
192195

193-
res = [str(x) for x in glob_gen]
196+
res = list(glob_gen)
197+
for p in res:
198+
self.assertIsInstance(p, Path)
199+
194200
self.assertTrue(len(res) == 3)
195201
self.assertTrue(foo_txt in res)
196202
self.assertTrue(bar_txt in res)

0 commit comments

Comments
 (0)