# HG changeset patch # User Franz Glasner # Date 1735818220 -3600 # Node ID 4a0c3c9eead744836076e911075df10deef6286a # Parent 1e512702825481d0fb7a04e42c3d92d2fafab7d7 Use the output of "scandir()" directly if it has a "close" method. Use a "nullcontext" otherwise. diff -r 1e5127028254 -r 4a0c3c9eead7 cutils/util/walk.py --- 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))