changeset 123:4a0c3c9eead7

Use the output of "scandir()" directly if it has a "close" method. Use a "nullcontext" otherwise.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 02 Jan 2025 12:43:40 +0100
parents 1e5127028254
children 3bd3f32b5e60
files cutils/util/walk.py
diffstat 1 files changed, 4 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/util/walk.py	Wed Jan 01 18:57:25 2025 +0100
+++ b/cutils/util/walk.py	Thu Jan 02 12:43:40 2025 +0100
@@ -18,7 +18,6 @@
         from scandir import scandir
     except ImportError:
         scandir = None
-from contextlib import closing
 
 from .cm import nullcontext
 from .constants import PY2
@@ -157,12 +156,10 @@
 
         dirs, nondirs, walk_dirs = [], [], []
 
-        scandir_it = scandir(path)
-        if hasattr(scandir_it, "close"):
-            scandir_ctx = closing(scandir_it)
-        else:
-            scandir_ctx = nullcontext(scandir_it)
-        with scandir_ctx as scandir_it:
+        scandir_cm = scandir(path)
+        if not hasattr(scandir_cm, "close"):
+            scandir_cm = nullcontext(scandir_cm)
+        with scandir_cm as scandir_it:
             while True:
                 try:
                     entry = WalkDirEntry.from_direntry(next(scandir_it))