From 4127cf401809d6631ca5a4c94ddb54155a87b66e Mon Sep 17 00:00:00 2001 From: Stanislav Zmiev Date: Mon, 2 Jan 2023 03:38:58 +0400 Subject: [PATCH 1/4] gh-89727: Improve os.walk complexity and speed --- Lib/os.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Lib/os.py b/Lib/os.py index 73a5442ee8b83f..a5d0fb2e18db1a 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -279,6 +279,7 @@ def renames(old, new): __all__.extend(["makedirs", "removedirs", "renames"]) + def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree generator. @@ -341,11 +342,11 @@ def walk(top, topdown=True, onerror=None, followlinks=False): """ sys.audit("os.walk", top, topdown, onerror, followlinks) - stack = [(False, fspath(top))] + stack = [fspath(top)] islink, join = path.islink, path.join while stack: - must_yield, top = stack.pop() - if must_yield: + top = stack.pop() + if isinstance(top, tuple): yield top continue @@ -422,13 +423,13 @@ def walk(top, topdown=True, onerror=None, followlinks=False): # the caller can replace the directory entry during the "yield" # above. if followlinks or not islink(new_path): - stack.append((False, new_path)) + stack.append(new_path) else: # Yield after sub-directory traversal if going bottom up - stack.append((True, (top, dirs, nondirs))) + stack.append((top, dirs, nondirs)) # Traverse into sub-directories for new_path in reversed(walk_dirs): - stack.append((False, new_path)) + stack.append(new_path) __all__.append("walk") From 5178f2428f25720c257dcad61596e9547f25c6c9 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 1 Jan 2023 23:57:02 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst diff --git a/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst b/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst new file mode 100644 index 00000000000000..9029ed4fd396f0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst @@ -0,0 +1 @@ +Simplify and optimize os.walk by using isinstance checks to check the top of the stack. From c08769700604247f1328b3e16bfa06abd90bcc38 Mon Sep 17 00:00:00 2001 From: Stanislav Zmiev Date: Mon, 2 Jan 2023 04:24:01 +0400 Subject: [PATCH 3/4] Remove unnecessary whitespace --- Lib/os.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/os.py b/Lib/os.py index a5d0fb2e18db1a..598c9e502301f7 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -279,7 +279,6 @@ def renames(old, new): __all__.extend(["makedirs", "removedirs", "renames"]) - def walk(top, topdown=True, onerror=None, followlinks=False): """Directory tree generator. From 69cc8474018752d4fb7aae4a0f56928d17057887 Mon Sep 17 00:00:00 2001 From: Stanislav Zmiev Date: Mon, 2 Jan 2023 18:26:42 +0400 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst Co-authored-by: Jelle Zijlstra --- .../next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst b/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst index 9029ed4fd396f0..38c0d5c4d5fb6f 100644 --- a/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst +++ b/Misc/NEWS.d/next/Library/2023-01-01-23-57-00.gh-issue-89727.ojedHN.rst @@ -1 +1 @@ -Simplify and optimize os.walk by using isinstance checks to check the top of the stack. +Simplify and optimize :func:`os.walk` by using :func:`isinstance` checks to check the top of the stack.