|
1 | 1 | import collections |
2 | | -import contextlib |
3 | 2 | import io |
4 | 3 | import os |
5 | 4 | import errno |
@@ -1418,24 +1417,6 @@ def close(self): |
1418 | 1417 | 'st_mode st_ino st_dev st_nlink st_uid st_gid st_size st_atime st_mtime st_ctime') |
1419 | 1418 |
|
1420 | 1419 |
|
1421 | | -class DummyDirEntry: |
1422 | | - """ |
1423 | | - Minimal os.DirEntry-like object. Returned from DummyPath.scandir(). |
1424 | | - """ |
1425 | | - __slots__ = ('name', '_is_symlink', '_is_dir') |
1426 | | - |
1427 | | - def __init__(self, name, is_symlink, is_dir): |
1428 | | - self.name = name |
1429 | | - self._is_symlink = is_symlink |
1430 | | - self._is_dir = is_dir |
1431 | | - |
1432 | | - def is_symlink(self): |
1433 | | - return self._is_symlink |
1434 | | - |
1435 | | - def is_dir(self, *, follow_symlinks=True): |
1436 | | - return self._is_dir and (follow_symlinks or not self._is_symlink) |
1437 | | - |
1438 | | - |
1439 | 1420 | class DummyPath(PathBase): |
1440 | 1421 | """ |
1441 | 1422 | Simple implementation of PathBase that keeps files and directories in |
@@ -1503,25 +1484,14 @@ def open(self, mode='r', buffering=-1, encoding=None, |
1503 | 1484 | stream = io.TextIOWrapper(stream, encoding=encoding, errors=errors, newline=newline) |
1504 | 1485 | return stream |
1505 | 1486 |
|
1506 | | - @contextlib.contextmanager |
1507 | | - def scandir(self): |
1508 | | - path = self.resolve() |
1509 | | - path_str = str(path) |
1510 | | - if path_str in self._files: |
1511 | | - raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path_str) |
1512 | | - elif path_str in self._directories: |
1513 | | - yield iter([path.joinpath(name)._dir_entry for name in self._directories[path_str]]) |
| 1487 | + def iterdir(self): |
| 1488 | + path = str(self.resolve()) |
| 1489 | + if path in self._files: |
| 1490 | + raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path) |
| 1491 | + elif path in self._directories: |
| 1492 | + return iter([self / name for name in self._directories[path]]) |
1514 | 1493 | else: |
1515 | | - raise FileNotFoundError(errno.ENOENT, "File not found", path_str) |
1516 | | - |
1517 | | - @property |
1518 | | - def _dir_entry(self): |
1519 | | - path_str = str(self) |
1520 | | - is_symlink = path_str in self._symlinks |
1521 | | - is_directory = (path_str in self._directories |
1522 | | - if not is_symlink |
1523 | | - else self._symlinks[path_str][1]) |
1524 | | - return DummyDirEntry(self.name, is_symlink, is_directory) |
| 1494 | + raise FileNotFoundError(errno.ENOENT, "File not found", path) |
1525 | 1495 |
|
1526 | 1496 | def mkdir(self, mode=0o777, parents=False, exist_ok=False): |
1527 | 1497 | path = str(self.parent.resolve() / self.name) |
@@ -2214,9 +2184,9 @@ def test_iterdir_nodir(self): |
2214 | 2184 |
|
2215 | 2185 | def test_scandir(self): |
2216 | 2186 | p = self.cls(self.base) |
2217 | | - with p.scandir() as entries: |
| 2187 | + with p._scandir() as entries: |
2218 | 2188 | self.assertTrue(list(entries)) |
2219 | | - with p.scandir() as entries: |
| 2189 | + with p._scandir() as entries: |
2220 | 2190 | for entry in entries: |
2221 | 2191 | child = p / entry.name |
2222 | 2192 | self.assertIsNotNone(entry) |
|
0 commit comments