# HG changeset patch # User Franz Glasner # Date 1737123848 -3600 # Node ID b2aba84ca426828f25fec60b59b9691ce6fba30c # Parent c1e875ba4bdc79acfe73bfca11bd0aac72769d08 Implement a "close()" method for cutils.util.walk.ScanDir. Also check for closed iterators when __next__ is called. diff -r c1e875ba4bdc -r b2aba84ca426 cutils/util/walk.py --- a/cutils/util/walk.py Thu Jan 16 23:18:04 2025 +0100 +++ b/cutils/util/walk.py Fri Jan 17 15:24:08 2025 +0100 @@ -171,6 +171,8 @@ return self def __next__(self): + if self._scandir_it is None: + raise StopIteration("closed") return WalkDirEntry.from_direntry(next(self._scandir_it)) if PY2: @@ -180,8 +182,13 @@ return self def __exit__(self, *args, **kwds): - if hasattr(self._scandir_it, "close"): - self._scandir_it.close() + self.close() + + def close(self): + if self._scandir_it is not None: + if hasattr(self._scandir_it, "close"): + self._scandir_it.close() + self._scandir_it = None else: @@ -203,6 +210,8 @@ return self def __next__(self): + if self._listdir_it is None: + raise StopIteration("closed") return WalkDirEntry.from_path_name(self._path, next(self._listdir_it)) @@ -215,6 +224,9 @@ def __exit__(self, *args, **kwds): pass + def close(self): + pass + def getfsencoding(): """Return the stored _FSENCODING of this module"""