Skip to content

Commit

Permalink
Allow removing entries while iterating over scandir results
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbean-bremen committed Aug 20, 2024
1 parent a54a8e6 commit 27934c3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ The released versions correspond to PyPI releases.
* added support for `os.fchmod`, allow file descriptor argument for `os.chmod` only for POSIX
for Python < 3.13

### Fixes
* removing files while iterating over `scandir` results is now possible (see [#1051](../../issues/1051))

## [Version 5.6.0](https://pypi.python.org/pypi/pyfakefs/5.6.0) (2024-07-12)
Adds preliminary Python 3.13 support.

Expand Down
2 changes: 1 addition & 1 deletion pyfakefs/fake_scandir.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __init__(self, filesystem, path):
self.abspath = self.filesystem.absnormpath(path)
self.path = to_string(path)
entries = self.filesystem.confirmdir(self.abspath, check_exe_perm=False).entries
self.entry_iter = iter(entries)
self.entry_iter = iter(tuple(entries))

def __iter__(self):
return self
Expand Down
14 changes: 14 additions & 0 deletions pyfakefs/tests/fake_os_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5452,6 +5452,20 @@ def test_scandir_with_fd(self):
children = [dir_entry.name for dir_entry in self.os.scandir(fd)]
assert sorted(children) == ["file1", "file2", "subdir"]

def test_file_removed_during_scandir(self):
# regression test for #1051
dir_path = self.make_path("wls")
file1_path = self.os.path.join(dir_path, "1.log")
self.create_file(file1_path)
file2_path = self.os.path.join(dir_path, "2.log")
self.create_file(file2_path)
with self.os.scandir(dir_path) as it:
for entry in it:
if entry.is_file():
self.os.remove(entry.path)
assert not self.os.path.exists(file1_path)
assert not self.os.path.exists(file2_path)

def check_stat(
self, absolute_symlink_expected_size, relative_symlink_expected_size
):
Expand Down

0 comments on commit 27934c3

Please sign in to comment.