From d18bfb95522364a542984006edae13bc4ee87636 Mon Sep 17 00:00:00 2001 From: barneygale Date: Sat, 6 Jul 2024 15:21:42 +0100 Subject: [PATCH 1/2] GH-119169: Slightly speed up `os.fwalk()` Handle "disappearing" files as in `walk()`: add them to the `nondirs` list rather than omitting them entirely. --- Lib/os.py | 21 ++++++++----------- ...-07-06-15-19-20.gh-issue-119169.v_JQyl.rst | 2 ++ 2 files changed, 11 insertions(+), 12 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-07-06-15-19-20.gh-issue-119169.v_JQyl.rst diff --git a/Lib/os.py b/Lib/os.py index 4b48afb040e565..f0c86e2abcc267 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -548,19 +548,16 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks): if isbytes: name = fsencode(name) try: - if entry.is_dir(): - dirs.append(name) - if entries is not None: - entries.append(entry) - else: - nondirs.append(name) + is_dir = entry.is_dir() except OSError: - try: - # Add dangling symlinks, ignore disappeared files - if entry.is_symlink(): - nondirs.append(name) - except OSError: - pass + is_dir = False + + if is_dir: + dirs.append(name) + if entries is not None: + entries.append(entry) + else: + nondirs.append(name) if topdown: yield toppath, dirs, nondirs, topfd diff --git a/Misc/NEWS.d/next/Library/2024-07-06-15-19-20.gh-issue-119169.v_JQyl.rst b/Misc/NEWS.d/next/Library/2024-07-06-15-19-20.gh-issue-119169.v_JQyl.rst new file mode 100644 index 00000000000000..444c88f01582b6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-06-15-19-20.gh-issue-119169.v_JQyl.rst @@ -0,0 +1,2 @@ +Slightly speed up :func:`os.fwalk` by simplifying handling of inaccessible +files. From 1b4fb109f9e46d9a1abb40116f39d88cf9321f61 Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 Jul 2024 17:47:19 +0100 Subject: [PATCH 2/2] Simplify diff --- Lib/os.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Lib/os.py b/Lib/os.py index 51cc67a4dd4dba..3e5e379217c6ce 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -532,15 +532,13 @@ def _fwalk(stack, isbytes, topdown, onerror, follow_symlinks): if isbytes: name = fsencode(name) try: - is_dir = entry.is_dir() + if entry.is_dir(): + dirs.append(name) + if entries is not None: + entries.append(entry) + else: + nondirs.append(name) except OSError: - is_dir = False - - if is_dir: - dirs.append(name) - if entries is not None: - entries.append(entry) - else: nondirs.append(name) if topdown: