changeset 199:b2aba84ca426

Implement a "close()" method for cutils.util.walk.ScanDir. Also check for closed iterators when __next__ is called.
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 17 Jan 2025 15:24:08 +0100
parents c1e875ba4bdc
children 22f92bf3572c
files cutils/util/walk.py
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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"""