Skip to content

Speed up posixpath.realpath() in the common case #125159

@nineteendo

Description

@nineteendo

Feature or enhancement

Proposal:

In the common case we go from 2 condition checks to 1:

diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index fccca4e066b76f..89d8267d927c65 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -428,13 +428,14 @@ def _realpath(filename, strict=False, sep=sep, curdir=curdir, pardir=pardir,
 
     while rest:
         name = rest.pop()
-        if name is None:
-            # resolved symlink target
-            seen[rest.pop()] = path
-            continue
         if not name or name == curdir:
-            # current dir
-            continue
+            if name is None:
+                # resolved symlink target
+                seen[rest.pop()] = path
+                continue
+            else:
+                # current dir
+                continue
         if name == pardir:
             # parent dir
             path = path[:path.rindex(sep)] or sep

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions