Skip to content

Commit 2b58ce2

Browse files
committed
feat(Lib/os): walk supports followlinks=walk_symlinks_as_files
1 parent 1b1653d commit 2b58ce2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/pylib/Lib/os_impl/walkImpl.nim

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ template append[A, B](ls: UnionList[A, B], x: B) =
6161
6262
type ScanDir[T] = iterator (path: T): DirEntry[T]{.closure.}
6363
64+
type WalkSymlinksAsFiles = distinct proc(){.noconv.}
65+
converter toBool(self: WalkSymlinksAsFiles): bool{.used.} = true
66+
let walk_symlinks_as_files* = WalkSymlinksAsFiles proc(){.noconv.} = discard ## _walk_symlinks_as_files
67+
6468
# translated from CPython-3.13-alpha/Lib/os.py L284
6569
iterator walk*[T](top: PathLike[T], topdown=True,
66-
onerror=shallIgnore, followlinks=False): WalkRes[T] =
70+
onerror=shallIgnore, followlinks: bool|WalkSymlinksAsFiles=False
71+
): WalkRes[T] =
6772
sys.audit("os.walk", top, topdown, onerror, followlinks)
6873
var stack = newUnionList[T, WalkRes[T]]()
6974
stack.append(fspath(top))
@@ -102,7 +107,10 @@ iterator walk*[T](top: PathLike[T], topdown=True,
102107
break
103108
let isdir =
104109
try:
105-
entry.is_dir()
110+
when followlinks is WalkSymlinksAsFiles:
111+
entry.is_dir(follow_symlinks=True) and not entry.is_junction()
112+
else:
113+
entry.is_dir()
106114
except OSError:
107115
# If is_dir() raises an OSError, consider the entry not to
108116
# be a directory, same behaviour as os.path.isdir().

0 commit comments

Comments
 (0)