GH-125413: Revert addition of pathlib.Path.scandir() method (#127377)

Remove documentation for `pathlib.Path.scandir()`, and rename the method to
`_scandir()`. In the private pathlib ABCs, make `iterdir()` abstract and
call it from `_scandir()`.

It's not worthwhile to add this method at the moment - see discussion:
https://discuss.python.org/t/ergonomics-of-new-pathlib-path-scandir/71721

Co-authored-by: Steve Dower <steve.dower@microsoft.com>
This commit is contained in:
Barney Gale 2024-12-05 21:39:43 +00:00 committed by GitHub
parent f4f530804b
commit 8b3cccf3f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 22 additions and 85 deletions

View file

@ -1,5 +1,4 @@
import collections
import contextlib
import io
import os
import errno
@ -1418,24 +1417,6 @@ DummyPathStatResult = collections.namedtuple(
'st_mode st_ino st_dev st_nlink st_uid st_gid st_size st_atime st_mtime st_ctime')
class DummyDirEntry:
"""
Minimal os.DirEntry-like object. Returned from DummyPath.scandir().
"""
__slots__ = ('name', '_is_symlink', '_is_dir')
def __init__(self, name, is_symlink, is_dir):
self.name = name
self._is_symlink = is_symlink
self._is_dir = is_dir
def is_symlink(self):
return self._is_symlink
def is_dir(self, *, follow_symlinks=True):
return self._is_dir and (follow_symlinks or not self._is_symlink)
class DummyPath(PathBase):
"""
Simple implementation of PathBase that keeps files and directories in
@ -1503,25 +1484,14 @@ class DummyPath(PathBase):
stream = io.TextIOWrapper(stream, encoding=encoding, errors=errors, newline=newline)
return stream
@contextlib.contextmanager
def scandir(self):
path = self.resolve()
path_str = str(path)
if path_str in self._files:
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path_str)
elif path_str in self._directories:
yield iter([path.joinpath(name)._dir_entry for name in self._directories[path_str]])
def iterdir(self):
path = str(self.resolve())
if path in self._files:
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path)
elif path in self._directories:
return iter([self / name for name in self._directories[path]])
else:
raise FileNotFoundError(errno.ENOENT, "File not found", path_str)
@property
def _dir_entry(self):
path_str = str(self)
is_symlink = path_str in self._symlinks
is_directory = (path_str in self._directories
if not is_symlink
else self._symlinks[path_str][1])
return DummyDirEntry(self.name, is_symlink, is_directory)
raise FileNotFoundError(errno.ENOENT, "File not found", path)
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
path = str(self.parent.resolve() / self.name)
@ -2214,9 +2184,9 @@ class DummyPathTest(DummyPurePathTest):
def test_scandir(self):
p = self.cls(self.base)
with p.scandir() as entries:
with p._scandir() as entries:
self.assertTrue(list(entries))
with p.scandir() as entries:
with p._scandir() as entries:
for entry in entries:
child = p / entry.name
self.assertIsNotNone(entry)