Skip to content

Commit f4f445b

Browse files
bpo-39567: Add audit for os.walk(), os.fwalk(), Path.glob() and Path.rglob(). (GH-18372)
1 parent 95905ce commit f4f445b

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

Lib/os.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
336336
dirs.remove('CVS') # don't visit CVS directories
337337
338338
"""
339-
top = fspath(top)
339+
sys.audit("os.walk", top, topdown, onerror, followlinks)
340+
return _walk(fspath(top), topdown, onerror, followlinks)
341+
342+
def _walk(top, topdown, onerror, followlinks):
340343
dirs = []
341344
nondirs = []
342345
walk_dirs = []
@@ -410,11 +413,11 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
410413
# the caller can replace the directory entry during the "yield"
411414
# above.
412415
if followlinks or not islink(new_path):
413-
yield from walk(new_path, topdown, onerror, followlinks)
416+
yield from _walk(new_path, topdown, onerror, followlinks)
414417
else:
415418
# Recurse into sub-directories
416419
for new_path in walk_dirs:
417-
yield from walk(new_path, topdown, onerror, followlinks)
420+
yield from _walk(new_path, topdown, onerror, followlinks)
418421
# Yield after recursion if going bottom up
419422
yield top, dirs, nondirs
420423

@@ -455,6 +458,7 @@ def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=
455458
if 'CVS' in dirs:
456459
dirs.remove('CVS') # don't visit CVS directories
457460
"""
461+
sys.audit("os.fwalk", top, topdown, onerror, follow_symlinks, dir_fd)
458462
if not isinstance(top, int) or not hasattr(top, '__index__'):
459463
top = fspath(top)
460464
# Note: To guard against symlink races, we use the standard

Lib/pathlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,7 @@ def glob(self, pattern):
11341134
"""Iterate over this subtree and yield all existing files (of any
11351135
kind, including directories) matching the given relative pattern.
11361136
"""
1137+
sys.audit("pathlib.Path.glob", self, pattern)
11371138
if not pattern:
11381139
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
11391140
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
@@ -1148,6 +1149,7 @@ def rglob(self, pattern):
11481149
directories) matching the given relative pattern, anywhere in
11491150
this subtree.
11501151
"""
1152+
sys.audit("pathlib.Path.rglob", self, pattern)
11511153
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
11521154
if drv or root:
11531155
raise NotImplementedError("Non-relative patterns are unsupported")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added audit for :func:`os.walk`, :func:`os.fwalk`, :meth:`pathlib.Path.glob`
2+
and :meth:`pathlib.Path.rglob`.

0 commit comments

Comments
 (0)